0% found this document useful (0 votes)
7 views

uart

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

uart

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdint.h>
#include <windows.h>
#include <time.h>

#define BUFFER_SIZE 256

HANDLE open_port(const char *device, unsigned long baud_rate, unsigned char


bit_size, unsigned char parity)
{
HANDLE port = CreateFileA(device, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (port == INVALID_HANDLE_VALUE)
{
return INVALID_HANDLE_VALUE;
}

BOOL success = FlushFileBuffers(port);


if (!success)
{
printf("Failed to flush serial port");
CloseHandle(port);
return INVALID_HANDLE_VALUE;
}

COMMTIMEOUTS timeouts = {0};


timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 0;

success = SetCommTimeouts(port, &timeouts);


if (!success)
{
printf("Failed to set serial timeouts");
CloseHandle(port);
return INVALID_HANDLE_VALUE;
}

DCB dcb = {0};


dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = baud_rate;
dcb.ByteSize = bit_size;
dcb.Parity = parity;
dcb.StopBits = ONESTOPBIT;
success = SetCommState(port, &dcb);
if (!success)
{
printf("Failed to set serial settings");
CloseHandle(port);
return INVALID_HANDLE_VALUE;
}

return port;
}

int uart_transmit(HANDLE port, unsigned char *TX_buf, unsigned int TX_buf_len)


{
int writtenbytes = 0;
if (WriteFile(port, TX_buf, TX_buf_len, (LPDWORD)&writtenbytes, NULL))
{
if (writtenbytes == 0)
{
printf("WriteFile() timed out\n");
return -1;
}
}
else
{
printf("WriteFile() failed\n");
return -1;
}
return 0;
}

int uart_receive(HANDLE port, unsigned char *RX_buf, unsigned int RX_buf_len)


{
int nbbytes = 0;
if (ReadFile(port, RX_buf, RX_buf_len, (LPDWORD)&nbbytes, NULL))
{
if (nbbytes == 0)
{
printf("ReadFile() timed out\n");
return -1;
}
}
else
{
printf("ReadFile() failed\n");
return -1;
}
return nbbytes;
}

int main()
{
const char *device = "\\\\.\\COM26";
unsigned long baud_rate = 9600;
unsigned char bit_size = 8;
unsigned char parity = 0;

char message[] = "1";


unsigned char RX_buf[BUFFER_SIZE] = {0};
int RX_buf_len = 0;

HANDLE port = open_port(device, baud_rate, bit_size, parity);


if (port == INVALID_HANDLE_VALUE)
{
return -1;
}

// Transmit message
int status = uart_transmit(port, (unsigned char *)message, sizeof(message));
if (status != 0)
{
CloseHandle(port);
return -1;
}

// Receive response
RX_buf_len = uart_receive(port, RX_buf, BUFFER_SIZE);
if (RX_buf_len > 0)
{
RX_buf[RX_buf_len] = '\0';
printf("Received: %s\n", RX_buf);
}

// Close the serial port


CloseHandle(port);
return 0;
}

You might also like