Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

serial port problems - two PCS are or communicating

Status
Not open for further replies.

infineonintern

Newbie level 4
Joined
Apr 21, 2009
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,334
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?):
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!
 

Re: serial port problems

Well, this definitely not how I would do I/O on the serial port, but since I don’t intend to give a course in programming, I will just name that a proper approach is to open the file with the FILE_FLAG_OVERLAPPED set, provide a properly initialized OVERLAPPED structure and use different threads for reading and writing with some synchronization in place.

Now in your example, I can’t help noticing that you call WriteFile with a pointer to a buffer that you only fill later on. Not only is this not a nice thing to do, but it won’t work because in non-overlapped mode WriteFile won’t return until it’s done writing ‘maxBytes’ bytes of garbage, so subsequent alterations to the buffer are meaningless.

At the other end of the pipe, you call ReadFile once in what it seems as an attempt to test for errors? In reality this call will not return until it either reads ‘maxBytes’ bytes, or receives an EOF. In any case you discard the read data when you do subsequent call in the ‘else’ path (assuming the call succeeds).

To summarize:
- on the read-side, remove the ‘if/else’ and keep only the ‘while’;
- on the write-side, initlialize the buffer before the ‘while’; remove the ‘if/else’, but keep the WriteFile (in a ‘while’, if you want).

Even if this works, I’d suggest you look into doing it asynchronously, as it should be done.

Arthur
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top