infineonintern
Newbie level 4
setcommtimeouts
To people who handled with serial port programming, please help. I am trying to let two PCS
to generate communication between each other. I am new to serial port programming and have
written two simple programs to run on each PC. The program is a DOS application in windows
and is written in C++. One of PC will be the "reading" PC and the other PC will be the
"writing" PC. The "reading" PC will just read and display how many bytes that is sent from
the serial port of the "writing" PC. The problem now is, when i run the two programs on each
PC, the "reading" PC is reading from its own serial port and the "writing" PC is writing to
its own serial port. They are not handshaking or communicating. I have connected the two
serial ports via a NULL modem cable but it is not working. I wonder is there is anything
wrong with my code or the setting of the handshaking. Please guide!
This is the code for the "reading" PC (the output of the program just printf 0 bytes, how to
let it detect the 9 bytes from the "writing" PC?):
This is the code for "writing" PC:
Please help anyone!
To people who handled with serial port programming, please help. I am trying to let two PCS
to generate communication between each other. I am new to serial port programming and have
written two simple programs to run on each PC. The program is a DOS application in windows
and is written in C++. One of PC will be the "reading" PC and the other PC will be the
"writing" PC. The "reading" PC will just read and display how many bytes that is sent from
the serial port of the "writing" PC. The problem now is, when i run the two programs on each
PC, the "reading" PC is reading from its own serial port and the "writing" PC is writing to
its own serial port. They are not handshaking or communicating. I have connected the two
serial ports via a NULL modem cable but it is not working. I wonder is there is anything
wrong with my code or the setting of the handshaking. Please guide!
This is the code for the "reading" PC (the output of the program just printf 0 bytes, how to
let it detect the 9 bytes from the "writing" PC?):
Code:
#include "windows.h"
#include <stdio.h>
#include <io.h>
#include <conio.h>
#include <stdlib.h>
#define maxBytes 32
#define MAX 128
int main()
{
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
DWORD dwBytesRead = 0;
char szBuff[maxBytes] = {0};
int readbytes, i;
//opening the serial port
hSerial = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
printf("Serial port does not exist\n");
}
printf("Other errors\n");
}
//setting parameters
dcbSerialParams.DCBlength = sizeof (dcbSerialParams);
//GetCommState is to retrieves the current control settings for a specific
communications device.
if (!GetCommState(hSerial, &dcbSerialParams))
{
printf("Not GetCommState, not able to retrieves the current control.\n");
}
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
//SetCommState configures a communications device according to the specifications
//in DCB. The function reinitializes all hardware control settings but it does not
//empty output or input queues
if (!SetCommState(hSerial, &dcbSerialParams))
{
printf("Not SetCommState, cannot configures serial port according to DCB
specifications set\n");
}
//setting timeouts
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 50;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 50;
//SetCommTimeouts set the time out parameters for all read and write operation
if (!SetCommTimeouts(hSerial, &timeouts))
{
printf("Not SetCommTimeouts, cannot set the timeout parameters to serial
port\n");
}
if(!ReadFile(hSerial, szBuff, maxBytes, &dwBytesRead, NULL)){
printf("error\n");
}
else{
while (ReadFile(hSerial, szBuff, maxBytes, &dwBytesRead, NULL))
{
readbytes = sscanf("%d", szBuff);
for (i = 0; i < readbytes; i++)
{
printf("%c",szBuff[i]);
}
printf("\n");
printf("Read %d bytes from serial port.\n",readbytes);
}
//closing down
CloseHandle(hSerial);
}
}
This is the code for "writing" PC:
Code:
#include "windows.h"
#include <stdio.h>
#include <io.h>
#include <conio.h>
#define maxBytes 32
int main()
{
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
DWORD dwBytesWrite = 0;
DWORD dwBytesRead = 0;
char szBuff[maxBytes]= {0};
int i;
//opening the serial port
hSerial = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
printf("Serial port does not exist");
}
printf("Other errors");
}
//setting parameters
dcbSerialParams.DCBlength = sizeof (dcbSerialParams);
//GetCommState is to retrieves the current control settings for a specific
communications device.
if (!GetCommState(hSerial, &dcbSerialParams))
{
printf("Not GetCommState, not able to retrieves the current control.");
}
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
//SetCommState configures a communications device according to the specifications
//in DCB. The function reinitializes all hardware control settings but it does not
//empty output or input queues
if (!SetCommState(hSerial, &dcbSerialParams))
{
printf("Not SetCommState, cannot configures serial port according to DCB
specifications set");
}
//setting timeouts
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 50;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 50;
//SetCommTimeouts set the time out parameters for all reand and write operation
if (!SetCommTimeouts(hSerial, &timeouts))
{
printf("Not SetCommTimeouts, cannot set the timeout parameters to serial
port");
}
//Writting data
//WriteFile write data from the specified file or i/o devices.
if (!WriteFile(hSerial, szBuff, maxBytes, &dwBytesWrite, NULL))
{
printf("Serial port cannot write file");
}
else
{
while(1)
{
i = sprintf(szBuff, "testing .\n");
printf("Writing %d bytes to serial port.\n", i);
}
}
//closing down
CloseHandle(hSerial);
}
Please help anyone!