I have tried to read data from sensors. The sensors controller is using db9 header (com1), because I will use com1, I use db9 to usb converter and get com 11.
I have program to read and write to the serial port, it work when I use com1, but when I change to com 11, the program fail to open the com because it reach ERROR_FILE_NOT_FOUND
here is my program for the serial port programming:
Serial::Serial(char *portName)
{
this->connected = false;
wchar_t wcPort[64];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcPort, strlen(portName), portName, _TRUNCATE);
this->hSerial = CreateFile(wcPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
//Check if the connection was successfull
if(this->hSerial==INVALID_HANDLE_VALUE)
{
//If not success full display an Error
if(GetLastError()==ERROR_FILE_NOT_FOUND){
//Print Error if neccessary
printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
}
else
{
printf("ERROR!!!");
}
}
else
{
DCB dcbSerialParams = {0};
if (!GetCommState(this->hSerial, &dcbSerialParams))
{
printf("failed to get current serial parameters!");
}
else
{
dcbSerialParams.BaudRate=CBR_38400;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
dcbSerialParams.fOutX=TRUE;
dcbSerialParams.fInX=TRUE;
if(!SetCommState(hSerial, &dcbSerialParams))
{
printf("ALERT: Could not set Serial Port parameters");
}
else
{
this->connected = true;
}
}
}
}
Is it because the software/program or the hardware problem?
when I try with hyperterminal, it can read and write the com 11.
To open COM ports numbered 10 and higher, you need to prefix the name with \\.\.
Now, in C, you must escape all those backslashes. So the port you need to open is "\\\\.\\COM11".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With