@@ -0,0 +1,18 | |||||
|
1 | ||||
|
2 | TEMPLATE = lib | |||
|
3 | CONFIG -= qt | |||
|
4 | CONFIG += static | |||
|
5 | ||||
|
6 | HEADERS += \ | |||
|
7 | src/RS232.h | |||
|
8 | ||||
|
9 | win32:SOURCES += src/RS232_win.c | |||
|
10 | ||||
|
11 | unix:SOURCES += src/RS232_unix.c | |||
|
12 | ||||
|
13 | debug:DEFINES+=RS232_debug | |||
|
14 | ||||
|
15 | DESTDIR=lib | |||
|
16 | ||||
|
17 | OBJECTS_DIR = obj | |||
|
18 |
@@ -1,7 +1,6 | |||||
1 | // SOPSUYSI_RS232.h |
|
1 | // SOPSUYSI_RS232.h | |
2 | #ifndef RS232_H |
|
2 | #ifndef RS232_H | |
3 | #define RS232_H |
|
3 | #define RS232_H | |
4 | #define RS232_debug |
|
|||
5 |
|
4 | |||
6 | #define badPortValue -1 |
|
5 | #define badPortValue -1 | |
7 | #define rs232noerr 0 |
|
6 | #define rs232noerr 0 |
This diff has been collapsed as it changes many lines, (862 lines changed) Show them Hide them | |||||
@@ -8,551 +8,541 | |||||
8 | #include <limits.h> |
|
8 | #include <limits.h> | |
9 | #include <stdlib.h> |
|
9 | #include <stdlib.h> | |
10 | #include <sys/stat.h> |
|
10 | #include <sys/stat.h> | |
11 | #include "rs232config.h" |
|
|||
12 |
|
11 | |||
13 | #ifdef HAVE_TERMIOS_H |
|
12 | #include <termios.h> | |
14 |
|
|
13 | #include <termio.h> | |
15 | #endif |
|
|||
16 | #ifdef HAVE_TERMIO_H |
|
|||
17 | #include <termio.h> |
|
|||
18 | #endif |
|
|||
19 | #include "RS232.h" |
|
14 | #include "RS232.h" | |
20 |
|
15 | |||
21 | #ifdef HAVE_WINDOWS_H |
|
|||
22 | #else |
|
|||
23 | #ifdef HAVE_TERMIOS_H |
|
|||
24 | rs232speed_t rs232cfspeed(unsigned int BaudeRate); |
|
16 | rs232speed_t rs232cfspeed(unsigned int BaudeRate); | |
25 |
|
17 | |||
26 | rs232port_t rs232open(char* psPortName) |
|
18 | rs232port_t rs232open(char* psPortName) | |
27 | { |
|
19 | { | |
28 | rs232port_t fd; |
|
20 | rs232port_t fd; | |
29 | int flags; |
|
21 | int flags; | |
30 | fd = (rs232port_t)open(psPortName, O_RDWR | O_NOCTTY );//| O_NDELAY); |
|
22 | fd = (rs232port_t)open(psPortName, O_RDWR | O_NOCTTY );//| O_NDELAY); | |
31 | //fcntl((int)fd, F_SETFL, FNDELAY); |
|
23 | //fcntl((int)fd, F_SETFL, FNDELAY); | |
32 | //fd = open(psPortName, O_RDWR | O_NOCTTY); |
|
24 | //fd = open(psPortName, O_RDWR | O_NOCTTY); | |
33 |
|
|
25 | #ifdef RS232_debug | |
34 |
|
|
26 | if(fd==-1)printf("can't open Port\n"); | |
35 |
|
|
27 | #else | |
36 |
|
|
28 | if(fd!=-1) | |
37 |
|
|
29 | { | |
38 |
|
|
30 | //flags = fcntl(fd, F_GETFL); | |
39 |
|
|
31 | //flags |= O_NONBLOCK; | |
40 |
|
|
32 | //flags|=FNDELAY; | |
41 |
|
|
33 | //fcntl(fd, F_SETFL, flags); | |
42 |
|
|
34 | //fcntl((int)fd, F_SETFL, FNDELAY); | |
43 |
|
|
35 | } | |
44 |
|
36 | |||
45 |
|
|
37 | #endif | |
46 | return fd; |
|
38 | return fd; | |
47 | } |
|
39 | } | |
48 |
|
40 | |||
49 | int rs232close(rs232port_t fd) |
|
41 | int rs232close(rs232port_t fd) | |
50 | { |
|
42 | { | |
51 |
|
|
43 | if ((int)fd == -1) | |
52 | { |
|
44 | { | |
53 |
|
|
45 | return -1; | |
54 | } |
|
46 | } | |
55 |
|
|
47 | else | |
56 | { |
|
48 | { | |
57 |
|
|
49 | close(fd); | |
58 |
|
|
50 | return 0; | |
59 | } |
|
51 | } | |
60 | } |
|
52 | } | |
61 |
|
53 | |||
62 | rs232portslist_t* rs232getportlist() |
|
54 | rs232portslist_t* rs232getportlist() | |
63 | { |
|
55 | { | |
64 |
|
|
56 | struct dirent *dp; | |
65 |
|
|
57 | char* path="/dev/serial/by-id"; | |
66 |
|
|
58 | DIR *dir = opendir(path); | |
67 |
|
|
59 | rs232portslist_t* firstitem=NULL; | |
68 |
|
|
60 | rs232portslist_t* previtem=NULL; | |
69 |
|
|
61 | char linkname[1024]; | |
70 |
|
|
62 | int i=0; | |
71 |
|
|
63 | if(dir!=NULL) | |
72 | { |
|
64 | { | |
73 |
|
|
65 | while ((dp=readdir(dir)) != NULL) | |
74 | { |
|
66 | { | |
75 |
|
|
67 | char* name = (char*)malloc(1024); | |
76 |
|
|
68 | for(i=0;i<1024;i++) | |
77 | { |
|
69 | { | |
78 | name[i]='\0'; |
|
70 | name[i]='\0'; | |
79 |
|
|
71 | linkname[i]='\0'; | |
80 | } |
|
72 | } | |
81 |
|
|
73 | struct stat statbuf; | |
82 |
|
|
74 | strcpy(linkname,path); | |
83 |
|
|
75 | strcat(linkname,"/"); | |
84 |
|
|
76 | strcat(linkname,dp->d_name); | |
85 |
|
|
77 | lstat(linkname, &statbuf); | |
86 |
|
|
78 | if(S_ISLNK(statbuf.st_mode)) | |
87 | { |
|
79 | { | |
88 |
|
|
80 | if(-1!=readlink(linkname,name,1024)) | |
89 | { |
|
81 | { | |
90 |
|
|
82 | for(i=0;i<1024;i++) | |
91 | { |
|
83 | { | |
92 | linkname[i]='\0'; |
|
84 | linkname[i]='\0'; | |
93 | } |
|
85 | } | |
94 |
|
|
86 | strcpy(linkname,path); | |
95 |
|
|
87 | strcat(linkname,"/"); | |
96 |
|
|
88 | strcat(linkname,name); | |
97 |
|
|
89 | for(i=0;i<1024;i++) | |
98 | { |
|
90 | { | |
99 | name[i]='\0'; |
|
91 | name[i]='\0'; | |
100 | } |
|
92 | } | |
101 |
|
|
93 | if(NULL!=realpath(linkname, name)) | |
102 | { |
|
94 | { | |
103 |
|
|
95 | rs232portslist_t* item = (rs232portslist_t*)malloc(sizeof(rs232portslist_t)); | |
104 | item->name = name; |
|
96 | item->name = name; | |
105 | item->next = NULL; |
|
97 | item->next = NULL; | |
106 |
|
|
98 | if(NULL!=previtem)previtem->next = item; | |
107 | previtem = item; |
|
99 | previtem = item; | |
108 |
|
|
100 | if(NULL==firstitem)firstitem = item; | |
109 | } |
|
101 | } | |
110 |
|
||||
111 | } |
|
|||
112 | } |
|
|||
113 |
|
102 | |||
114 | } |
|
103 | } | |
115 | } |
|
104 | } | |
116 | return firstitem; |
|
105 | ||
|
106 | } | |||
|
107 | } | |||
|
108 | return firstitem; | |||
117 | } |
|
109 | } | |
118 |
|
110 | |||
119 | void rs232deleteportlist(rs232portslist_t* list) |
|
111 | void rs232deleteportlist(rs232portslist_t* list) | |
120 | { |
|
112 | { | |
121 |
|
|
113 | if(list!=NULL) | |
122 | { |
|
114 | { | |
123 |
|
|
115 | if(list->next != NULL) | |
124 |
|
|
116 | rs232deleteportlist(list->next); | |
125 |
|
|
117 | free(list); | |
126 | } |
|
118 | } | |
127 | } |
|
119 | } | |
128 |
|
120 | |||
129 | int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) |
|
121 | int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) | |
130 | { |
|
122 | { | |
131 | if ((int)fd == -1) |
|
123 | if ((int)fd == -1) | |
132 | { |
|
124 | { | |
133 |
|
|
125 | return -1; | |
134 | } |
|
126 | } | |
135 |
|
|
127 | else | |
136 | { |
|
128 | { | |
137 |
|
|
129 | struct termios terminos; | |
138 |
|
|
130 | tcgetattr(fd, &terminos); | |
139 |
|
|
131 | terminos.c_iflag=0; | |
140 |
|
|
132 | terminos.c_oflag=0; | |
141 |
|
|
133 | terminos.c_cflag=0; | |
142 |
|
|
134 | terminos.c_lflag=0; | |
143 |
|
|
135 | cfsetispeed(&terminos, rs232cfspeed(BaudeRate)); | |
144 |
|
|
136 | cfsetospeed(&terminos, rs232cfspeed(BaudeRate)); | |
145 |
|
|
137 | terminos.c_cflag |= (CLOCAL | CREAD); | |
146 |
|
|
138 | rs232cfparity((int)fd, &terminos, Parity); | |
147 |
|
|
139 | rs232cfnbstop((int)fd, &terminos, NbStop); | |
148 |
|
|
140 | rs232cfcsize((int)fd, &terminos, ChSize); | |
149 |
|
|
141 | terminos.c_cc[VMIN]=0; | |
150 |
|
|
142 | terminos.c_cc[VTIME]=1; | |
151 |
|
|
143 | tcflush(fd, TCIFLUSH); | |
152 |
|
|
144 | #ifdef RS232_debug | |
153 |
|
|
145 | if(tcsetattr(fd, TCSANOW, &terminos)!=0)printf("bad setup\n"); | |
154 |
|
|
146 | #else | |
155 |
|
|
147 | tcsetattr(fd, TCSANOW, &terminos); | |
156 |
|
|
148 | #endif | |
157 |
|
|
149 | return 0; | |
158 | } |
|
150 | } | |
159 | } |
|
151 | } | |
160 |
|
152 | |||
161 | int rs232setbaudrate(rs232port_t fd, int baudrate) |
|
153 | int rs232setbaudrate(rs232port_t fd, int baudrate) | |
162 | { |
|
154 | { | |
163 |
|
|
155 | if ((int)fd == -1) | |
164 | { |
|
156 | { | |
165 |
|
|
157 | return fd; | |
166 | } |
|
158 | } | |
167 |
|
|
159 | else | |
168 | { |
|
160 | { | |
169 |
|
|
161 | struct termios terminos; | |
170 |
|
|
162 | tcgetattr((int)fd, &terminos); | |
171 |
|
|
163 | cfsetispeed(&terminos, rs232cfspeed(baudrate)); | |
172 |
|
|
164 | cfsetospeed(&terminos, rs232cfspeed(baudrate)); | |
173 |
|
|
165 | tcsetattr((int)fd, TCSANOW, &terminos); | |
174 |
|
|
166 | return 0; | |
175 | } |
|
167 | } | |
176 | } |
|
168 | } | |
177 |
|
169 | |||
178 | int rs232setparity(rs232port_t fd, rs232parity Parity) |
|
170 | int rs232setparity(rs232port_t fd, rs232parity Parity) | |
179 | { |
|
171 | { | |
180 |
|
|
172 | if ((int)fd == -1) | |
181 | { |
|
173 | { | |
182 |
|
|
174 | return fd; | |
183 | } |
|
175 | } | |
184 |
|
|
176 | else | |
185 | { |
|
177 | { | |
186 |
|
|
178 | struct termios terminos; | |
187 |
|
|
179 | tcgetattr((int)fd, &terminos); | |
188 |
|
|
180 | terminos.c_cflag &= ~PARENB; | |
189 |
|
|
181 | terminos.c_cflag &= ~PARODD; | |
190 |
|
|
182 | switch(Parity) | |
191 | { |
|
183 | { | |
192 |
|
|
184 | case rs232parityNo: | |
193 |
|
|
185 | terminos.c_cflag &= ~PARENB; | |
194 | break; |
|
186 | break; | |
195 |
|
|
187 | case rs232parityOdd: | |
196 |
|
|
188 | terminos.c_cflag |= PARENB; | |
197 |
|
|
189 | terminos.c_cflag |= PARODD; | |
198 | break; |
|
190 | break; | |
199 |
|
|
191 | case rs232parityEven: | |
200 |
|
|
192 | terminos.c_cflag |= PARENB; | |
201 |
|
|
193 | terminos.c_cflag &= ~PARODD; | |
202 | break; |
|
194 | break; | |
203 | default: |
|
195 | default: | |
204 |
|
|
196 | terminos.c_cflag &= ~PARENB; | |
205 | break; |
|
197 | break; | |
206 | } |
|
198 | } | |
207 |
|
|
199 | tcsetattr((int)fd, TCSANOW, &terminos); | |
208 |
|
|
200 | return 0; | |
209 | } |
|
201 | } | |
210 | } |
|
202 | } | |
211 |
|
203 | |||
212 | int rs232setnbstop(rs232port_t fd, rs232stop NbStop) |
|
204 | int rs232setnbstop(rs232port_t fd, rs232stop NbStop) | |
213 | { |
|
205 | { | |
214 |
|
|
206 | if ((int)fd == -1) | |
215 | { |
|
207 | { | |
216 |
|
|
208 | return fd; | |
217 | } |
|
209 | } | |
218 |
|
|
210 | else | |
219 | { |
|
211 | { | |
220 |
|
|
212 | struct termios terminos; | |
221 |
|
|
213 | tcgetattr((int)fd, &terminos); | |
222 |
|
|
214 | switch(NbStop) | |
223 | { |
|
215 | { | |
224 |
|
|
216 | case rs232OneStop: | |
225 |
|
|
217 | terminos.c_cflag &= ~CSTOPB; | |
226 |
|
|
218 | break; | |
227 |
|
|
219 | case rs232One5Stop: | |
228 |
|
|
220 | terminos.c_cflag |= CSTOPB; | |
229 |
|
|
221 | break; | |
230 |
|
|
222 | case rs232TwoStop: | |
231 |
|
|
223 | terminos.c_cflag |= CSTOPB; | |
232 |
|
|
224 | break; | |
233 | default: |
|
225 | default: | |
234 |
|
|
226 | terminos.c_cflag &= ~CSTOPB; | |
235 |
|
|
227 | break; | |
236 | } |
|
228 | } | |
237 |
|
|
229 | tcsetattr((int)fd, TCSANOW, &terminos); | |
238 |
|
|
230 | return 0; | |
239 | } |
|
231 | } | |
240 | } |
|
232 | } | |
241 |
|
233 | |||
242 |
|
234 | |||
243 | int rs232setcsize(rs232port_t fd, int ChSize) |
|
235 | int rs232setcsize(rs232port_t fd, int ChSize) | |
244 | { |
|
236 | { | |
245 |
|
|
237 | if ((int)fd == -1) | |
246 | { |
|
238 | { | |
247 |
|
|
239 | return fd; | |
248 | } |
|
240 | } | |
249 |
|
|
241 | else | |
250 | { |
|
242 | { | |
251 |
|
|
243 | struct termios terminos; | |
252 |
|
|
244 | tcgetattr((int)fd, &terminos); | |
253 |
|
|
245 | terminos.c_cflag &= ~CSIZE; | |
254 |
|
|
246 | switch(ChSize) | |
255 | { |
|
247 | { | |
256 | case 5: |
|
248 | case 5: | |
257 |
|
|
249 | terminos.c_cflag |= CS5; | |
258 | break; |
|
250 | break; | |
259 | case 6: |
|
251 | case 6: | |
260 |
|
|
252 | terminos.c_cflag |= CS6; | |
261 | break; |
|
253 | break; | |
262 | case 7: |
|
254 | case 7: | |
263 |
|
|
255 | terminos.c_cflag |= CS7; | |
264 | break; |
|
256 | break; | |
265 | default: |
|
257 | default: | |
266 |
|
|
258 | terminos.c_cflag |= CS8; | |
267 | break; |
|
259 | break; | |
268 | } |
|
260 | } | |
269 |
|
|
261 | tcsetattr((int)fd, TCSANOW, &terminos); | |
270 |
|
|
262 | return 0; | |
271 | } |
|
263 | } | |
272 | } |
|
264 | } | |
273 |
|
265 | |||
274 | rs232speed_t rs232cfspeed(unsigned int BaudeRate) |
|
266 | rs232speed_t rs232cfspeed(unsigned int BaudeRate) | |
275 | { |
|
267 | { | |
276 |
|
|
268 | if(BaudeRate<25) | |
277 |
|
|
269 | return B0; | |
|
270 | ||||
|
271 | if(BaudeRate<67) | |||
|
272 | return B50; | |||
|
273 | ||||
|
274 | if(BaudeRate<93) | |||
|
275 | return B75; | |||
|
276 | ||||
|
277 | if(BaudeRate<123) | |||
|
278 | return B110; | |||
|
279 | ||||
|
280 | if(BaudeRate<142) | |||
|
281 | return B134; | |||
|
282 | ||||
|
283 | if(BaudeRate<175) | |||
|
284 | return B150; | |||
|
285 | ||||
|
286 | if(BaudeRate<250) | |||
|
287 | return B200; | |||
|
288 | ||||
|
289 | if(BaudeRate<450) | |||
|
290 | return B300; | |||
|
291 | ||||
|
292 | if(BaudeRate<900) | |||
|
293 | return B600; | |||
|
294 | ||||
|
295 | if(BaudeRate<1500) | |||
|
296 | return B1200; | |||
|
297 | ||||
|
298 | if(BaudeRate<2100) | |||
|
299 | return B1800; | |||
|
300 | ||||
|
301 | if(BaudeRate<3600) | |||
|
302 | return B2400; | |||
278 |
|
303 | |||
279 |
|
|
304 | if(BaudeRate<7200) | |
280 | return B50; |
|
305 | return B4800; | |
281 |
|
306 | |||
282 |
|
|
307 | if(BaudeRate<1400) | |
283 | return B75; |
|
308 | return B9600; | |
284 |
|
309 | |||
285 |
|
|
310 | if(BaudeRate<28800) | |
286 |
|
|
311 | return B19200; | |
287 |
|
312 | |||
288 |
|
|
313 | if(BaudeRate<48000) | |
289 |
|
|
314 | return B38400; | |
290 |
|
315 | |||
291 |
|
|
316 | if(BaudeRate<86400) | |
292 |
|
|
317 | return B57600; | |
293 |
|
318 | |||
294 |
|
|
319 | if(BaudeRate<172800) | |
295 |
|
|
320 | return B115200; | |
296 |
|
||||
297 | if(BaudeRate<450) |
|
|||
298 | return B300; |
|
|||
299 |
|
||||
300 | if(BaudeRate<900) |
|
|||
301 | return B600; |
|
|||
302 |
|
||||
303 | if(BaudeRate<1500) |
|
|||
304 | return B1200; |
|
|||
305 |
|
||||
306 | if(BaudeRate<2100) |
|
|||
307 | return B1800; |
|
|||
308 |
|
||||
309 | if(BaudeRate<3600) |
|
|||
310 | return B2400; |
|
|||
311 |
|
||||
312 | if(BaudeRate<7200) |
|
|||
313 | return B4800; |
|
|||
314 |
|
||||
315 | if(BaudeRate<1400) |
|
|||
316 | return B9600; |
|
|||
317 |
|
||||
318 | if(BaudeRate<28800) |
|
|||
319 | return B19200; |
|
|||
320 |
|
||||
321 | if(BaudeRate<48000) |
|
|||
322 | return B38400; |
|
|||
323 |
|
||||
324 | if(BaudeRate<86400) |
|
|||
325 | return B57600; |
|
|||
326 |
|
||||
327 | if(BaudeRate<172800) |
|
|||
328 | return B115200; |
|
|||
329 |
|
321 | |||
330 |
|
|
322 | if(BaudeRate<345600) | |
331 |
|
|
323 | return B230400; | |
332 |
|
324 | |||
333 |
|
|
325 | if(BaudeRate<345600) | |
334 |
|
|
326 | return B460800; | |
335 |
|
327 | |||
336 |
|
|
328 | if(BaudeRate<748800) | |
337 |
|
|
329 | return B576000; | |
338 |
|
330 | |||
339 |
|
|
331 | if(BaudeRate<1210800) | |
340 |
|
|
332 | return B921600; | |
341 |
|
333 | |||
342 |
|
|
334 | if(BaudeRate<1750000) | |
343 |
|
|
335 | return B1500000; | |
344 |
|
336 | |||
345 |
|
|
337 | if(BaudeRate<2250000) | |
346 |
|
|
338 | return B2000000; | |
347 |
|
339 | |||
348 |
|
|
340 | if(BaudeRate<2750000) | |
349 |
|
|
341 | return B2500000; | |
350 |
|
|
342 | else | |
351 |
|
|
343 | return B3000000; | |
352 |
|
344 | |||
353 | } |
|
345 | } | |
354 |
|
346 | |||
355 |
|
347 | |||
356 | int rs232cfparity(int fd, struct termios *terminos, rs232parity Parity) |
|
348 | int rs232cfparity(int fd, struct termios *terminos, rs232parity Parity) | |
357 | { |
|
349 | { | |
358 |
|
|
350 | if ((int)fd == -1) | |
359 | { |
|
351 | { | |
360 |
|
|
352 | return fd; | |
361 | } |
|
353 | } | |
362 |
|
|
354 | else | |
363 | { |
|
355 | { | |
364 |
|
|
356 | terminos->c_cflag &= ~PARENB; | |
365 |
|
|
357 | terminos->c_cflag &= ~PARODD; | |
366 |
|
|
358 | switch(Parity) | |
367 | { |
|
359 | { | |
368 |
|
|
360 | case rs232parityNo: | |
369 |
|
|
361 | terminos->c_cflag &= ~PARENB; | |
370 |
|
|
362 | terminos->c_cflag &= ~PARODD; | |
371 | break; |
|
363 | break; | |
372 |
|
|
364 | case rs232parityOdd: | |
373 |
|
|
365 | terminos->c_cflag |= PARENB; | |
374 |
|
|
366 | terminos->c_cflag |= PARODD; | |
375 | break; |
|
367 | break; | |
376 |
|
|
368 | case rs232parityEven: | |
377 |
|
|
369 | terminos->c_cflag |= PARENB; | |
378 |
|
|
370 | terminos->c_cflag &= ~PARODD; | |
379 | break; |
|
371 | break; | |
380 | default: |
|
372 | default: | |
381 |
|
|
373 | terminos->c_cflag &= ~PARENB; | |
382 |
|
|
374 | terminos->c_cflag &= ~PARODD; | |
383 | break; |
|
375 | break; | |
384 | } |
|
376 | } | |
385 |
|
|
377 | return 0; | |
386 | } |
|
378 | } | |
387 | } |
|
379 | } | |
388 |
|
380 | |||
389 | int rs232cfnbstop(int fd, struct termios *terminos, rs232stop NbStop) |
|
381 | int rs232cfnbstop(int fd, struct termios *terminos, rs232stop NbStop) | |
390 | { |
|
382 | { | |
391 |
|
|
383 | if ((int)fd == -1) | |
392 | { |
|
384 | { | |
393 |
|
|
385 | return fd; | |
394 | } |
|
386 | } | |
395 |
|
|
387 | else | |
396 | { |
|
388 | { | |
397 |
|
|
389 | switch(NbStop) | |
398 | { |
|
390 | { | |
399 | case 2: |
|
391 | case 2: | |
400 |
|
|
392 | terminos->c_cflag |= CSTOPB; | |
401 | break; |
|
393 | break; | |
402 | default: |
|
394 | default: | |
403 |
|
|
395 | terminos->c_cflag &= ~CSTOPB; | |
404 | break; |
|
396 | break; | |
405 | } |
|
397 | } | |
406 |
|
|
398 | return 0; | |
407 | } |
|
399 | } | |
408 | } |
|
400 | } | |
409 |
|
401 | |||
410 |
|
402 | |||
411 | int rs232cfcsize(int fd, struct termios *terminos, int ChSize) |
|
403 | int rs232cfcsize(int fd, struct termios *terminos, int ChSize) | |
412 | { |
|
404 | { | |
413 |
|
|
405 | if ((int)fd == -1) | |
414 | { |
|
406 | { | |
415 |
|
|
407 | return fd; | |
416 | } |
|
408 | } | |
417 |
|
|
409 | else | |
418 | { |
|
410 | { | |
419 |
|
|
411 | terminos->c_cflag &= ~CSIZE; | |
420 |
|
|
412 | switch(ChSize) | |
421 | { |
|
413 | { | |
422 | case 5: |
|
414 | case 5: | |
423 |
|
|
415 | terminos->c_cflag |= CS5; | |
424 | break; |
|
416 | break; | |
425 | case 6: |
|
417 | case 6: | |
426 |
|
|
418 | terminos->c_cflag |= CS6; | |
427 | break; |
|
419 | break; | |
428 | case 7: |
|
420 | case 7: | |
429 |
|
|
421 | terminos->c_cflag |= CS7; | |
430 | break; |
|
422 | break; | |
431 | default: |
|
423 | default: | |
432 |
|
|
424 | terminos->c_cflag |= CS8; | |
433 | break; |
|
425 | break; | |
434 | } |
|
426 | } | |
435 |
|
|
427 | return 0; | |
436 | } |
|
428 | } | |
437 | } |
|
429 | } | |
438 |
|
430 | |||
439 |
|
431 | |||
440 | int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) |
|
432 | int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) | |
441 | { |
|
433 | { | |
442 |
|
|
434 | if ((int)fd == -1) | |
443 | { |
|
435 | { | |
444 |
|
|
436 | return -1; | |
445 | } |
|
437 | } | |
446 |
|
|
438 | else | |
447 | { |
|
439 | { | |
448 |
|
|
440 | return write((int)fd, psWrite, WriteBufferSize); | |
449 | } |
|
441 | } | |
450 | } |
|
442 | } | |
451 |
|
443 | |||
452 |
|
444 | |||
453 | int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize) |
|
445 | int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize) | |
454 | { |
|
446 | { | |
455 |
|
447 | |||
456 |
|
|
448 | if ((int)fd == -1) | |
457 | { |
|
449 | { | |
458 |
|
|
450 | return -1; | |
459 | } |
|
451 | } | |
460 |
|
|
452 | else | |
461 | { |
|
453 | { | |
462 |
|
|
454 | return read((int)fd, psReadHex, ReadBufferSize); | |
463 | } |
|
455 | } | |
464 |
|
456 | |||
465 | } |
|
457 | } | |
466 |
|
458 | |||
467 |
|
459 | |||
468 | int rs232setRTS(rs232port_t fd) |
|
460 | int rs232setRTS(rs232port_t fd) | |
469 | { |
|
461 | { | |
470 |
int status; |
|
462 | int status; | |
471 | ioctl((int)fd, TIOCMGET, &status); |
|
463 | ioctl((int)fd, TIOCMGET, &status); | |
472 | status &= ~TIOCM_RTS; |
|
464 | status &= ~TIOCM_RTS; | |
473 | if (ioctl((int)fd, TIOCMSET, &status)) |
|
465 | if (ioctl((int)fd, TIOCMSET, &status)) | |
474 | { |
|
466 | { | |
475 | return -1; |
|
467 | return -1; | |
476 | } |
|
468 | } | |
477 | return 0; |
|
469 | return 0; | |
478 | } |
|
470 | } | |
479 |
|
471 | |||
480 | int rs232clearRTS(rs232port_t fd) |
|
472 | int rs232clearRTS(rs232port_t fd) | |
481 | { |
|
473 | { | |
482 |
int status; |
|
474 | int status; | |
483 | ioctl((int)fd, TIOCMGET, &status); |
|
475 | ioctl((int)fd, TIOCMGET, &status); | |
484 | status |= TIOCM_RTS; |
|
476 | status |= TIOCM_RTS; | |
485 | if (ioctl((int)fd, TIOCMSET, &status)) |
|
477 | if (ioctl((int)fd, TIOCMSET, &status)) | |
486 | { |
|
478 | { | |
487 | return -1; |
|
479 | return -1; | |
488 | } |
|
480 | } | |
489 |
return 0; |
|
481 | return 0; | |
490 | } |
|
482 | } | |
491 |
|
483 | |||
492 | int rs232setDTR(rs232port_t fd) |
|
484 | int rs232setDTR(rs232port_t fd) | |
493 | { |
|
485 | { | |
494 | int status; |
|
486 | int status; | |
495 | ioctl((int)fd, TIOCMGET, &status); |
|
487 | ioctl((int)fd, TIOCMGET, &status); | |
496 |
status &= ~TIOCM_DTR; |
|
488 | status &= ~TIOCM_DTR; | |
497 | if (ioctl((int)fd, TIOCMSET, &status)) |
|
489 | if (ioctl((int)fd, TIOCMSET, &status)) | |
498 | { |
|
490 | { | |
499 |
return -1; |
|
491 | return -1; | |
500 | } |
|
492 | } | |
501 | return 0; |
|
493 | return 0; | |
502 | } |
|
494 | } | |
503 |
|
495 | |||
504 |
|
496 | |||
505 | int rs232clearDTR(rs232port_t fd) |
|
497 | int rs232clearDTR(rs232port_t fd) | |
506 | { |
|
498 | { | |
507 | int status; |
|
499 | int status; | |
508 | ioctl((int)fd, TIOCMGET, &status); |
|
500 | ioctl((int)fd, TIOCMGET, &status); | |
509 |
status |= TIOCM_DTR; |
|
501 | status |= TIOCM_DTR; | |
510 | if (ioctl((int)fd, TIOCMSET, &status)) |
|
502 | if (ioctl((int)fd, TIOCMSET, &status)) | |
511 | { |
|
503 | { | |
512 |
return -1; |
|
504 | return -1; | |
513 | } |
|
505 | } | |
514 | return 0; |
|
506 | return 0; | |
515 | } |
|
507 | } | |
516 |
|
508 | |||
517 |
|
509 | |||
518 |
|
510 | |||
519 | int rs232saferead(rs232port_t fd,char* data,int count ) |
|
511 | int rs232saferead(rs232port_t fd,char* data,int count ) | |
520 | { |
|
512 | { | |
521 |
|
|
513 | int read=0; | |
522 |
|
|
514 | int i=0; | |
523 |
|
|
515 | for(i=0;i<1000;i++) | |
524 | { |
|
516 | { | |
525 |
|
|
517 | read = rs232read((int)fd,data,count); | |
526 |
|
|
518 | //printf("read %d bytes\n",read); | |
527 |
|
|
519 | if(read==-1)read=0; | |
528 |
|
|
520 | count-=read; | |
529 |
|
|
521 | data+=read; | |
530 |
|
|
522 | if(count==0) | |
531 | return 0; |
|
523 | return 0; | |
532 |
|
|
524 | usleep(10); | |
533 | } |
|
525 | } | |
534 |
|
|
526 | return -1; | |
535 | } |
|
527 | } | |
536 |
|
528 | |||
537 |
|
529 | |||
538 |
|
530 | |||
539 | int rs232safewrite(rs232port_t fd,char* data,int count) |
|
531 | int rs232safewrite(rs232port_t fd,char* data,int count) | |
540 | { |
|
532 | { | |
541 |
|
|
533 | int written=0; | |
542 |
|
|
534 | int i=0; | |
543 |
|
|
535 | for(i=0;i<1000;i++) | |
544 | { |
|
536 | { | |
545 |
|
|
537 | written = rs232write((int)fd,data+written,count); | |
546 |
|
|
538 | //printf("%d bytes written\n",written); | |
547 |
|
|
539 | if(written==-1)written=0; | |
548 |
|
|
540 | count-=written; | |
549 |
|
|
541 | data+=written; | |
550 |
|
|
542 | if(count==0) | |
551 | return 0; |
|
543 | return 0; | |
552 | } |
|
544 | } | |
553 |
|
|
545 | return -1; | |
554 | } |
|
546 | } | |
555 |
|
547 | |||
556 | #endif |
|
|||
557 | #endif //#ifdef HAVE_TERMIOS_H |
|
|||
558 |
|
548 |
@@ -1,4 +1,3 | |||||
1 | #include "rs232config.h" |
|
|||
2 |
|
|
1 | #include <stdio.h> | |
3 | #include <unistd.h> |
|
2 | #include <unistd.h> | |
4 | #include <fcntl.h> |
|
3 | #include <fcntl.h> | |
@@ -6,12 +5,10 | |||||
6 | #include <errno.h> |
|
5 | #include <errno.h> | |
7 | #include <malloc.h> |
|
6 | #include <malloc.h> | |
8 |
|
7 | |||
9 | #ifdef HAVE_WINDOWS_H |
|
8 | #include <windows.h> | |
10 | #include <windows.h> |
|
|||
11 | #endif |
|
|||
12 | #include "RS232.h" |
|
9 | #include "RS232.h" | |
13 |
|
10 | |||
14 | #ifdef HAVE_WINDOWS_H |
|
11 | ||
15 |
|
12 | |||
16 |
|
13 | |||
17 | int privatedecodeparity(rs232parity Parity ) |
|
14 | int privatedecodeparity(rs232parity Parity ) | |
@@ -383,4 +380,4 int rs232clearDTR(rs232port_t fd) | |||||
383 | return -1; |
|
380 | return -1; | |
384 | } |
|
381 | } | |
385 |
|
382 | |||
386 | #endif //#ifdef HAVE_WINDOWS_H |
|
383 |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (674 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (732 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (9585 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (14851 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (14852 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (2500 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (597 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (1522 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (1766 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (14852 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (688 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (527 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (9655 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (7986 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
This diff has been collapsed as it changes many lines, (604 lines changed) Show them Hide them |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now