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