COM communication on Windows with C#

Hi we have a DISCO_L476VG and are working on a COM connection between the board and PC.
It will connect properly to the terminal using i.e. putty. But when we want to connect to the board using C# with the System.IO.Ports package it will not work, resulting in a System.IO.IOException. In C# you can check if it is connected. C# says it is connected to the COM port and tries to send data. But my STM board does not log a connection or the message to the ST-Link COM connection. The connect/disconnect and message logging does work if we connect using terminal.

We think it is a driver issue and have tried several drivers. Some result in C# not crashing, but also not receiving the send data.

The C# code does work on other COM devices and we have tested this properly.

Where can I find the proper drivers for windows 10, or which one should I use from the update driver selection. Are there any known problems with mbed OS and C# perhaps?

Hello Luc,

There are three UART ports available on your DISCO-L476VG board:

  • Serial1, available through PB_6, PB_7 pins. An external serial-to-USB converter is needed for connection to a PC.
  • Serial2, available through the USB connector of the serial-to-USB converter built into the onboard ST-Link Probe.
  • Serial4, available trough PA_0, PA_1 pins. An external serial-to-USB converter is needed for connection to a PC.

If you’d like to connect to the DISCO board over Serial2 then you should use the STMicroelectronics STLink Virtual COM Port (COM3) on your PC. The USB driver is provided by STMicroelectronics and it’s installed automatically on Windows 10 and newer.
To use the Serial1 or Serial4 port of your DISCO board you need to connect an external serial-to-USB converter (dongle) to the corresponding pins on the DISCO board and install a USB driver (usually provided by the converter manufacturer) onto your PC.

However, in each case the connection is over UART (despite some section of it is using USB line) so make sure the speed (bit rate), data bit length, parity and number of stop bits are configured to be the same on both ends (DISCO board and PC).

According to the picture above you are trying to use COM4 for the communication but you expect to see a log or message over the ST-Link COM connection.

Was the virtual port COM4 on your PC created by a serial-to-USB driver of a dongle connected to a serial port on the DISCO board?

Hi hudakz,

Thanks for your reply.

I am using the USBSerial class and have a second USB cable connected on USB USER on the underside of the board. Like I said, I can connect using a terminal i.e. putty or mobaxterm, and thus do not need a external UART to Serial/COM device. When using the same COM connection as I do in mobaxterm it does not work in C#.

Also the logs I talked about I added myself after detecting a device was disconnected by polling USBSerial::connected() or after USBSerial::wait_ready()

I am currently testing using the example on this mbed USBSerial page and the example by microsft.

I modified the mbed example to disable the blocking USBSerial connection so it will just send data and not wait to be connected. Also I added a printf so I know when data is send send.
In the TimeoutException on line 69 of the C# example I added a Console.WriteLine and increased the read and write timeout to 2000ms.

C# is polling data from the COM connection, and I know mbed is at least sending data because I can see it receiving in mobaxterm. Yet still no success on the C# side.

you need to set the DTR signal on the PC side. putty does this, some other terminal programs need also a manual setting of DTR.

1 Like

Thanks a lot, This solved our problem!