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