Reputation: 321
I'm trying to establish a UART interface between an external Microcontroller and Windows using C.
I'm using the following code to set up the UART Parameters and then send in a character to the designated COM port.
I'm successful in sending a character. But How do I receive one back ? The code is as follows:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
HANDLE hSerial;
int main()
{
// OPEN SERIAL PORT AND SET INITAL UART PARAMETERS
//=================================================
DCB dcbSerialParams = {0}; COMMTIMEOUTS timeouts = {0};
fprintf(stderr, "Opening serial port...");
hSerial = CreateFile("\\\\.\\COM3", GENERIC_READ|GENERIC_WRITE, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hSerial == INVALID_HANDLE_VALUE){fprintf(stderr, "Error\n");return 1;}
else {fprintf(stderr, "OK\n");}
// Set device parameters (115200 baud, 1 start bit, 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0){fprintf(stderr, "Error getting device state\n");CloseHandle(hSerial);return 1;}
dcbSerialParams.BaudRate = CBR_57600; dcbSerialParams.ByteSize = 8; dcbSerialParams.StopBits = ONESTOPBIT; dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0){fprintf(stderr, "Error setting device parameters\n");CloseHandle(hSerial);return 1;}
// Set COM port timeout settings
timeouts.ReadIntervalTimeout = 50; timeouts.ReadTotalTimeoutConstant = 50; timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50; timeouts.WriteTotalTimeoutMultiplier = 10;
if(SetCommTimeouts(hSerial, &timeouts) == 0){fprintf(stderr, "Error setting timeouts\n"); CloseHandle(hSerial); return 1;}
// SETUP AND SEND DATA FROM UART
//==============================
int VarNum=8;
char str[15];
sprintf(str,"%ld",VarNum);
DWORD bytes_written, total_bytes_written = 0;
fprintf(stderr, "Sending bytes...");
if(!WriteFile(hSerial,str, strlen(str), &bytes_written, NULL))
{
fprintf(stderr, "Error\n");
CloseHandle(hSerial);
return 1;
}
fprintf(stderr, "%d bytes written\n", bytes_written);
// CLOSE SERIAL PORT AND EXIT MAIN FUNCTION
//=========================================
fprintf(stderr, "Closing serial port...");
if (CloseHandle(hSerial) == 0){fprintf(stderr, "Error\n"); return 1;}
fprintf(stderr, "OK\n");return 0;
}
Upvotes: 2
Views: 5549
Reputation: 11406
You can use ReadFile()
for that:
BOOL bOk = ReadFile(hSerial, buffer, sizeof(buffer) - 1, &bytesRead, NULL);
if (bOk && (bytesRead > 0)) {
buffer[bytesRead] = '\0';
}
When reading from a serial port, ReadFile()
should block until there is more data, or a timeout occurs. (this should be done in a seperate thread (in a loop), or maybe by using ReadFileEx()
for asynchronous operation).
When reading from a communications device, the behavior of ReadFile is governed by the current communication time-outs as set and retrieved using the
SetCommTimeouts
andGetCommTimeouts
functions. Unpredictable results can occur if you fail to set the time-out values.
Also checkout SetCommState()
and PurgeComm()
.
Upvotes: 1