首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

这样使用SerialPort串口通讯有有关问题吗

2012-01-08 
这样使用SerialPort串口通讯有问题吗?测试代码如下:byte[]dataSendnewbyte[4]dataSend[0]0x04dataSend

这样使用SerialPort串口通讯有问题吗?
测试代码如下:
byte[]   dataSend   =   new   byte[4];
                        dataSend[0]   =   0x04;
                        dataSend[1]   =   0x04;
                        dataSend[2]   =   0x00;
                        dataSend[3]   =   0x08;

                        SerialPort   sp   =   new   SerialPort( "COM1 ",   9600,   Parity.Mark,   8,   StopBits.One);
                        sp.Open();
                        this.rtBox.Text   =   "串口打开成功! "   +   "\r\n ";
                        sp.ReadTimeout   =   1000;
                        sp.WriteTimeout   =   1000;
                        try
                        {
                                this.rtBox.Text   +=   "开始发送命令数据... "   +   "\r\n ";
                                sp.Write(dataSend,   0,   4);


                                byte[]   buffer   =   new   byte[14];
                                this.rtBox.Text   +=   "开始接收返回数据... "   +   "\r\n ";

                                System.Threading.Thread.Sleep(2000);
                                int   n   =   sp.BytesToRead;
                                sp.Read(buffer,   0,   buffer.Length);
                                //int   n   =   sp.BytesToRead;

                                this.rtBox.Text   +=   "返回数据: "   +   StrToByte.byteToStr(buffer)   +   "\r\n ";

                        }
                        finally
                        {
                                sp.Close();
                                this.rtBox.Text   +=   "关闭串口成功! ";


                        }

实际情况是,发送接收都正常,但是接受到的数据有乱码出现,请问哪位大侠知道什么原因?谢谢!

[解决办法]
没问题。接收数据和你期望的数据不一样,应该是你的发送的数据格式编码不符合你的需求。

热点排行