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