Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can send/Can't read .NET serial port [duplicate]

Possible Duplicate:
C# serialport and hyperterminal

Problem

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?

Project Info:

  • WPF
  • .NET 3.5 (not the Client Profile version)
  • Developing on Windows 7
  • Visual Studio 2010
  • DA-15 serial to USB

Hyperterm settings:

  • COM20
  • Bits per second: 115200
  • Data bits: 8
  • Parity: None
  • Stop bits: 1
  • Flow control: None

C# Code:

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);

Edit:

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.

like image 459
tehDorf Avatar asked Jun 27 '12 00:06

tehDorf


1 Answers

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.

like image 129
Mark Hall Avatar answered Sep 28 '22 02:09

Mark Hall