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

1

5

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.

Hamford answered 27/6, 2012 at 0:0 Comment(1)
I checked and I think it is, their answer is the same as Mark's and it worked for me.Hamford
A
7

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.

Arel answered 27/6, 2012 at 0:35 Comment(1)
Setting DTR and RTS to true resolved my read issue which was driving me crazy.Simoneaux

© 2022 - 2024 — McMap. All rights reserved.