@@ -1,242 +1,220 | |||
|
1 | 1 | // SOPSUYSI_RS232.c - Function to communicate on Serial Port |
|
2 | 2 | |
|
3 | 3 | #include <stdio.h> |
|
4 | 4 | #include <unistd.h> |
|
5 | 5 | #include <fcntl.h> |
|
6 | 6 | #include <termios.h> |
|
7 | 7 | #include <string.h> |
|
8 | 8 | #include <errno.h> |
|
9 | 9 | #include "RS232.h" |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | speed_t rs232cfspeed(unsigned int BaudeRate) |
|
13 | 13 | { |
|
14 | 14 | if(BaudeRate<25) |
|
15 | 15 | return B0; |
|
16 | 16 | |
|
17 | 17 | if(BaudeRate<67) |
|
18 | 18 | return B50; |
|
19 | 19 | |
|
20 | 20 | if(BaudeRate<93) |
|
21 | 21 | return B75; |
|
22 | 22 | |
|
23 | 23 | if(BaudeRate<123) |
|
24 | 24 | return B110; |
|
25 | 25 | |
|
26 | 26 | if(BaudeRate<142) |
|
27 | 27 | return B134; |
|
28 | 28 | |
|
29 | 29 | if(BaudeRate<175) |
|
30 | 30 | return B150; |
|
31 | 31 | |
|
32 | 32 | if(BaudeRate<250) |
|
33 | 33 | return B200; |
|
34 | 34 | |
|
35 | 35 | if(BaudeRate<450) |
|
36 | 36 | return B300; |
|
37 | 37 | |
|
38 | 38 | if(BaudeRate<900) |
|
39 | 39 | return B600; |
|
40 | 40 | |
|
41 | 41 | if(BaudeRate<1500) |
|
42 | 42 | return B1200; |
|
43 | 43 | |
|
44 | 44 | if(BaudeRate<2100) |
|
45 | 45 | return B1800; |
|
46 | 46 | |
|
47 | 47 | if(BaudeRate<3600) |
|
48 | 48 | return B2400; |
|
49 | 49 | |
|
50 | 50 | if(BaudeRate<7200) |
|
51 | 51 | return B4800; |
|
52 | 52 | |
|
53 | 53 | if(BaudeRate<1400) |
|
54 | 54 | return B9600; |
|
55 | 55 | |
|
56 | 56 | if(BaudeRate<28800) |
|
57 | 57 | return B19200; |
|
58 | 58 | |
|
59 | 59 | if(BaudeRate<48000) |
|
60 | 60 | return B38400; |
|
61 | 61 | |
|
62 | 62 | if(BaudeRate<86400) |
|
63 | 63 | return B57600; |
|
64 | 64 | |
|
65 | 65 | if(BaudeRate<172800) |
|
66 | 66 | return B115200; |
|
67 | 67 | else |
|
68 | 68 | // if(172799<BaudeRate) |
|
69 | 69 | return B230400; |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | |
|
73 | 73 | int rs232cfparity(int fd, struct termios *terminos, int Parity) |
|
74 | 74 | { |
|
75 | 75 | if (fd == -1) |
|
76 | 76 | { |
|
77 | 77 | return fd; |
|
78 | 78 | } |
|
79 | 79 | else |
|
80 | 80 | { |
|
81 | 81 | switch(Parity) |
|
82 | 82 | { |
|
83 | 83 | case 1: |
|
84 | 84 | terminos->c_cflag |= PARENB; |
|
85 | 85 | break; |
|
86 | 86 | default: |
|
87 | 87 | terminos->c_cflag &= ~PARENB; |
|
88 | 88 | break; |
|
89 | 89 | } |
|
90 | 90 | return 0; |
|
91 | 91 | } |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | |
|
95 | 95 | int rs232cfnbstop(int fd, struct termios *terminos, int NbStop) |
|
96 | 96 | { |
|
97 | 97 | if (fd == -1) |
|
98 | 98 | { |
|
99 | 99 | return fd; |
|
100 | 100 | } |
|
101 | 101 | else |
|
102 | 102 | { |
|
103 | 103 | switch(NbStop) |
|
104 | 104 | { |
|
105 | 105 | case 2: |
|
106 | 106 | terminos->c_cflag |= CSTOPB; |
|
107 | 107 | break; |
|
108 | 108 | default: |
|
109 | 109 | terminos->c_cflag &= ~CSTOPB; |
|
110 | 110 | break; |
|
111 | 111 | } |
|
112 | 112 | return 0; |
|
113 | 113 | } |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | |
|
117 | 117 | int rs232cfcsize(int fd, struct termios *terminos, int ChSize) |
|
118 | 118 | { |
|
119 | 119 | if (fd == -1) |
|
120 | 120 | { |
|
121 | 121 | return fd; |
|
122 | 122 | } |
|
123 | 123 | else |
|
124 | 124 | { |
|
125 | 125 | switch(ChSize) |
|
126 | 126 | { |
|
127 | 127 | case 5: |
|
128 | 128 | terminos->c_cflag |= CS5; |
|
129 | 129 | break; |
|
130 | 130 | case 6: |
|
131 | 131 | terminos->c_cflag |= CS6; |
|
132 | 132 | break; |
|
133 | 133 | case 7: |
|
134 | 134 | terminos->c_cflag |= CS7; |
|
135 | 135 | break; |
|
136 | 136 | default: |
|
137 | 137 | terminos->c_cflag |= CS8; |
|
138 | 138 | break; |
|
139 | 139 | } |
|
140 | 140 | return 0; |
|
141 | 141 | } |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | |
|
145 | 145 | int rs232open(char* psPortName) |
|
146 | 146 | { |
|
147 | 147 | |
|
148 | 148 | int fd = open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY); |
|
149 | 149 | |
|
150 | 150 | return fd; |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | |
|
154 | 154 | int rs232setup(int fd, int ChSize, int BaudeRate, int Parity, int NbStop) |
|
155 | 155 | { |
|
156 | 156 | if (fd == -1) |
|
157 | 157 | { |
|
158 | 158 | return fd; |
|
159 | 159 | } |
|
160 | 160 | else |
|
161 | 161 | { |
|
162 | 162 | struct termios terminos; |
|
163 | 163 | tcgetattr(fd, &terminos); |
|
164 | 164 | |
|
165 | 165 | cfsetispeed(&terminos, rs232cfspeed(BaudeRate)); |
|
166 | 166 | cfsetospeed(&terminos, rs232cfspeed(BaudeRate)); |
|
167 | 167 | |
|
168 | 168 | terminos.c_cflag |= (CLOCAL | CREAD); |
|
169 | 169 | rs232cfparity(fd, &terminos, Parity); |
|
170 | 170 | rs232cfnbstop(fd, &terminos, NbStop); |
|
171 | 171 | rs232cfcsize(fd, &terminos, ChSize); |
|
172 | 172 | |
|
173 | 173 | |
|
174 | 174 | tcsetattr(fd, TCSANOW, &terminos); |
|
175 | 175 | tcgetattr(fd, &terminos); |
|
176 | 176 | return 0; |
|
177 | 177 | } |
|
178 | 178 | } |
|
179 | 179 | |
|
180 | 180 | |
|
181 | 181 | int rs232write(int fd,char *psWrite) |
|
182 | 182 | { |
|
183 | 183 | int Len_psWrite = (int)strlen(psWrite); |
|
184 | int charsWrite=0; | |
|
185 | ||
|
186 | if (fd == -1) | |
|
187 | { | |
|
188 | return -1; | |
|
189 | } | |
|
190 | else | |
|
191 | { | |
|
192 | charsWrite = write(fd, psWrite, Len_psWrite); | |
|
193 | return charsWrite; | |
|
194 | } | |
|
195 | } | |
|
196 | ||
|
197 | ||
|
198 | int rs232read(int fd,char *psReadHex) | |
|
199 | { | |
|
200 | char ReadBuff[ReadBufferSize]; | |
|
201 | int CharsRead=0, Reads=0, i=0; | |
|
202 | ||
|
203 | for(i=0;i<ReadBufferSize;i++) | |
|
204 | ReadBuff[i]='\0'; | |
|
205 | 184 | |
|
206 | 185 | if (fd == -1) |
|
207 | 186 | { |
|
208 | 187 | return fd; |
|
209 | 188 | } |
|
210 | 189 | else |
|
211 | 190 | { |
|
212 | read(fd, ReadBuff, ReadBufferSize); | |
|
213 | while(CharsRead<(ReadBufferSize-1)) | |
|
214 | { | |
|
215 | for(i=0;i<ReadBufferSize;i++) | |
|
216 | ReadBuff[i]='\0'; | |
|
191 | return write(fd, psWrite, Len_psWrite); | |
|
192 | } | |
|
193 | } | |
|
194 | ||
|
217 | 195 | |
|
218 | if((Reads = read(fd, ReadBuff, (ReadBufferSize-1)-CharsRead)) > 0) | |
|
219 | { | |
|
220 | CharsRead = CharsRead + Reads; | |
|
221 | ReadBuff[CharsRead]='\0'; | |
|
222 | strcat(psReadHex, ReadBuff); | |
|
223 |
|
|
|
224 | ||
|
225 | } | |
|
226 | return CharsRead; | |
|
196 | int rs232read(int fd,char *psReadHex, int ReadBufferSize) | |
|
197 | { | |
|
198 | if (fd == -1) | |
|
199 | { | |
|
200 | return fd; | |
|
201 | } | |
|
202 | else | |
|
203 | { | |
|
204 | return read(fd, psReadHex, ReadBufferSize); | |
|
227 | 205 | } |
|
228 | 206 | } |
|
229 | 207 | |
|
230 | 208 | |
|
231 | 209 | int rs232close(int fd) |
|
232 | 210 | { |
|
233 | 211 | if (fd == -1) |
|
234 | 212 | { |
|
235 | 213 | return fd; |
|
236 | 214 | } |
|
237 | 215 | else |
|
238 | 216 | { |
|
239 | 217 | close(fd); |
|
240 | 218 | return 0; |
|
241 | 219 | } |
|
242 | 220 | } |
@@ -1,11 +1,9 | |||
|
1 | 1 | // SOPSUYSI_RS232.h |
|
2 | 2 | |
|
3 | 3 | |
|
4 | #define ReadBufferSize 16*1024 | |
|
5 | ||
|
6 | 4 | |
|
7 | 5 | int rs232open(char* psPortName); |
|
8 | 6 | int rs232close(int fd); |
|
9 | 7 | int rs232setup(int fd, int ChSize, int BaudeRate, int Parity, int NbStop); |
|
10 | 8 | int rs232write(int fd,char *psWrite); |
|
11 | int rs232read(int fd,char *psReadHex); | |
|
9 | int rs232read(int fd,char *psReadHex, int ReadBufferSize); |
General Comments 0
You need to be logged in to leave comments.
Login now