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:
, 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?