AnsweredAssumed Answered

How to configure Serialport in PC program for STM32 Virtual comport?

Question asked by seo.sangwoo.001 on Feb 6, 2018

Hello.

 

I am in trouble with connecting virtual comport in STM32 to PC.

 

I think that the virtual comport is properly working in the MCU, and it is well communicated with TeraTerm (the serial port communication application in PC).

 

Now, I am coding for serial port in C++ and C# for the comport, but an error or exception occurred with serial port error code 87.

 

 

C code.

 

BuadRate = CBR_115200

ByteSize = 8

fParity = FALSE;

Parity = NOPARITY

StopBits = ONESTOPBIT

 

bool CSerialPort::configurePort(DWORD BaudRate, BYTE ByteSize, DWORD fParity,
BYTE Parity, BYTE StopBits)
{
if ((m_bPortReady = GetCommState(m_hComm, &m_dcb)) == 0)
{

MessageBox(NULL, "GetCommState Error", "Error", MB_OK + MB_ICONERROR);
CloseHandle(m_hComm);
return false;
}

m_dcb.BaudRate = BaudRate;
m_dcb.ByteSize = ByteSize;
m_dcb.Parity = Parity;
m_dcb.StopBits = StopBits;
m_dcb.fBinary = true;
m_dcb.fDsrSensitivity = false;
m_dcb.fParity = fParity;
m_dcb.fOutX = false;
m_dcb.fInX = false;
m_dcb.fNull = false;
m_dcb.fAbortOnError = true;
m_dcb.fOutxCtsFlow = false;
m_dcb.fOutxDsrFlow = false;
m_dcb.fDtrControl = DTR_CONTROL_DISABLE;
m_dcb.fDsrSensitivity = false;
m_dcb.fRtsControl = RTS_CONTROL_DISABLE;
m_dcb.fOutxCtsFlow = false;
m_dcb.fOutxCtsFlow = false;

m_bPortReady = SetCommState(m_hComm, &m_dcb);  // <= Error occurs

if (m_bPortReady == 0)
{
CString str1;
str1.Format(_T("%d"), GetLastError());
MessageBox(NULL, str1, "error", MB_OK);
CloseHandle(m_hComm);
return false;
}
return true;
}

 

2. C# Code

SerialPort port = new SerialPort("COM2", 115200, Parity.None, 8, StopBits.One);
port.ReadTimeout = 1000;
port.WriteTimeout = 1000;
try
{
port.Open();
}
catch (Exception exp)
{
Console.WriteLine(exp.ToString());
}

Outcomes