本文介绍了字节无法正确读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好。我试图从串口读取一个字节数据。我从高级串口监视器读取的数据是正确的但是当我尝试使用C#读取数据时,会读取不正确的数据。这是我的一段代码   SerialPort serialPort1 = new SerialPort(" COM3", 19200,Parity.None,8); serialPort1.PortName =" COM3"; serialPort1。 BaudRate = 19200; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; int received_byte;   while(true) { 尝试 { serialPort1.Open(); received_byte = serialPort1.ReadByte(); textBox1.Text = received_byte.ToString(); //进行一些数据处理   } catch(System.Exception ex) { label1.Text = ex.Message; }   serialPort1.Close(); } 任何人都可以建议任何解决方案。 Thanx Ahsan 解决方案 试试这个 SerialPort serialport1 = new SerialPort( "COM3",19200,Parity.None,8,StopBits.One); byte [] rcvByte = new byte [1];  //因为你想要读取1个字节的数据 serialport1.Open(); serialport1.Read(rcvByte,0,rcvByte.Length ); serialport1.Close(); rcvByte将包含接收数据的字节。Hello everyone. I am trying to read one byte data from the serial port. The data I read from Advanced serial port monitor is correct but when I try to read the data using C#, incorrect data is read. Here is a piece of my code SerialPort serialPort1 = new SerialPort("COM3", 19200, Parity.None, 8);serialPort1.PortName = "COM3";serialPort1.BaudRate = 19200;serialPort1.DataBits = 8;serialPort1.Parity = Parity.None;serialPort1.StopBits = StopBits.One;int received_byte; while (true){try{serialPort1.Open();received_byte = serialPort1.ReadByte();textBox1.Text=received_byte.ToString();//do some processing of data }catch (System.Exception ex){label1.Text = ex.Message;} serialPort1.Close();}Can anyone suggest any solution plz.ThanxAhsan 解决方案 try thisSerialPort serialport1=new SerialPort("COM3",19200,Parity.None,8,StopBits.One);byte[] rcvByte=new byte[1];  //since you want to read 1 byte of dataserialport1.Open();serialport1.Read(rcvByte,0,rcvByte.Length);serialport1.Close();rcvByte will have the byte of received data. 这篇关于字节无法正确读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-12 20:09