i made this code to log an input from the serial port (COM2) and store it in a buffer.
the problem is that while it compiles fine, i have to take and test it 50km away in a different office and wanted to know if it works fine before i use it? it´ll be a pain to bring it back to debug.
could anyone try and compile it and if they have a serial device, test it to tell me if it works ( i dont have a serial device here)?
you help would be very much appreciated.
Code:
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
{
DCB dcb;
HANDLE hSerial;
BOOL fSuccess;
char *pcCommPort = "COM2"; //Set at com 2 for moment
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: 9,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_9600; // 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)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
}
HANDLE hSerial;
char szBuff[250 + 1] = {0};
DWORD dwBytesRead = 0;
if(!ReadFile(hSerial, szBuff, 250, &dwBytesRead, NULL)){
char lastError[1024];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lastError,
1024,
NULL);
printf("\nno bytes read!!\n\n");
}else{printf("bytes have been read!!\n\n");
printf("contents of buffer: %s\n\n", szBuff);
}
}