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