c#串口readline()读取数据时碰到的问题。

斯囧图 2012-05-27 10:24:51
本人初学c#一星期,诸多问题请教高手。

我将newline设置为换行符
然后在用串口readline读取数据时碰到一问题

string str;
str = serial.readline();

char[] chardata = str.ToCharArray();

当发送的数据的数据为 13 0D 0A时可以正确读取13

但是当发送数据 88 0D 0A时,在调试窗口可以看到读出的值为63

当发送数据大于0x7F时,读取的数据均为63。请教高手何解。。。。。

还有

以前写c语言时可以很方便的用指针将四个字节组成一个int

char c[4] = {0x01, 0x02, 0x03, 0x04};
int *p = (int *)c;

c#里没有指针,请问有没有类似的方法能达到相同目的

由于是写单片机的上位机程序,类似的问题串口通信时会经常碰到。。。。

谢过先哦~~~~~~~~~~~

我的分不多额。。能给的都给了。。救救我吧
...全文
843 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bdmh 2012-05-28
  • 打赏
  • 举报
回复
BitConverter.ToInt32,可以转换
只在此山中 2012-05-28
  • 打赏
  • 举报
回复
问题
1 代码不完整,不好判断原因
2 88 0D 0A的88是16进制,10进制? 串口读数据排除波特率不对应该没问题.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports; using System.Windows.Forms; namespace SComm { public class Com { private SerialPort sport; /// /// 设置发送缓冲区大小 /// public int outBufferSize { set { sport.WriteBufferSize = value; } } /// /// 设置接收缓冲区大小 /// public int inBufferSize { set { sport.ReadBufferSize = value; } } public Com() { sport = new SerialPort(); } /// /// 初始化 /// /// 端口名称 /// 波特率 /// 校验方式 public Com(string portName,int rate,Parity parity) { sport = new SerialPort(portName,rate,parity); } public bool InitCom() { if (sport.IsOpen) return true; else { try { sport.Open(); return true; } catch (Exception e) { return false; } } } public void Close() { if (sport.IsOpen) sport.Close(); } /// /// 串口设置并打开 /// /// /// /// /// public bool InitCom(string portName, int rate, Parity parity) { if (sport.IsOpen) sport.Close(); sport.BaudRate = rate; sport.PortName = portName; sport.Parity = parity; try { sport.Open(); return true; } catch (Exception e) { return false; } } /// /// 发送字节 /// /// 要发送的字节 /// 发送字节的数量 /// public bool write(byte[] writeBytes,int count) { if (InitCom()) { try { sport.Write(writeBytes, 0, count); return true; } catch (Exception e) { return false; } } return false; } /// /// 发送字符串 /// /// /// public bool write(string writeStrs) { if (InitCom()) { try { sport.Write(writeStrs); System.Threading.Thread.Sleep(100); return true; } catch { return false; } } return false; } /// /// 读取数据 /// /// 读取的字节数 /// public byte[] Read(int NumBytes) { byte[] inbuffer=null; if (sport.IsOpen && sport.BytesToRead > 0) { if (NumBytes > sport.BytesToRead) NumBytes = sport.BytesToRead; try { int b = sport.ReadByte(); string s = sport.ReadExisting(); string s1 = sport.NewLine; // string ss = sport.ReadLine(); inbuffer = new byte[NumBytes]; int count = sport.Read(inbuffer, 0, NumBytes); } catch (TimeoutException) { throw; } } // sport.Close(); return inbuffer; } public byte[] Read() { return Read(sport.BytesToRead); } public string ReadLine() { try { if (sport.IsOpen && sport.BytesToRead > 0) { string s = sport.ReadExisting(); return sport.ReadLine(); } return null; } catch (TimeoutException e) { return e.Message; } } public string ReadExisting() { return sport.ReadExisting(); } public void SendSignal(byte cmd) { byte[] hexdata = new byte[5]; hexdata[0] = 0x01; hexdata[1] = 0x03; hexdata[2] = cmd; ushort crc = CommWithARM. CRC_16(hexdata, 3); hexdata[3] = (byte)(crc / 256); hexdata[4] = (byte)(crc % 256); write(hexdata, 5); } public void OnOnCommMscomm1() { byte[] rxdata= Read(); ////string srxdata = ReadLine(); int i = 0; try ////{ //// foreach (char s in srxdata) //// { //// rxdata[i++] = (byte)s; //// } ////} ////catch { } if (rxdata != null) { int len = rxdata.Length; if (len == 23) { CommWithARM.CheckHandSignal(rxdata); } else if (len >= 53) { CommWithARM.HandData(rxdata, len); } } } } }

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧