//* sample C program that access a serial port under Win32 *

#include<windows.h> #include<stdio.h> #include<string.h> int main() { HANDLE hCOM; // file handle for the communication port DCB dcbCOM; // communication port parameters to tweak struct _COMMTIMEOUTS toCOM; // communication port timeout parameters to tweak (WinXP or later) int status; // TRUE or !=0 if OK // declare the structure that describes a file or a device hCOM = CreateFile( TEXT("COM3"), // file or device name GENERIC_READ|GENERIC_WRITE, // access mode: can read and/or write 0, // sharing mode NULL, // security attributes OPEN_EXISTING, // whether to create a new file 0, // file attributes to set, NULL); // template file handle if (hCOM==INVALID_HANDLE_VALUE) { fprintf(stderr, "ERROR: cannot create COM device handle\n"); return(0); } // initialize COM port // get the current COM device parameters status = GetCommState(hCOM, &dcbCOM); if (status) { // modify the parameters to suit our needs dcbCOM.BaudRate = 9600; dcbCOM.ByteSize = 8; dcbCOM.Parity = NOPARITY; dcbCOM.StopBits = ONESTOPBIT; // set the COM device parameters according to that variable status=SetCommState(hCOM, &dcbCOM); if (status) { // set the internal input and output buffer status=SetupComm(hCOM, 256, 256); // ^^^ increase this to store incoming data // if you are not reading too often // Believe me, Windows will not like it if the buffer gets full // ^^^ increase this if you are writing large chunks of data // Note: if you are sending frequently and a lot of data // increasing this buffer will only postpone // the unevitable buffer full error if (status) { // GET CURRENT TIMEOUT PARAMETERS for ReadFile, ReasdFileEx, WriteFile, and WriteFileEx status = GetCommTimeouts(hCOM, &toCOM); if (status) { // toCOM.ReadIntervalTimeout - bytes coming one by one sooner // than that are considered one data chunk transmission // toCOM.ReadTotalTimeoutMultiplier - multipiler to compute the total timeout // based on the requested number of bytes to read // toCOM.ReadTotalTimeoutConstant - additional time added to the timeput above // Actual TIMEOUT = ReadIntervalTimeout*ReadTotalTimeoutMultiplier*(bytes to read) + ReadTotalTimeoutConstant // There are three toCOM.WriteXXXX parameters that define the same for writing // Return immediately from Reading toCOM.ReadIntervalTimeout = MAXDWORD; toCOM.ReadTotalTimeoutConstant = 0; toCOM.ReadTotalTimeoutMultiplier = 0; // SET MODIFIED TIMEOUT PARAMETERS for ReadFile, ReasdFileEx, WriteFile, and WriteFileEx status = SetCommTimeouts(hCOM, &toCOM); if (status) { // setup area to keep data char buffer[1024]; DWORD size=0; DWORD done; for (;;) { // POPULATE A LINE OF TEXT fgets(buffer, sizeof(buffer), stdin); if (strcmp(buffer,".\n")==0) break; buffer[strlen(buffer)]=0; strcat(buffer, "\r\n"); // SAMPLE WRITE // write some data to the file or device status = WriteFile(hCOM, // file or device handle &buffer, // address of buffer with data to send strlen(buffer), // how many bytes should be sent &done, // how many bytes were actually written NULL); // NULL or Overlapped structure if used if (!status) fprintf(stderr, "ERROR: cannot write to COM port\n"); // make sure that everything from internal buffer is sent status=FlushFileBuffers(hCOM); if (!status) fprintf(stderr, "ERROR: cannot flush COM port\n"); // they may be actually sent after flushing // because of the non-full buffer of 1024 bytes // buffers speed your program in general though fprintf(stdout, "%d bytes sent\n", done); // WAIT - allow for processing by the embedded device Sleep(20); // if you attempt to read immediately an incomplete response will be returned // and the remainder of the response will be kept until we read from the UART // for the next time. // A better alternative is to keep reading character by character until // we consume '\n' or whatever is used for the message delimiter without // explicit iddle wait using Sleep(ms) // SAMPLE READ - this will return whatever is in the buffer // as long as there is at least one byte, otherwise, it waits // IF TIMEOUT IS SET then the function returns 0 bytes read // after the timeout expires ReadFile(hCOM, // file or device handle &buffer, // address of buffer to put received data strlen(buffer), // how many bytes are expected &done, // how many bytes were actually received NULL); // must be NULL, not supported buffer[done]=0; // Note: last byte in [done-1] fprintf(stdout, "%d bytes read\n%s\n", done, buffer); } } else fprintf(stderr, "ERROR: cannot set the timeouts for COM port\n"); } else fprintf(stderr, "ERROR: cannot get the timeouts for COM port\n"); } else fprintf(stderr, "ERROR: cannot set buffer size for COM port\n"); } else fprintf(stderr, "ERROR: cannot initialize COM port parameters\n"); } else fprintf(stderr, "ERROR: cannot create COM port parameters record \n"); // release device back to the Operating System CloseHandle(hCOM); return(0); }