ok guys, i have this right now. and it still doesnt work!
p.s. i ran it through a beautifier, hope you like it!!
Code:
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main( int argc, char *argv [] )
{
char character;
char *pcCommPort;
if (argc > 1)
{
pcCommPort = argv[1];
}
else
{
pcCommPort = "COM2";
}
DCB dcb;
HANDLE hSerial;
BOOL fSuccess;
// char *pcCommPort = "COM2"; //Set at com 2 for moment
char szBuff[250 + 1] = { 0 }; // What's wrong with just 251?
DWORD dwBytesRead = 0;
char string[BUFSIZ];
hSerial = CreateFile(pcCommPort, GENERIC_READ |GENERIC_WRITE, 0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hSerial == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hSerial, &dcb);
if (!fSuccess)
{
// Handle the error.
printf("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_57600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hSerial, &dcb);
if (!fSuccess)
{
printf("could not implement port settings");
return (3);
}
printf("Serial port %s successfully reconfigured.\n", pcCommPort);
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=1000;
timeouts.ReadTotalTimeoutConstant=1000;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;
if(!SetCommTimeouts(hSerial, &timeouts)){
//error occureed. Inform user
}
//ReadFile(hSerial, szBuff, 250, &dwBytesRead, NULL);
ReadFile(hSerial, szBuff, 250, &dwBytesRead, NULL);
printf("\nthis many bytes have been read: %s\n\n", dwBytesRead);
sprintf(string, szBuff);
FILE *fp = fopen("bufferdata.txt", "a+b");
{
//*****************Writes contents of decoded final data to the appropriate hour file********
fprintf(fp, "\n"); // write to a new line
fputs(string, fp);
printf("String Written in file\n\n");
//******************Closes the file, flushes all buffers and waits for next cycle*************************
fclose(fp);
fflush(NULL);
printf("All processes successful\n\n");
printf("data obtained : %s", string);
scanf("%c", &character);
}
return 0;
}