2
votes

I'm using C# and .NET 4.5, with the Visual Studio 2012 compiler/IDE to open and interact with a serial port. My code is designed to connect to the QSB quadrature-to-USB converter from US Digital.

Here is the code that I'm using to open the port and connect.

this.Port = new SerialPort();

this.Port.BaudRate = 230400;
this.Port.PortName = "COM9";
this.Port.Parity = Parity.None;
this.Port.Handshake = Handshake.None;
this.Port.DataBits = 8;
this.Port.StopBits = StopBits.One;

this.Port.Open();

Setting a breakpoint immediately after this.Port.Open() allows me to verify that the serial port is indeed connected. In another section of code, the following is called in response to a button push:

this.Port.WriteLine("W168");

This command *should cause my hardware to spin a motor, and in fact it does if I send the command using Putty, or using a Python script that I wrote (both using exactly the same settings as the C# code does). Yet nothing happens. I can open the port in Putty or Python and execute the command with the expected results, and then run my C# code and nothing happens.

Am I missing something C# specific that prevents this from working?

For what it's worth, here is my working Python code:

ser = serial.Serial("COM9", 230400, timeout=1)
ser.write(b"W168\n")

Link to pySerial documentation: http://pyserial.sourceforge.net/pyserial_api.html#classes

Default values for fields mentioned in the C# code but not mentioned in the python call above are:

  • bytesize = 8
  • parity = none
  • stopbits = one
  • xonxoff = false
  • rtscts = false
  • dsrdtr = false
1
There is two things that you can try. First, since you have no handshake you need to modify these two values: this.Port.DtrEnable = true; and this.Port.RtsEnable = true;. Second you can decrease your baud rate to a more standard amount like 9600 to see if connectivity is possible. - Ckrempp
I switched the baudrate on the hardware to 115200 (highest standard on most serial ports), connected with Putty to verify that it works, and then tried from C#. No go. BUT, your suggestion to enable Dtr and Rts worked. :-D Thanks, I've been banging my head on this problem for hours. - Caleb Chamberlain
You are welcome, I know I had a similar problem with serial ports and it came down to one of those two solutions. - Ckrempp

1 Answers

6
votes

When working with Serial Ports in C# there is one thing to always remember when establishing a connection. If you set the handshake value to none like this:

this.Port.Handshake = Handshake.None;

Then you need to set a few more parameters as well for the connection to be completed, and they are:

this.Port.DtrEnable = true;
this.Port.RtsEnable = true;

The reaso is because the Dtrenable means this:

Data Terminal Ready (DTR) signal

MSDN explains what DTR means as this:

Data Terminal Ready (DTR) is typically enabled during XON/XOFF software handshaking and Request to Send/Clear to Send (RTS/CTS) hardware handshaking, and modem communications.

Rtsenable means this:

Request to Send (RTS) signal

MSDN explains what RTS means as this:

The Request to Transmit (RTS) signal is typically used in Request to Send/Clear to Send (RTS/CTS) hardware handshaking.

Together these two parameters handle the handshaking of the serial port communications without you having to define it between the master and slave.