Hi all,
I created a serial port program that reads a file and sends it across a serial link to another computer.
The file contains about 2MB worth of text data in a txt file.
On my other computer, contains a receiver program that writes what was read through the serial port into a txt file.
The funny thing is, the output file grew to about 20+ MB and I don't recall putting my sender in a loop.
Any advice would be helpful.
I currently do not have the code with me now but this is what I can recall from memory.
Sender Portion:
Win32Port serial = new Win32Port("COM1", 38400, -1, 8, 2);
FILE * in;
in = fopen("data.txt", "r");
if(in == NULL)
{
return -1;
}
int lineNo = 0;
char data[1025];
for(lineNo = 0; fgets(data, 1024, in) != NULL; lineNo++)
{
printf("Line %d has been read.\n", lineNo);
serial->write(data, strlen(data), 1000);
}
fclose(in);
Receiver portion:
HANDLE hCom;
hCom = CreateHandle("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_NORMAL, 0);
if(hCom == INVALID_HANDLE_VALUE)
return -1;
if(!SetCommMask(hCom, EV_CTS | EV_DSR | EV_TXEMPTY | EV_RXHAR))
return -2;
DCB settings;
GetCommState(hCom, settings);
settings.BaudRate = 38400;
settings.ByteSize = 8;
settings.Parity = 0;
settings.StopBits = 2;
if(!SetCommState(hCom, settings))
return -3;
COMMTIMEOUTS cto;
GetCommTimeouts(hCom, cto);
cto.ReadIntervalTimeout = 0;
cto.ReadTotalTimeoutMultiplier = 0;
cto.ReadTotalTimeoutConstant = 0;
if(!SetCommState(hCom, cto))
return -4;
char buffer[2049];
DWORD size = 2048;
DWORD readSize;
COMSTAT comstatus;
FILE * out;
out = fopen("results.txt", w+b);
if(out == NULL)
return -5;
ClearCommError(hCom, NULL, &comstatus);
size = min(2048, comstatus.cbInQue);
while(size > 0)
{
if(ReadFile(hCom, buffer, size, &readSize, NULL)
{
fputs(buffer, out);
fflush(out);
}
else
{
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, buffer, 2049, NULL);
printf("%s\r\n", buffer);
}
}
fclose(out);