@@ -1,287 +1,285 | |||
|
1 | 1 | #include "rs232config.h" |
|
2 | 2 | #include <stdio.h> |
|
3 | 3 | #include <unistd.h> |
|
4 | 4 | #include <fcntl.h> |
|
5 | 5 | #include <string.h> |
|
6 | 6 | #include <errno.h> |
|
7 | 7 | |
|
8 | 8 | #ifdef HAVE_WINDOWS_H |
|
9 | 9 | #include <windows.h> |
|
10 | 10 | #endif |
|
11 | 11 | #include "RS232.h" |
|
12 | 12 | |
|
13 | 13 | #ifdef HAVE_WINDOWS_H |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | |
|
17 | 17 | |
|
18 | 18 | rs232speed_t rs232cfspeed(unsigned int BaudeRate) |
|
19 | 19 | { |
|
20 | 20 | |
|
21 | 21 | if(BaudeRate<123) |
|
22 | 22 | return CBR_110; |
|
23 | 23 | |
|
24 | 24 | if(BaudeRate<450) |
|
25 | 25 | return CBR_300; |
|
26 | 26 | |
|
27 | 27 | if(BaudeRate<900) |
|
28 | 28 | return CBR_600; |
|
29 | 29 | |
|
30 | 30 | if(BaudeRate<1500) |
|
31 | 31 | return CBR_1200; |
|
32 | 32 | |
|
33 | 33 | if(BaudeRate<3600) |
|
34 | 34 | return CBR_2400; |
|
35 | 35 | |
|
36 | 36 | if(BaudeRate<7200) |
|
37 | 37 | return CBR_4800; |
|
38 | 38 | |
|
39 | 39 | if(BaudeRate<14000) |
|
40 | 40 | return CBR_9600; |
|
41 | 41 | |
|
42 | 42 | if(BaudeRate<16800) |
|
43 | 43 | return CBR_14400; |
|
44 | 44 | |
|
45 | 45 | if(BaudeRate<28800) |
|
46 | 46 | return CBR_19200; |
|
47 | 47 | |
|
48 | 48 | if(BaudeRate<48000) |
|
49 | 49 | return CBR_38400; |
|
50 | 50 | |
|
51 | 51 | if(BaudeRate<86400) |
|
52 | 52 | return CBR_57600; |
|
53 | 53 | |
|
54 | 54 | if(BaudeRate<172800) |
|
55 | 55 | return CBR_115200; |
|
56 | 56 | else |
|
57 | 57 | return CBR_256000; |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | rs232port_t rs232open(char* psPortName) |
|
62 | 62 | { |
|
63 | 63 | rs232port_t fd; |
|
64 | 64 | fd = CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); |
|
65 | 65 | return fd; |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | int rs232close(rs232port_t fd) |
|
69 | 69 | { |
|
70 | 70 | if (fd == badPortValue) |
|
71 | 71 | { |
|
72 | 72 | return -1; |
|
73 | 73 | } |
|
74 | 74 | else |
|
75 | 75 | { |
|
76 | 76 | CloseHandle(fd); |
|
77 | 77 | return 0; |
|
78 | 78 | } |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | 81 | int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) |
|
82 | 82 | { |
|
83 | 83 | if (fd == badPortValue) |
|
84 | 84 | { |
|
85 | 85 | return -1; |
|
86 | 86 | } |
|
87 | 87 | else |
|
88 | 88 | { |
|
89 | 89 | DCB dcbSerialParams = {0}; |
|
90 | 90 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
91 | 91 | GetCommState(fd, &dcbSerialParams); |
|
92 | 92 | dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate); |
|
93 | 93 | dcbSerialParams.ByteSize=ChSize; |
|
94 | 94 | dcbSerialParams.StopBits=NbStop; |
|
95 | 95 | dcbSerialParams.Parity=Parity; |
|
96 | 96 | SetCommState(fd, &dcbSerialParams); |
|
97 | 97 | COMMTIMEOUTS timeouts={0}; |
|
98 | timeouts.ReadIntervalTimeout=10; | |
|
99 | timeouts.ReadTotalTimeoutConstant=10; | |
|
98 | timeouts.ReadIntervalTimeout=100; | |
|
99 | timeouts.ReadTotalTimeoutConstant=100; | |
|
100 | 100 | timeouts.ReadTotalTimeoutMultiplier=1; |
|
101 |
timeouts.WriteTotalTimeoutConstant= |
|
|
101 | timeouts.WriteTotalTimeoutConstant=100; | |
|
102 | 102 | timeouts.WriteTotalTimeoutMultiplier=10; |
|
103 | 103 | SetCommTimeouts(fd, &timeouts); |
|
104 | 104 | return 0; |
|
105 | 105 | } |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | int rs232setbaudrate(rs232port_t fd, int baudrate) |
|
109 | 109 | { |
|
110 | 110 | if (fd == badPortValue) |
|
111 | 111 | { |
|
112 | 112 | return -1; |
|
113 | 113 | } |
|
114 | 114 | else |
|
115 | 115 | { |
|
116 | 116 | DCB dcbSerialParams = {0}; |
|
117 | 117 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
118 | 118 | GetCommState(fd, &dcbSerialParams); |
|
119 | 119 | dcbSerialParams.BaudRate=rs232cfspeed(baudrate); |
|
120 | 120 | SetCommState(fd, &dcbSerialParams); |
|
121 | 121 | return 0; |
|
122 | 122 | } |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | int rs232setparity(rs232port_t fd, rs232parity Parity) |
|
126 | 126 | { |
|
127 | 127 | if (fd == badPortValue) |
|
128 | 128 | { |
|
129 | 129 | return -1; |
|
130 | 130 | } |
|
131 | 131 | else |
|
132 | 132 | { |
|
133 | 133 | DCB dcbSerialParams = {0}; |
|
134 | 134 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
135 | 135 | GetCommState(fd, &dcbSerialParams); |
|
136 | 136 | dcbSerialParams.Parity = Parity; |
|
137 | 137 | SetCommState(fd, &dcbSerialParams); |
|
138 | 138 | return 0; |
|
139 | 139 | } |
|
140 | 140 | } |
|
141 | 141 | |
|
142 | 142 | int rs232setnbstop(rs232port_t fd, rs232stop NbStop) |
|
143 | 143 | { |
|
144 | 144 | if (fd == badPortValue) |
|
145 | 145 | { |
|
146 | 146 | return -1; |
|
147 | 147 | } |
|
148 | 148 | else |
|
149 | 149 | { |
|
150 | 150 | DCB dcbSerialParams = {0}; |
|
151 | 151 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
152 | 152 | GetCommState(fd, &dcbSerialParams); |
|
153 | 153 | switch(NbStop) |
|
154 | 154 | { |
|
155 | 155 | case 2: |
|
156 | 156 | dcbSerialParams.StopBits = 2; |
|
157 | 157 | break; |
|
158 | 158 | default: |
|
159 | 159 | dcbSerialParams.StopBits = 1; |
|
160 | 160 | break; |
|
161 | 161 | } |
|
162 | 162 | SetCommState(fd, &dcbSerialParams); |
|
163 | 163 | return 0; |
|
164 | 164 | } |
|
165 | 165 | } |
|
166 | 166 | |
|
167 | 167 | |
|
168 | 168 | int rs232setcsize(rs232port_t fd, int ChSize) |
|
169 | 169 | { |
|
170 | 170 | if (fd == badPortValue) |
|
171 | 171 | { |
|
172 | 172 | return -1; |
|
173 | 173 | } |
|
174 | 174 | else |
|
175 | 175 | { |
|
176 | 176 | DCB dcbSerialParams = {0}; |
|
177 | 177 | dcbSerialParams.DCBlength=sizeof(dcbSerialParams); |
|
178 | 178 | GetCommState(fd, &dcbSerialParams); |
|
179 | 179 | dcbSerialParams.ByteSize = ChSize; |
|
180 | 180 | SetCommState(fd, &dcbSerialParams); |
|
181 | 181 | return 0; |
|
182 | 182 | } |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | |
|
186 | 186 | |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | |
|
190 | 190 | int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) |
|
191 | 191 | { |
|
192 | 192 | |
|
193 | 193 | if (fd == badPortValue) |
|
194 | 194 | { |
|
195 | 195 | return -1; |
|
196 | 196 | } |
|
197 | 197 | else |
|
198 | 198 | { |
|
199 | 199 | DWORD dwBytesWriten = 0; |
|
200 | 200 | WriteFile(fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL); |
|
201 | 201 | return dwBytesWriten; |
|
202 | 202 | } |
|
203 | 203 | } |
|
204 | 204 | |
|
205 | 205 | |
|
206 | 206 | int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize) |
|
207 | 207 | { |
|
208 | 208 | |
|
209 | 209 | if (fd == badPortValue) |
|
210 | 210 | { |
|
211 | 211 | return -1; |
|
212 | 212 | } |
|
213 | 213 | else |
|
214 | 214 | { |
|
215 | 215 | DWORD dwBytesRead = 0; |
|
216 | 216 | ReadFile(fd, psRead, ReadBufferSize, &dwBytesRead, NULL); |
|
217 | 217 | return dwBytesRead; |
|
218 | 218 | } |
|
219 | 219 | |
|
220 | 220 | } |
|
221 | 221 | |
|
222 | 222 | |
|
223 | 223 | int rs232saferead(rs232port_t fd,char* data,int count ) |
|
224 | 224 | { |
|
225 | 225 | int read=0; |
|
226 | 226 | int i=0; |
|
227 | 227 | for(i=0;i<100;i++) |
|
228 | 228 | { |
|
229 | 229 | read = rs232read(fd,data,count); |
|
230 | 230 | count -=read; |
|
231 | 231 | data+=read; |
|
232 | 232 | if(count==0) |
|
233 | 233 | return 0; |
|
234 | 234 | } |
|
235 | 235 | return -1; |
|
236 | 236 | } |
|
237 | 237 | |
|
238 | 238 | |
|
239 | 239 | |
|
240 | 240 | int rs232safewrite(rs232port_t fd,char* data,int count) |
|
241 | 241 | { |
|
242 | 242 | int written=0; |
|
243 | 243 | int i=0; |
|
244 | 244 | for(i=0;i<1000;i++) |
|
245 | 245 | { |
|
246 | 246 | written = rs232write(fd,data+written,count); |
|
247 | 247 | count-=written; |
|
248 | 248 | data+=written; |
|
249 | 249 | if(count==0) |
|
250 | 250 | return 0; |
|
251 | 251 | } |
|
252 | 252 | return -1; |
|
253 | 253 | } |
|
254 | 254 | |
|
255 | 255 | |
|
256 | 256 | int rs232setRTS(rs232port_t fd) |
|
257 | 257 | { |
|
258 |
if(EscapeCommFunction(fd, |
|
|
258 | if(EscapeCommFunction(fd, CLRRTS)) | |
|
259 | 259 | return 0; |
|
260 | 260 | return -1; |
|
261 | 261 | } |
|
262 | 262 | |
|
263 | 263 | int rs232clearRTS(rs232port_t fd) |
|
264 | 264 | { |
|
265 |
if(EscapeCommFunction(fd, |
|
|
265 | if(EscapeCommFunction(fd, SETRTS)) | |
|
266 | 266 | return 0; |
|
267 | 267 | return -1; |
|
268 | 268 | } |
|
269 | 269 | |
|
270 | 270 | int rs232setDTR(rs232port_t fd) |
|
271 | 271 | { |
|
272 |
if(EscapeCommFunction(fd, |
|
|
272 | if(EscapeCommFunction(fd, CLRDTR)) | |
|
273 | 273 | return 0; |
|
274 | 274 | return -1; |
|
275 | 275 | } |
|
276 | 276 | |
|
277 | 277 | |
|
278 | 278 | int rs232clearDTR(rs232port_t fd) |
|
279 | 279 | { |
|
280 |
if(EscapeCommFunction(fd, |
|
|
280 | if(EscapeCommFunction(fd, SETDTR)) | |
|
281 | 281 | return 0; |
|
282 | 282 | return -1; |
|
283 | 283 | } |
|
284 | 284 | |
|
285 | ||
|
286 | ||
|
287 | 285 | #endif //#ifdef HAVE_WINDOWS_H |
General Comments 0
You need to be logged in to leave comments.
Login now