C#上位机
2016-03-25 11:13
495 查看
今天分配的任务是做出上位机接收单片机发送的四元数,里程数,角度值数据,协议都已经给出,采用串口通信。
a) 波特率:115200
b) 数据位:8
c) 校验位:None
d) 停止位:1
接收数据: int len = serialPort1.BytesToRead;
byte[] data = new byte[len];
serialPort1.Read(data, 0, len);
23是协议中一个包的长,根据实际需要进行更改
for (int i = 0; i < len; i += 23)
{
byte[] newByte = ByteConvertToFloat.SubByte(data, i, 23);//下面定义的一个截取定长字节数组的方法
if (newByte[0] == 0x24)//帧头
{
if (newByte[1] == 0x02)//Paket_Quat
{
if (newByte[2] == 0x00)//四元数标志位
{
//对收到的数据进行处理的代码,字节转整型浮点型等
}
}
}
截取定长数组:
接收到的数据不一定是完整的,要进行验证,而且可以一帧一帧进行处理,方法是照搬的
别人博客上的方法 :http://blog.sina.com.cn/s/blog_903d6d5d0101i7rk.html
public static class ByteConvertToFloat
{
/// <summary>
/// 截取定长的数组字节
/// </summary>
/// <param name="srcBytes">要被截取的源数组</param>
/// <param name="startIndex">开始截取的索引值</param>
/// <param name="length">要截取的字节长度</param>
/// <returns></returns>
public static byte[] SubByte(byte[] srcBytes, int startIndex, int length)
{
System.IO.MemoryStream bufferStream = new System.IO.MemoryStream();
byte[] returnByte = new byte[] { };
if (srcBytes == null) { return returnByte; }
if (startIndex < 0) { startIndex = 0; }
if (startIndex < srcBytes.Length)
{
if (length < 1 || length > srcBytes.Length - startIndex)
{
length = srcBytes.Length - startIndex;
}
bufferStream.Write(srcBytes, startIndex, length);
returnByte = bufferStream.ToArray();
bufferStream.SetLength(0);
bufferStream.Position = 0;
}
bufferStream.Close();
bufferStream.Dispose();
return returnByte;
}
}
有误之处请指正,谢谢,多多交流,共同进步。
a) 波特率:115200
b) 数据位:8
c) 校验位:None
d) 停止位:1
接收数据: int len = serialPort1.BytesToRead;
byte[] data = new byte[len];
serialPort1.Read(data, 0, len);
23是协议中一个包的长,根据实际需要进行更改
for (int i = 0; i < len; i += 23)
{
byte[] newByte = ByteConvertToFloat.SubByte(data, i, 23);//下面定义的一个截取定长字节数组的方法
if (newByte[0] == 0x24)//帧头
{
if (newByte[1] == 0x02)//Paket_Quat
{
if (newByte[2] == 0x00)//四元数标志位
{
//对收到的数据进行处理的代码,字节转整型浮点型等
}
}
}
截取定长数组:
接收到的数据不一定是完整的,要进行验证,而且可以一帧一帧进行处理,方法是照搬的
别人博客上的方法 :http://blog.sina.com.cn/s/blog_903d6d5d0101i7rk.html
public static class ByteConvertToFloat
{
/// <summary>
/// 截取定长的数组字节
/// </summary>
/// <param name="srcBytes">要被截取的源数组</param>
/// <param name="startIndex">开始截取的索引值</param>
/// <param name="length">要截取的字节长度</param>
/// <returns></returns>
public static byte[] SubByte(byte[] srcBytes, int startIndex, int length)
{
System.IO.MemoryStream bufferStream = new System.IO.MemoryStream();
byte[] returnByte = new byte[] { };
if (srcBytes == null) { return returnByte; }
if (startIndex < 0) { startIndex = 0; }
if (startIndex < srcBytes.Length)
{
if (length < 1 || length > srcBytes.Length - startIndex)
{
length = srcBytes.Length - startIndex;
}
bufferStream.Write(srcBytes, startIndex, length);
returnByte = bufferStream.ToArray();
bufferStream.SetLength(0);
bufferStream.Position = 0;
}
bufferStream.Close();
bufferStream.Dispose();
return returnByte;
}
}
有误之处请指正,谢谢,多多交流,共同进步。
相关文章推荐
- C# ListView用法详解
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- c# 如何通过反射 获取\设置属性值
- C#中的ODBC、OLEDB连接
- C#控制鼠标移动
- C#解leetcode 64. Minimum Path Sum
- C# 实体转换为DataTable
- C#入门到精通 笔记二
- C#获取本地IP地址兼容win7和xp
- C# 类构造函数赋值里属性与字段赋值注意项
- C# 对多个文件进行zip压缩
- C#中的静态成员、静态方法、静态类介绍
- 在C#中如何使用正式表达式获取匹配所需数据
- 从0自学C#10--在线绘制类图
- C#Winform子报表功能
- C#的互操作性:缓冲区、结构、指针
- 【C#进阶系列】16 数组
- 【C#进阶系列】15 数组
- C#打印GroupBox控件区域
- c# socket 异步