VS Arduino
Visual Micro >> General Board >> Problem with Leonardo serial port in .NET
https://www.visualmicro.com/forums/YaBB.pl?num=1431524832

Message started by roger co on May 13th, 2015 at 1:47pm

Title: Problem with Leonardo serial port in .NET
Post by roger co on May 13th, 2015 at 1:47pm
I have a sketch running on the Arduino which sends text over the serial port to the PC and responds to commands from the PC

Using a Uno or Leonardo the text is displayed correctly in terminal or monitor window (eg built in serial monitor or TeraTerm)

However using a VB.NET (or C#) Windows forms application nothing is recieved or can be sent to the serial port if a Leonardo is used, but it all works fine with a Uno (exactly the same code at both ends in both cases).

The VB programme scans the available serial ports and finds the one with "Arduino Leonardo" name ok, it opens ok, but the programme can't write to the port (timeout error) and nothing is recieved from the port. Works fine with a Uno.

This is probably not a Visual Micro problem; it appears to be a Visual Studio problem. I just wondered if anyone else has encountered it and if there is a fix/workaround.

When the Leo is connected Windows device manager is reporting it as a keyboard and a mouse and a gamepad and a serial port - are the other functions interferring? But only for an app compiled in Visual studio!
Puzzled of Plymouth

Title: Re: Problem with Leonardo serial port in .NET
Post by Visual Micro on May 13th, 2015 at 3:05pm
So just to be clear. You are not trying to use visual micro to upload but you have your own .net windows program that you want to communicate with the Leonardo?

Title: Re: Problem with Leonardo serial port in .NET
Post by roger co on May 13th, 2015 at 3:12pm
Visual Micro (inside Visual Studio) is used to compile and upload the sketch to the Arduino.

The problem is with the application that then communiates with the Arduino running the sketch over the USB virtual serial port. A Uno works fine. A Leo only works with a teminal emulator (eg TeraTerm). The application sees the port but can't write or read to it. The application works fine for a Uno (or a Mega2560)

As I said this is 99% certain not a VM problem, more a general enquiry whether anyone else has this problem with a Windows Forms application produced in Visual Studio.

It looks like a problem in .NET v4.5 Framework

Title: Re: Problem with Leonardo serial port in .NET
Post by Visual Micro on May 13th, 2015 at 3:26pm
Okay so you are using a different app. The visual micro serial monitor works okay?

It could be a few things.

1) You might be picking up the wrong port for the Leo. It uses one for normal serial and one for upload. Normally the upload port is only detectable when it boots or if you connect to the normal serial port at 1200 baud causing a reboot.

2) The leo requires a short delay before the serial comes up. In the setup() of the arduino code you should have "while (!Serial) {}" which is waiting for the usb to come up.

3) The windows app also needs to wait for the port.

Does your code only attempt to open once. Did you add a button to try manual connect?

Did you try hard coding the port name that visual micro manages to use? That way you can see if your port detection code is getting the wrong port?

I don't think it's a problem with .net

Sorry can't be more help

Title: Re: Problem with Leonardo serial port in .NET
Post by roger co on May 13th, 2015 at 4:39pm
Nope, none of those I'm afraid.
I now have the minimal sketch - ouputs a count every half second - and a minimal windows app - opens a serial port and display numbers recieved.
Uno works, Leo port opens ok but nothing recieved.
In a terminal both Leo and Uno work - numbers dislayed

Attached doc has the minimal sketch and form code if anyone is interested in spotting the error.

(no it doesn't, website error - Could not open a new file on the server, check the paths and chmods D:/Domains/visualmicro.com/www/public_html/yabbfiles/Attachments)

Title: Re: Problem with Leonardo serial port in .NET
Post by Visual Micro on May 13th, 2015 at 4:43pm
I've altered the attachment permissions please try again

Title: Re: Problem with Leonardo serial port in .NET
Post by roger co on May 13th, 2015 at 4:52pm
Amazingly in the meantime (5mins) :) I have stumbled upon the cause of the problem.

For Leonardo when opening the COMport you have to set both DtrEnable and RtsEnable properties true.

Unlike the Uno or Mega2560. The default is that they are false, and many code examples out there set them false explicitly.

Hope this saves someone else some headbanging.

RogerCO

Title: Re: Problem with Leonardo serial port in .NET
Post by RFEFX on Jun 23rd, 2019 at 9:08pm

roger co wrote on May 13th, 2015 at 4:52pm:

For Leonardo when opening the COMport you have to set both DtrEnable and RtsEnable properties true.
se explicitly.

Hope this saves someone else some headbanging.

RogerCO



unfortunately i have been banging my head on the desk trying to figure out why my leonardo would not send anything to my VB program.

Thank you oh so very much !!
Genius in my book !! :D

Title: Re: Problem with Leonardo serial port in .NET
Post by Visual Micro on Jun 24th, 2019 at 12:17pm
Hi both,

You will see that in the Visual Micro serial monitor the default sets DTR and RTS to true. THis is the case for all Arduino boards. In fact I think it is only the ESP boards that do not use this system but they have a config setting to tell us not to enable DTR.

The only time you should see an external code example that does not enable DTR is when triggering the bootloader for certain boards that require a 1200 baud connect to reset the chip and put it on programming mode. The Leonardo is a board that uses this special reset and those are the code examples you might have seen, all relate to upload.

Your own VB programs will not want to upload, instead just use the Serial to the board. In this case you should always set the DTR for all Arduino boards.




VS Arduino » Powered by YaBB 2.6.12!
YaBB Forum Software © 2000-2024. All Rights Reserved.