-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuart.c
More file actions
133 lines (101 loc) · 4.28 KB
/
uart.c
File metadata and controls
133 lines (101 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
#define buffSize 1000
HANDLE hComm; // Handle to the Serial port
OVERLAPPED o;
void openComPort()
{
BOOL Status; // Status of the various operations
char ComPortName[] = "\\\\.\\COM3"; // Name of the Serial port(May Change) to be opened,
hComm = CreateFile(ComPortName, // Name of the Port to be Opened
GENERIC_READ | GENERIC_WRITE, // Read/Write Access
0, // No Sharing, ports cant be shared
NULL, // No Security
OPEN_EXISTING, // Open existing port only
/*FILE_FLAG_OVERLAPPED*/0,
NULL); // Null for Comm Devices
/*if (hComm == INVALID_HANDLE_VALUE)
printf("\n Error! - Port %s can't be opened\n", ComPortName);
else
printf("\n Port %s Opened\n ", ComPortName);*/
o.hEvent = CreateEvent(
NULL, // default security attributes
TRUE, // manual-reset event
FALSE, // not signaled
NULL // no name
);
// Initialize the rest of the OVERLAPPED structure to zero.
o.Internal = 0;
o.InternalHigh = 0;
o.Offset = 0;
o.OffsetHigh = 0;
/*------------------------------- Setting the Parameters for the SerialPort ------------------------------*/
DCB dcbSerialParams = { 0 }; // Initializing DCB structure
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
Status = GetCommState(hComm, &dcbSerialParams); //retreives the current settings
/*if (Status == FALSE)
printf("\n Error! in GetCommState()");
*/
dcbSerialParams.BaudRate = CBR_115200; // Setting BaudRate = 115200
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT; // Setting StopBits = 1
dcbSerialParams.Parity = NOPARITY; // Setting Parity = None
dcbSerialParams.fDtrControl = 0;
dcbSerialParams.fRtsControl = 0;
Status = SetCommState(hComm, &dcbSerialParams); //Configuring the port according to settings in DCB
SetCommMask(hComm, EV_RXCHAR);
//if (Status == FALSE)
//{
// printf("\n Error! in Setting DCB Structure");
//}
//else //If Successfull display the contents of the DCB Structure
//{
// printf("\n\n Setting DCB Structure Successfull\n");
// printf("\n Baudrate = %d", dcbSerialParams.BaudRate);
// printf("\n ByteSize = %d", dcbSerialParams.ByteSize);
// printf("\n StopBits = %d", dcbSerialParams.StopBits);
// printf("\n Parity = %d", dcbSerialParams.Parity);
//}
/*------------------------------------ Setting Timeouts --------------------------------------------------*/
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
SetCommTimeouts(hComm, &timeouts);
/* printf("\n\n Error! in Setting Time Outs");
else
printf("\n\n Setting Serial Port Timeouts Successfull");*/
/*------------------------------------ Setting Receive Mask ----------------------------------------------*/
Status = SetCommMask(hComm, EV_RXCHAR); //Configure Windows to Monitor the serial device for Character Reception
/*if (Status == FALSE)
printf("\n\n Error! in Setting CommMask");
else
printf("\n\n Setting CommMask successfull\n");*/
}
void comSend(const char* data, int len)
{
char lpBuffer[buffSize]; // lpBuffer should be char or byte array, otherwise write wil fail
DWORD dNoOFBytestoWrite; // No of bytes to write into the port
DWORD dNoOfBytesWritten = 0; // No of bytes written to the port
dNoOFBytestoWrite = min(buffSize, len); // Calculating the no of bytes to write into the port
strncpy(lpBuffer, data, dNoOFBytestoWrite);
BOOL Status = WriteFile(hComm, // Handle to the Serialport
lpBuffer, // Data to be written to the port
dNoOFBytestoWrite, // No of bytes to write into the port
&dNoOfBytesWritten, // No of bytes written to the port
NULL);
}
int comRead(char *buff, int bufSize)
{
int bytesRead;
ReadFile(hComm, buff, bufSize, &bytesRead, 0);
if (bytesRead > 0 && bytesRead < bufSize)
{
buff[bytesRead] = 0;
}
return bytesRead;
}