Go Down

Topic: Problem talking to Leonardo via USB serial from .NET on Windows (Read 753 times) previous topic - next topic

Corata

First up, I'm a newby here so please be tolerant if I am posting in the wrong place or commit some other transgression of etiquette.

I have been puzzling over a problem that I encountered when porting a design from an Arduino Uno to a Leonardo.  I found related topics in the forum but nothing quite the same.  Eventually I found a solution by the time-honoured method of making random changes until something worked, so here it is in case it helps someone else.

An application written in C# running on .NET in Windows communicated via the USB serial connection with an Arduino Uno OK, running on Windows XP, 7 or 8, but a Leonardo refused to acknowledge the connection.  I could see data being received by the Leonardo's Rx LED flashing so I was talking to the right port but the Leonardo refused to respond.  Both the Uno and the Leonardo played nicely with the Serial Monitor program so both boards were OK.

After puzzling with this for a while I made the following interesting discovery.

The following port set-up (C# code on the PC) works with the Leonardo but not the Uno:

Code: [Select]
port = new SerialPort (portname);  //e.g. "COM3"
port.BaudRate = 38400;             //..or whatever baud rate you need
port.Parity = Parity.None;         //These mirror the port settings
port.StopBits = StopBits.One;      //reported by Window's property page
port.DataBits = 8;                 //for the Arduino's COM port
port.Handshake = Handshake.None;
port.ReadTimeout = 2000;
port.WriteTimeout = 500;
port.DtrEnable = true              //This line is the key
port.Open ();


...wherease the following works with the Uno but not the Leonardo:

Code: [Select]
port = new SerialPort (portname);
port.BaudRate = 38400;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.DataBits = 8;
port.Handshake = Handshake.None;
port.ReadTimeout = 2000;
port.WriteTimeout = 500;
port.DtrEnable = false;             //This line may be omitted for the Uno - still works
port.Open ();


I can't figure out why the DtrEnable property should make any difference given that we have asked for Handshake = None.  Incidentally, selecting other options for the Handshake (XOn/XOff or Cts/Rts) made no difference, so possibly that line of code also is optional.

This all still looks more like a workaround than a fix.  It seems that there is some significant difference in the way in which Java (the Serial Monitor) talks to the USB driver and the way in which .NET talks to it, which might be worth a look but I don't have the knowledge to get down to that driver-level in the code.

I hope that it might be helpful if anybody else out there is working with .NET.
Mike.

Go Up