是字符串处理还是串口通讯有问题?
用serialport 控件做串口通讯,接受数据后
- C# code
public void serialPort_ReceiveData(object sender, SerialDataReceivedEventArgs e) { string s = ""; byte[] APIcmd = new byte[34]; this.serialPort1.Read(APIcmd, 0, APIcmd.Length); s = System.Text.ASCIIEncoding.ASCII.GetString(APIcmd);//这样处理得到的结果是s="123\0\0\0\0\0\0\0\0\0\0" //for (int i = 0; i < APIcmd.Length; i++) //{ // s += APIcmd[i].ToString(); //}//这样处理的结果是s="495051000000000000" this.textBox1.Invoke(new testTextBox(stringTest), new object[] { s.Trim() }); }
现在要得到“123",要怎么处理啊?
因为s="123\0\0\0\0\0\0\0"写入数据库的时候会有问题
[解决办法]
不懂,帮顶啊
[解决办法]
read 方法返回了取得的长度 你没有用到
int count = this.serialPort1.Read(APIcmd, 0, APIcmd.Length);
s = System.Text.ASCIIEncoding.ASCII.GetString(APIcmd, 0, count );//
[解决办法]
好好学习天天向上