Possible Duplicate:
C# serialport and hyperterminal
I'm having trouble with my serial connection. I can send data to my device, but I can't read data. If I use Hyperterm, everyting work fine - I see the data coming in and going out.
Using my code, however, my serial port object never receives any data, but data I send is received by the device.
Any ideas?
SerialPort serial = new SerialPort();
serial.PortName = "COM20";
serial.BaudRate = 115200;
serial.DataBits = 8;
serial.Parity = Parity.None;
serial.StopBits = StopBits.One;
serial.Handshake = Handshake.None;
serial.Open();
serial.Write("Hello World\r\n"); // Echoed on device screen
while (0 == serial.BytesToRead) // Never receive a response
System.Threading.Thread.Sleep(100);
char[] first = new char[serial.BytesToRead];
serial.Read(first, 0, first.Length);
I noted it above, but just to make sure everyone knows, I am using a USB to Serial Port cable. I don't think this is the issue because it works on Hyperterm, but just in case.
Take a look at the MSDN Example they are using a seperate thread to check for Data. You can also subscribe to the DataReceived Event to see if it is firing at all. And there is a Community comment about setting DTR and RTS to true, if the connected device needs them it will not transmit data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With