C# serial port DATA read hang problem
I want开发者_如何学Python to continuosly read DATA on serial port
DATA format is A5 30 31 32 32....... 0D
almost 50bytes recieved on one query
I do it with DATA = Convert.ToByte(serialPort1.ReadByte()); line code but program will hang after two three queries.
Kindly anyone share a simple serial port code.
Ashraf
Most likely you have a defective serial port. Try connecting your device to a different port.
Your call to ReadByte() may simply be stuck in an infinite timeout waiting for more data that doesn't come through.
Are you sure you continue to receive data? Are you sure you setup the baudrate, parity, stopbits and handshake correctly?
You can try this SerialPort.ReadExisting Method
Reads all immediately available bytes, based on the encoding, in both the stream and the input buffer of the SerialPort object.
Here is a sample code i used, it works fine to me
public static string ReadMessage(int index)
{
using (SerialPort sp = new SerialPort(_portNumber))
{
sp.Open();
sp.Write("AT+CMGR=" + index + "\r");
Thread.Sleep(2000);
return sp.ReadExisting();
}
}
精彩评论