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