Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
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:

C++
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);
Posted
Updated 16-Sep-11 4:54am
v2
Comments
CPallini 16-Sep-11 3:20am    
You should post the relevant code here. We cannot guess where the 'magic' happens, without it.
Richard MacCutchan 16-Sep-11 11:50am    
Don't use fgets() and fputs(), but fread() and fwrite(), then you will have complete control over exactly how many bytes are transferred.
amsga 16-Sep-11 11:54am    
The reason why I'm using fgets is due to the requirement that the data has to be sent line by line and I have no idea how long the line can be but I do know that it is smaller than the buffer.
Richard MacCutchan 16-Sep-11 14:49pm    
Fine, but it's still not working! And since all you are doing is trying to recreate the original file there is absolutely no requirement to send it line by line.
amsga 17-Sep-11 9:32am    
I'm actually processing the line in another method. I want to see if what I sent and what I receive are identical.

1 solution

1. Run it again and time it. How long did it take to send the data.
2. Calculate how long it should have taken to send. E.g. at 38400 baud, no parity and one stop bit, each byte will be transmitted as 10 bits (one start bit + 8 data bits + one stop bit), results in 3840 bytes per second.
3. If time to send data is approx. equal to calculated time then the problem is at the receiving side.

Another thought - compare the TX data file to the RX data file - could give you a clue as to where to look for the bug.

Pay special attention to the number of bytes received from the serial port, are you using that number when you write to the file? Are you maybe using the buffer size in each call to WriteFile. (It's a common mistake)

Are you SURE you are using the same setting for both serial ports?

Have fun on your bug hunt :)
 
Share this answer
 
Comments
amsga 18-Sep-11 22:32pm    
I tried using WinMerge to see the difference between the data and most of the time, I'm receiving additional data per line. Only once is a while do the data match on both sides.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900