-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
165 lines (136 loc) · 4.81 KB
/
serial.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <Windows.h>
#include <string.h>
#include <conio.h> /*win32-specific library, necessary from reading directly from input instead of from buffered stdin*/
#include "serial.h"
#define BAUDRATE CBR_9600
enum ROBOT_DIR
{
ROBOT_LEFT,
ROBOT_RIGHT,
ROBOT_AHEAD
};
HANDLE hSerial;
void doLeft(void)
{
}
void initSerial(char* comport)
{
/* The following is from the PuTTY source code (c) 1997-2011 Simon Tatham.
* Munge the string supplied by the user into a Windows filename.
*
* Windows supports opening a few "legacy" devices (including
* COM1-9) by specifying their names verbatim as a filename to
* open.
* However, this doesn't let you get at devices COM10 and above.
* For that, you need to specify a filename like "\\.\COM10".
* This is also necessary for special serial and serial-like
* devices such as \\.\WCEUSBSH001. It also works for the "legacy"
* names, so you can do \\.\COM1 (verified as far back as Win95).
* See <http://msdn2.microsoft.com/en-us/library/aa363858.aspx>
* (CreateFile() docs).
* So, we believe that prepending "\\.\" should always be the
* Right Thing. However, just in case someone finds something to
* talk to that doesn't exist under there, if the serial line
* contains a backslash, we use it verbatim. (This also lets
* existing configurations using \\.\ continue working.)
*/
int length = strlen(comport);
char *serfilename = calloc(sizeof (char), length + 5);
snprintf(serfilename, length + 5, "%s%s",
strchr(comport, '\\') ? "" : "\\\\.\\",
comport);
/*----------------------------------------------------------
// Open COMPORT for reading and writing
//----------------------------------------------------------*/
hSerial = CreateFile(serfilename,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hSerial == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
/*serial port does not exist. Inform user.*/
printf(" serial port does not exist \n");
}
/*some other error occurred. Inform user.*/
printf(" some other error occured. Inform user.\n");
exit(1);
}
COMMTIMEOUTS timeouts = {0};
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength = sizeof (dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams))
{
/*error getting state*/
printf("error getting state \n");
}
/*Set ALL the values!
Intializes the parameters as Baudrate, Bytesize,
Stopbits, Parity and Timeoutparameters of
the COM port
*/
dcbSerialParams.BaudRate = BAUDRATE;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if (!SetCommState(hSerial, &dcbSerialParams))
{
/*error setting serial port state*/
printf("error setting state \n");
}
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hSerial, &timeouts))
{
/*error occureed. Inform user*/
printf("error setting timeout state \n");
}
}
/*--------------------------------------------------------------
// Function: readByte
// Description: reads a single byte from the COM port into
// buffer buffRead
//--------------------------------------------------------------*/
int readByte( char* buffRead)
{
DWORD dwBytesRead = 0;
if (!ReadFile(hSerial, buffRead, 1, &dwBytesRead, NULL))
{
printf("error reading byte from input buffer \n Error code %d \n", GetLastError());
}
/* printf("Byte read from read buffer is: %X \n", (uint8_t)buffRead[0]); */
return(0);
}
/*--------------------------------------------------------------
// Function: writeByte
// Description: writes a single byte stored in buffRead to
// the COM port
//--------------------------------------------------------------*/
int writeByte( char *buffWrite)
{
DWORD dwBytesWritten = 0;
if (!WriteFile(hSerial, buffWrite, 1, &dwBytesWritten, NULL))
{
printf("%x\n", GetLastError());
printf("error writing byte to output buffer \n");
}
/* printf("Byte written to write buffer is: 0x%X \n", buffWrite[0]); */
return (0);
}
void cleanUp(void)
{
printf("MEMORY FREED\n");
CloseHandle(hSerial);
printf("ZIGBEE I/O DONE, SERIAL HANDLE CLOSED\n");
}