1

I need to read a byte stream from a lpc1768 that uses a VirtualCOM port to connect to a workstation. SerialPort, however fails to open; after some research:

I/O exception error when using serialport.open()

STM32 USB VCP (Virtual Com Port)

https://electronics.stackexchange.com/questions/161772/stm32-usb-vcp-bug

System.IO.IOException: A device attached to the system is not functioning C# .NET 4.0

SerialPort.Open() - The parameter is incorrect

https://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport

it appears that SerialPort might be expecting COM settings from the MCU; I'm able to receive bytes in teraterm just fine:

enter image description here

, the code sending the bytes being:

void USBCommTask(void const *argument) {    
int ret = -1;
char strMessage[50];

// osDelay sometimes skipped depending depending on allocated stack size
ret = USBD_Initialize(0U);
osDelay(6000);
sprintf(strMessage, "USBD_Initialize returned %d", ret);
PrintDbgMessage(strMessage, 6000);

memset(&strMessage[0], 0, sizeof(strMessage));

ret = USBD_Connect(0U);
osDelay(6000);
sprintf(strMessage, "USBD_Connect returned %d", ret);
PrintDbgMessage(strMessage, 6000);

while(!USBD_Configured (0U));
PrintDbgMessage("USBD configured successfully!", 6000);

// Read from the USB port
// http://www.keil.com/support/man/docs/rlarm/rlarm_usb_create_cdc_acm.htm
// https://community.arm.com/developer/tools-software/tools/f/keil-forum/32812/usbd_cdc_acm_datasend-no-sending
// http://wirelessblue.blogspot.com/2017/05/serial-port-over-usb-project.html
while (1) {
    PrintDbgMessage("Writing to USB ... ", 6000);
    strcpy(strMessage, "ab");
    ret = USBD_CDC_ACM_WriteData(0U, (const uint8_t *)strMessage, strlen(strMessage));
    if (ret > 0) {
        sprintf(strMessage, "Wrote %d bytes of data to USB!", ret);
        PrintDbgMessage(strMessage, 6000);
    } else {
        sprintf(strMessage, "USBD_CDC_ACM_WriteData returned %d", ret);
        PrintDbgMessage(strMessage, 6000);
    }
    PrintDbgMessage("Wrote to USB!", 6000);

} }

I use these settings to (try) to connect to the MCU:

    SerialPort serialPort = null;
    try
    {
        // Create a new SerialPort object with default settings.
        serialPort = new SerialPort();
        // Allow the user to set the appropriate properties.
        serialPort.PortName = "COM6";
        //  serialPort.BaudRate = 9600;
        // Use 115200 in all cases 
        // serialPort.BaudRate = 115200;
        serialPort.BaudRate = 115200;
        serialPort.Parity = 0;
        serialPort.DataBits = 8;
        //        serialPort.StopBits = StopBits.None;
        serialPort.Handshake = Handshake.None;
        // Set the read/write timeouts
        serialPort.ReadTimeout = 500;
        serialPort.WriteTimeout = 500;

        // These settings don't work
        // https://stackoverflow.com/questions/14885288/i-o-exception-error-when-using-serialport-open
        // https://stackoverflow.com/questions/18729146/system-io-ioexception-a-device-attached-to-the-system-is-not-functioning-c-shar
        //serialPort.DtrEnable = true;
        //serialPort.RtsEnable = true;
        // Check the firmware settings:
        // https://electronics.stackexchange.com/questions/161772/stm32-usb-vcp-bug


        serialPort.Open();
        serialPort.DataReceived += WaitForHB01Ack;


        Console.WriteLine("Port opened successfully!");
    }
    catch (UnauthorizedAccessException ex)
    {
        Console.WriteLine(ex.ToString());
        Console.WriteLine("UnauthorizedAccessException");
    }
    catch (IOException ex)
    {
        Console.WriteLine(ex.ToString());
        Console.WriteLine("IOexception");
        //_serialPort.Close();
        //Thread.Sleep(100);
        //_serialPort.Open();
    }

If teraterm is running I get an unauthorized access exception:

    System.UnauthorizedAccessException: Access to the path 'COM6' is denied.
   at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
   at System.IO.Ports.SerialPort.Open()
   at COM_detect.Program.HB01Reader() in E:\Program.cs:line 83

If not:

System.IO.IOException: Ein an das System angeschlossenes Gerät funktioniert nicht.
   at System.IO.Ports.SerialStream.InitializeDCB(Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Boolean discardNull)
   at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
   at System.IO.Ports.SerialPort.Open()
   at COM_detect.Program.HB01Reader() in E:\Program.cs:line 83
IOexception 

Is just SerialPort, not the way to go?

Kev
  • 21
  • 4
  • Check Device Manager and make sure you installed the vendor driver and not the default Microsoft driver. – jdweng Jul 27 '20 at 12:32
  • I cannot find the vendor driver; all that comes up are drivers for osmbed that I'm not used (I have RTOS1). – Kev Jul 27 '20 at 12:44
  • Try following https://www.helpjet.net/Fs-38115880-10418693-35369996.html – jdweng Jul 27 '20 at 12:50
  • I can already see the driver in the device manager. The above link has only an .inf file; I'm reinstalling from https://www.ftdichip.com/Drivers/VCP.htm – Kev Jul 27 '20 at 13:22
  • Read the documentation. I think you may be using the I2C instead of serial port. Read Chapter 14 : https://www.nxp.com/docs/en/user-guide/UM10360.pdf – jdweng Jul 27 '20 at 13:46
  • No, it's the serial port. I've managed to get it to work using the serial port package. – Kev Jul 28 '20 at 07:03
  • I had a problem where the the serial port class would hang when calling the `Open` method while it would work perfectly fine using Tera Term. I created a minimal working version for an STM32 microcontroller and [documented](https://drive.google.com/file/d/1rZRDoJ0WaFs9f6b6Y6WBxMvZTXnd90nx/view?usp=sharing) the process. The tricks involved `DtrEnable` and `RtsEnable` as well as responding to the `CDC_SET_LINE_CODING` control message and responding with the appropriate board rate. If the document helps I will make it an answer to this question. The built in NET SerialPort is a bit finicky. – Kwiksilver Sep 23 '20 at 17:52

0 Answers0