Go Back   CodeCall Programming Forum > Software Development > C and C++
Register Blogs Search Today's Posts Mark Forums Read

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-11-2009, 01:06 AM
Newbie
 
Join Date: Jun 2009
Posts: 5
infineonintern is an unknown quantity at this point
serial port to poll on request

I currently have two programs("reading" and "writing") and each program run on one PC to do textfile transferring/logging. The PCs is connected via a null modem cable. The program is run in widows and is a DOS application written in c++. The programs work but it is working in a way that i have to run the "reading" program first to let it poll before i can run the "writing" program to send the content of the text file.

How do i make it in a way that the "reading" program will only start polling upon request from the "writing" program? Meaning, when i clicked the "writing" program to send the data, it will communicate with the "reading" program to start and get ready to receive.


This is my codes for the "reading" side (Upon receiving the content from the "writing" side, it will log it into a text file called "new.txt")
Code:
#include "windows.h"
#include <stdio.h>
#include <ctype.h>
#include <io.h>
#include <conio.h>
#include <stdlib.h>

#define maxBytes 111

int main()
{
	HANDLE hSerial;
	DCB dcbSerialParams = {0};
	COMMTIMEOUTS timeouts = {0};
	DWORD dwBytesRead = 0;
	char szBuff[maxBytes] = {0};
	int firstdigit = 0;
	FILE *fp;
	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\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_115200;
	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 = 40;
	timeouts.ReadTotalTimeoutConstant = 40;
	timeouts.ReadTotalTimeoutMultiplier = 40;
	timeouts.WriteTotalTimeoutConstant = 40;
	timeouts.WriteTotalTimeoutMultiplier = 40;

	//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");
	}

	//reading data
	//ReadFile reads data from the specified file or i/o devices.
	printf("The content inside the file: \n\n");
	if(!ReadFile(hSerial, szBuff, maxBytes, &dwBytesRead, NULL))
	{
		 printf("error\n");
	}
	else
	{
		fp = fopen("c:\\new.txt", "w");
		while (ReadFile(hSerial, szBuff, maxBytes, &dwBytesRead, NULL))
		{
			
			for (i = 0; i < dwBytesRead; i++)
			{
				if (szBuff[i] == 10 || szBuff[i] == 13)
				{
					fprintf(fp, "\n");
					printf("\n");
					break;
				}
				else
				{
				fprintf(fp, "%c", szBuff[i]);
				printf("%c", szBuff[i]);	
				}
			}
						 
		}

		fclose(fp);

	CloseHandle(hSerial);

	}
}



This is my codes for thw "writing" side (The program will read the content inside a textfile called "helloworld.txt" and write it line by line to the serial port)
Code:
#include "windows.h"
#include <stdio.h>
#include <io.h>
#include <conio.h>
#include <stdlib.h>

#define maxBytes 111
#define MAX 256

int main()
{
	HANDLE hSerial;
	DCB dcbSerialParams = {0};
	COMMTIMEOUTS timeouts = {0};
	DWORD dwBytesWrite = 0;
	int i;
	char szBuff[maxBytes];
	char stemp[MAX];
	FILE *fp;


	//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_115200;
	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 = 40;
	timeouts.ReadTotalTimeoutConstant = 40;
	timeouts.ReadTotalTimeoutMultiplier = 40;
	timeouts.WriteTotalTimeoutConstant = 40;
	timeouts.WriteTotalTimeoutMultiplier = 40;

	//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.\n");
	}

	//Writting data
	//WriteFile write data from the specified file or i/o devices.
	printf("The content inside the file: \n\n");
	fp = fopen("c:\\helloworld.txt", "r");
	while ((fgets(stemp, MAX, fp)) != NULL)
	{
		i = sprintf(szBuff, "%s", stemp);
		if (!WriteFile(hSerial, szBuff, maxBytes, &dwBytesWrite, NULL))
		{
			printf("Serial port cannot write file.\n");
		}
		else
		{
				printf("%s", szBuff);
		}
	}
	fclose(fp);
	CloseHandle(hSerial);
}
I know that this is just a small program but how do i make the "reading" program poll only when the "writing" program is trying to send data?

Someone please help, new in serial port.

Last edited by infineonintern; 06-11-2009 at 01:42 AM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-14-2009, 10:31 PM
dargueta's Avatar
Code Warrior
 
Join Date: Oct 2007
Age: 19
Posts: 2,602
dargueta has much to be proud ofdargueta has much to be proud ofdargueta has much to be proud ofdargueta has much to be proud ofdargueta has much to be proud ofdargueta has much to be proud ofdargueta has much to be proud ofdargueta has much to be proud of
Re: serial port to poll on request

You can't, unless you have a small stub running or something like that, which defeats the purpose of what you're asking. The reading program has to be running first because the system doesn't know what to do with the incoming data. What you could do is have the programs exchange control codes to avoid extraneous polling. For example:

Code:
<start reader>
<start writer>
WRITER: Wake up! I have data to send.
READER: I'm ready.
WRITER: Okay. I'm sending you 58127 bytes.
WRITER: <sends data>
READER: I got 58127 bytes.
WRITER: Good. I'm done. Bye. <exits>
READER: Bye. <goes to sleep>
__________________
Cannot delete '002pgin': There is not enough free disk space.
Delete one or more files to free disk space, and then try again.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-16-2009, 03:10 AM
Newbie
 
Join Date: Jun 2009
Posts: 5
infineonintern is an unknown quantity at this point
Re: serial port to poll on request

Quote:
Originally Posted by dargueta View Post
Code:
<start reader>
<start writer>
WRITER: Wake up! I have data to send.
READER: I'm ready.

WRITER: Good. I'm done. Bye. <exits>
READER: Bye. <goes to sleep>
Do you mean that the reader is actually operating in a sleep mode? If it is, how can i do that?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
serial port problems infineonintern C and C++ 0 06-01-2009 11:48 PM
talking to the serial port aloishis89 PHP Forum 3 05-28-2009 09:55 PM
serial port problem mld Pascal/Delphi 1 08-27-2008 11:44 AM
Serial Port Close Problem in Pocket PC HosseinSadeghi C# Programming 0 07-14-2007 02:40 AM


All times are GMT -5. The time now is 08:21 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0