Serial Port Baudrate setting wronngly

  • Thread starter Thread starter Arun_Kovoor
  • Start date Start date
A

Arun_Kovoor

Guest
Hi

We have Meilhaus ME 9000 RS422/RS485 card installed in a workstation with Windows 10. The dirvers for the card are installed properly (verified in Device manager) and the 8 ports of the cards are appearing as comports. We are able to communicate from one port to another port of the card. We have written a small code to communcate to an external device. While the same code is working in a PC with Windows 7 and the externl unit responds, we are facing a typical issue with windows 10 OS. When we set the baud rate of any port to 9600 bps using the function SetCommState, and send data using writeFile function, the function does not written any error and succeeds, but the external unit does not respond to the data sent. When we verified the data by connecting Oscilloscope to the RX terminals of the port, we observed that data was getting transmitted, but the edffective buad rate was almost half the value. But of verification of baud rate from mode command (typed in command prompt) it is correctly retrieved as 9600. The same test code when run in windows 7, we we see the oscilloscope output the baudrate is correclty set. The following is the code we are using

We are using windows 10 pro with visual studio 2017 having windows SDK version 10.0.17763.0
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <WinBase.h>
#include <strsafe.h>

/*#include <Pcomm.h>*/

HANDLE Cport[17];
char comports[17][10] = { "\\\\.\\COM0","\\\\.\\COM1", "\\\\.\\COM2", "\\\\.\\COM3", "\\\\.\\COM4",
"\\\\.\\COM5", "\\\\.\\COM6", "\\\\.\\COM7", "\\\\.\\COM8",
"\\\\.\\COM9", "\\\\.\\COM10", "\\\\.\\COM11", "\\\\.\\COM12",
"\\\\.\\COM13", "\\\\.\\COM14", "\\\\.\\COM15", "\\\\.\\COM16" };

char mode_str[128];
int cport_nr = 0;

int Serial_OpenComport(int comport_number, int baudrate, const char *mode)
{
DWORD Status = 0;
int error = 0;
DCB port_settings = { 0 };

if ((comport_number > 15) || (comport_number < 0))
{
printf("illegal comport number\n");
return(1);
}

Cport[comport_number] = CreateFileA(comports[comport_number],
GENERIC_READ | GENERIC_WRITE,
0, /* no share */
NULL, /* no security */
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, /* no threads */
NULL); /* no templates */

error = GetLastError();

if (Cport[comport_number] == INVALID_HANDLE_VALUE)
{
printf("unable to open comport \t %d\n", error);
return(1);
}
if (!GetCommState(Cport[comport_number], &port_settings))
{
printf("unable to set comport cfg settings\n");
CloseHandle(Cport[comport_number]);
return(1);
}
port_settings.DCBlength = sizeof(port_settings);

/*port_settings.BaudRate = CBR_9600;

port_settings.ByteSize = 8;

port_settings.StopBits = ONESTOPBIT;

port_settings.Parity = EVENPARITY;*/
port_settings.fDtrControl = 1;
port_settings.fRtsControl = 1;
port_settings.XonLim = 2048;
port_settings.XoffLim = 512;
port_settings.XonChar = 0;
port_settings.XoffChar = 0;

switch (baudrate)
{
case 110: strcpy(mode_str, "baud=110");
port_settings.BaudRate = CBR_110;
break;
case 300: strcpy(mode_str, "baud=300");
port_settings.BaudRate = CBR_300;
break;
case 600: strcpy(mode_str, "baud=600");
port_settings.BaudRate = CBR_600;
break;
case 1200: strcpy(mode_str, "baud=1200");
port_settings.BaudRate = CBR_1200;
break;
case 2400: strcpy(mode_str, "baud=2400");
port_settings.BaudRate = CBR_2400;
break;
case 4800: strcpy(mode_str, "baud=4800");
port_settings.BaudRate = CBR_4800;
break;
case 9600: strcpy(mode_str, "baud=9600");
port_settings.BaudRate = CBR_9600;
break;
case 14400: strcpy(mode_str, "baud=14400");
port_settings.BaudRate = CBR_14400;
break;
case 19200: strcpy(mode_str, "baud=19200");
port_settings.BaudRate = CBR_19200;
break;
case 38400: strcpy(mode_str, "baud=38400");
port_settings.BaudRate = CBR_38400;
break;
case 56000: strcpy(mode_str, "baud=56000");
port_settings.BaudRate = CBR_56000;
break;
case 57600: strcpy(mode_str, "baud=57600");
port_settings.BaudRate = CBR_57600;
break;
case 115200: strcpy(mode_str, "baud=115200");
port_settings.BaudRate = CBR_115200;
break;
case 128000: strcpy(mode_str, "baud=128000");
port_settings.BaudRate = CBR_128000;
break;
case 256000: strcpy(mode_str, "baud=256000");
port_settings.BaudRate = CBR_256000;
break;
case 500000: strcpy(mode_str, "baud=500000");
port_settings.BaudRate = CBR_256000;
break;
case 1000000: strcpy(mode_str, "baud=1000000");
port_settings.BaudRate = CBR_256000;
break;
default: printf("invalid baudrate\n");
return(1);
break;
}

if (strlen(mode) != 3)
{
printf("invalid mode \"%s\"\n", mode);
return(1);
}

switch (mode[0])
{
case '8': strcat(mode_str, " data=8");
port_settings.ByteSize = 8;
break;
case '7': strcat(mode_str, " data=7");
port_settings.ByteSize = 7;
break;
case '6': strcat(mode_str, " data=6");
port_settings.ByteSize = 6;
break;
case '5': strcat(mode_str, " data=5");
port_settings.ByteSize = 5;
break;
default: printf("invalid number of data-bits '%c'\n", mode[0]);
return(1);
break;
}

switch (mode[1])
{
case 'N':
case 'n': strcat(mode_str, " parity=n");
port_settings.Parity = NOPARITY;
break;
case 'E':
case 'e': strcat(mode_str, " parity=e");
port_settings.Parity = EVENPARITY;
break;
case 'O':
case 'o': strcat(mode_str, " parity=o");
port_settings.Parity = ODDPARITY;
break;
default: printf("invalid parity '%c'\n", mode[1]);
return(1);
break;
}

switch (mode[2])
{
case '1': strcat(mode_str, " stop=1");
port_settings.StopBits = ONESTOPBIT;
break;
case '2': strcat(mode_str, " stop=2");
port_settings.StopBits = TWOSTOPBITS;
break;
default: printf("invalid number of stop bits '%c'\n", mode[2]);
return(1);
break;
}

strcat(mode_str, " dtr=on rts=on");


if (!SetCommState(Cport[comport_number], &port_settings))
{
printf("unable to set comport cfg settings\n");
CloseHandle(Cport[comport_number]);
return(1);
}

COMMTIMEOUTS Cptimeouts;

Cptimeouts.ReadIntervalTimeout = MAXDWORD;
Cptimeouts.ReadTotalTimeoutMultiplier = 1;
Cptimeouts.ReadTotalTimeoutConstant = 1;
Cptimeouts.WriteTotalTimeoutMultiplier = 200;
Cptimeouts.WriteTotalTimeoutConstant = 50;

if (!SetCommTimeouts(Cport[comport_number], &Cptimeouts))
{
printf("unable to set comport time-out settings\n");
CloseHandle(Cport[comport_number]);
return(1);
}

return(0);
}

char TempChar; //Temporary character used for reading
char SerialBuffer[256];//Buffer for storing Rxed Data
DWORD NoBytesRead;
int i = 0;

int main()
{
int first_port = -1, second_port = -1, ret_value1=0,ret_value2=0, Status=0, indx=0;
DWORD ret_value = 0,error=0;
char mode[] = { '8','E','1',0 };
unsigned char buffer_write[3] = {60,116,68/*,3,144,0,8,40,0,0,23,48,0,24,16,132,0,0,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,62/*60,26,68,0,0,0,0,48,0,0,120,48,0,0,0,132,0,0,62,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,62 */}, buffer_read[100] = { 0 };

printf("Enter First port for Send:\n");
ret_value = scanf("%d", &first_port);
ret_value1 = Serial_OpenComport(first_port, 9600, mode);


printf("Enter Secont port for send:\n");
ret_value = scanf("%d", &second_port);



ret_value2 = Serial_OpenComport(second_port, 9600, mode);
if (ret_value1 == 0 && ret_value2 == 0)
{
while (1)
{
/*if (ret_value1 == 0 && ret_value2 == 0)*/
{
/*printf("Enter bytes to write\n");
scanf("%s", buffer_write);
printf("Bytes_to_be_written:%s\n", buffer_write);*/


//Sleep(200);
ret_value1 = WriteFile(Cport[first_port], buffer_write, /*strlen((char*)buffer_write)*/ sizeof(buffer_write), (LPDWORD)((void *)&ret_value), NULL);

printf("Write_status_port1:%d\t%d\t%s\n", ret_value1, ret_value, buffer_write);


ret_value2 = WriteFile(Cport[second_port], buffer_write, /*strlen((char*)buffer_write)*/ sizeof(buffer_write), (LPDWORD)((void *)&ret_value), NULL);

printf("Write_status_Port2:%d\t%d\t%s\n", ret_value2, ret_value, buffer_write);


Sleep(20);
#if 0
do
//{
ReadFile(Cport[second_port], //Handle of the Serial port
&buffer_read, //Temporary character
sizeof(buffer_read),//Size of TempChar
&NoBytesRead, //Number of bytes read
NULL);
error = GetLastError();
/*SerialBuffer = TempChar;// Store Tempchar into buffer
i++;*/
//printf("Read_status11:%s\t%d\n", SerialBuffer, NoBytesRead);
} while (NoBytesRead > 0);



printf("Read_status:%s\t%d\n", buffer_read, NoBytesRead);
i = 0;
#endif
/*ret_value2=ReadFile(Cport[second_port], buffer_read, 100, (LPDWORD)((void *)&ret_value), NULL);


printf("Bytes Received:%d\t%d\n", ret_value2, ret_value);
for (indx = 0; indx < ret_value; indx++)
printf("%d\t", buffer_read[indx]);
_getch();
CloseHandle(Cport[second_port]);
CloseHandle(Cport[first_port]);*/
}
}
}
CloseHandle(Cport[first_port]);
CloseHandle(Cport[second_port]);
// getch();

return 0;
}

Continue reading...
 
Back
Top