![]() |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 3
| serial port to poll on request 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);
}
Someone please help, new in serial port. |
| infineonintern is offline | |
| | #2 |
| Deathray Engineer Join Date: Mar 2007
Posts: 3,211
| Uh, if it's a DOS program, why are you including Windows.h? The black box of fun that comes with Windows XP isn't really DOS... ![]() You might want to make sure you understand what you're programming for, before you get too much into it. This technically isn't a C++ issue, so this may get thrown into another section of the forums by the moderators. Oh, and welcome to cboard.
__________________ |
| MacGyver is offline | |
| | #3 | |
| Registered User Join Date: Aug 2006
Posts: 76
| Quote:
You should have your 'Writer' sit and wait for a go-ahead sequence of characters from the reader, before it begins spewing forth data. That way, start up order isn't important. You also need the bidirectional communications in both programs, and should probably do transfers in discrete blocks, so that you can detect errors (simple block checksums can work here), and re-transmit blocks as required. | |
| rdrast is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can't Read From Serial Port | HalNineThousand | Linux Programming | 14 | 03-20-2008 05:56 PM |
| brace-enclosed error | jdc18 | C++ Programming | 53 | 05-03-2007 05:49 PM |
| Reading and writing to a serial port | SwarfEye | C Programming | 2 | 08-18-2006 12:28 AM |
| need guidance to connect to serial port | gnychis | Linux Programming | 1 | 06-02-2005 10:10 AM |
| DOS, Serial, and Touch Screen | jon_nc17 | A Brief History of Cprogramming.com | 0 | 01-08-2003 04:59 PM |