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