-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialport.c
133 lines (118 loc) · 3.42 KB
/
serialport.c
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 <stdio.h>
#include <windows.h>
#include "serialport.h"
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
sprintf((LPTSTR)lpDisplayBuf, TEXT("%s failed with error %d:\n%s"), lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
/**
\brief Opens a new connection to a serial port
\param portname name of the serial port(COM1 - COM9 or \\\\.\\COM1-COM256)
\param baudrate the baudrate of this port (for example 9600)
\param stopbits th nuber of stoppbits (one, onePointFive or two)
\param parity the parity (even, odd, off or mark)
\return HANDLE to the serial port
*/
HANDLE openSerialPort(LPCSTR portname,enum Baudrate baudrate, enum Stopbits stopbits, enum Paritycheck parity)
{
int fail = 0;
DWORD accessdirection =GENERIC_READ | GENERIC_WRITE;
HANDLE hSerial = CreateFile(portname,
accessdirection,
0,
0,
OPEN_EXISTING,
0,
0);
if (hSerial == INVALID_HANDLE_VALUE) {
//ErrorExit("CreateFile");
fail = 1;
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams)) {
//ErrorExit("GetCommState");
fail =1;
}
dcbSerialParams.BaudRate=baudrate;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=stopbits;
dcbSerialParams.Parity=parity;
if(!SetCommState(hSerial, &dcbSerialParams)){
//ErrorExit("SetCommState");
fail=1;
}
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;
if(!SetCommTimeouts(hSerial, &timeouts)){
//ErrorExit("SetCommTimeouts");
fail=1;
}
if(fail){
return 0;
}else{
return hSerial;
}
}
/**
\brief Read data from the serial port
\param hSerial File HANDLE to the serial port
\param buffer pointer to the area where the read data will be written
\param buffersize maximal size of the buffer area
\return amount of data that was read
*/
DWORD readFromSerialPort(HANDLE hSerial, char * buffer, int buffersize)
{
DWORD dwBytesRead = 0;
if(!ReadFile(hSerial, buffer, buffersize, &dwBytesRead, NULL)){
//ErrorExit("ReadFile");
return 0;
}else{
return dwBytesRead;
}
}
/**
\brief write data to the serial port
\param hSerial File HANDLE to the serial port
\param buffer pointer to the area where the read data will be read
\param length amount of data to be read
\return amount of data that was written
*/
DWORD writeToSerialPort(HANDLE hSerial, char * data, int length)
{
DWORD dwBytesRead = 0;
if(!WriteFile(hSerial, data, length, &dwBytesRead, NULL)){
//ErrorExit("WriteFile");
return 0;
}else{
return dwBytesRead;
}
}
void closeSerialPort(HANDLE hSerial)
{
CloseHandle(hSerial);
}