Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <sys/ioctl.h>
00012 #include <fcntl.h>
00013 #include <termios.h>
00014 #include <string.h>
00015 #include <unistd.h>
00016 #include <stdio.h>
00017 #include <leptrino/rs_comm.h>
00018
00019 #define MAX_BUFF 10
00020 #define MAX_LENGTH 255
00021
00022 #define STS_IDLE 0
00023 #define STS_WAIT_STX 1
00024 #define STS_DATA 2
00025 #define STS_WAIT_ETX 3
00026 #define STS_WAIT_BCC 4
00027
00028 static int Comm_RcvF = 0;
00029 static int p_rd = 0, p_wr = 0;
00030 static int fd = 0;
00031 static int rcv_n = 0;
00032
00033 static UCHAR delim;
00034 static UCHAR rcv_buff[MAX_BUFF][MAX_LENGTH];
00035 static UCHAR stmp[MAX_LENGTH];
00036
00037 struct termios tio;
00038
00039
00040
00041
00042
00043
00044
00045 int Comm_Open(const char * dev)
00046 {
00047
00048 if (fd != 0)
00049 Comm_Close();
00050
00051 fd = open(dev, O_RDWR | O_NDELAY | O_NOCTTY);
00052 if (fd < 0)
00053 return NG;
00054
00055 delim = 0;
00056
00057 return OK;
00058 }
00059
00060
00061
00062
00063
00064
00065
00066 void Comm_Close()
00067 {
00068 if (fd > 0)
00069 {
00070 close(fd);
00071 }
00072 fd = 0;
00073
00074 return;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 void Comm_Setup(long baud, int parity, int bitlen, int rts, int dtr, char code)
00088 {
00089 long brate;
00090 long cflg;
00091
00092 switch (baud)
00093 {
00094 case 2400:
00095 brate = B2400;
00096 break;
00097 case 4800:
00098 brate = B4800;
00099 break;
00100 case 9600:
00101 brate = B9600;
00102 break;
00103 case 19200:
00104 brate = B19200;
00105 break;
00106 case 38400:
00107 brate = B38400;
00108 break;
00109 case 57600:
00110 brate = B57600;
00111 break;
00112 case 115200:
00113 brate = B115200;
00114 break;
00115 case 230400:
00116 brate = B230400;
00117 break;
00118 case 460800:
00119 brate = B460800;
00120 break;
00121 default:
00122 brate = B9600;
00123 break;
00124 }
00125
00126 switch (parity)
00127 {
00128 case PAR_NON:
00129 cflg = 0;
00130 break;
00131 case PAR_ODD:
00132 cflg = PARENB | PARODD;
00133 break;
00134 default:
00135 cflg = PARENB;
00136 break;
00137 }
00138
00139 switch (bitlen)
00140 {
00141 case 7:
00142 cflg |= CS7;
00143 break;
00144 default:
00145 cflg |= CS8;
00146 break;
00147 }
00148
00149 switch (dtr)
00150 {
00151 case 1:
00152 cflg &= ~CLOCAL;
00153 break;
00154 default:
00155 cflg |= CLOCAL;
00156 break;
00157 }
00158
00159 switch (rts)
00160 {
00161 case 0:
00162 cflg &= ~CRTSCTS;
00163 break;
00164 default:
00165 cflg |= CRTSCTS;
00166 break;
00167 }
00168
00169
00170 tio.c_cflag = cflg | CREAD;
00171 tio.c_lflag = 0;
00172 tio.c_iflag = 0;
00173 tio.c_oflag = 0;
00174 tio.c_cc[VTIME] = 0;
00175 tio.c_cc[VMIN] = 0;
00176
00177 cfsetspeed(&tio, brate);
00178 tcflush(fd, TCIFLUSH);
00179 tcsetattr(fd, TCSANOW, &tio);
00180
00181 delim = code;
00182 return;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192 int Comm_SendData(UCHAR *buff, int l)
00193 {
00194 if (fd <= 0)
00195 return -1;
00196
00197 int count = write(fd, buff, l);
00198 if(count != l)
00199 return NG;
00200
00201 return OK;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210 int Comm_GetRcvData(UCHAR *buff)
00211 {
00212 int l = rcv_buff[p_rd][0];
00213
00214 if (p_wr == p_rd)
00215 return 0;
00216
00217 memcpy(buff, &rcv_buff[p_rd][0], l);
00218 p_rd++;
00219 if (p_rd >= MAX_BUFF)
00220 p_rd = 0;
00221
00222 l = strlen((char*)buff);
00223
00224 return l;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233 int Comm_CheckRcv()
00234 {
00235 return p_wr - p_rd;
00236 }
00237
00238
00239
00240
00241
00242
00243
00244 unsigned char rbuff[MAX_LENGTH];
00245 unsigned char ucBCC;
00246 void Comm_Rcv(void)
00247 {
00248 int rt = 0;
00249 unsigned char ch;
00250 static int RcvSts = 0;
00251
00252 while (1)
00253 {
00254 rt = read(fd, rbuff, 1);
00255
00256 if (rt > 0)
00257 {
00258 rbuff[rt] = 0;
00259 ch = rbuff[0];
00260
00261 switch (RcvSts)
00262 {
00263 case STS_IDLE:
00264 ucBCC = 0;
00265 rcv_n = 0;
00266 if (ch == CHR_DLE)
00267 RcvSts = STS_WAIT_STX;
00268 break;
00269 case STS_WAIT_STX:
00270 if (ch == CHR_STX)
00271 {
00272 RcvSts = STS_DATA;
00273 }
00274 else
00275 {
00276 RcvSts = STS_IDLE;
00277 }
00278 break;
00279 case STS_DATA:
00280 if (ch == CHR_DLE)
00281 {
00282 RcvSts = STS_WAIT_ETX;
00283 }
00284 else
00285 {
00286 stmp[rcv_n] = ch;
00287 ucBCC ^= ch;
00288 rcv_n++;
00289 }
00290 break;
00291 case STS_WAIT_ETX:
00292 if (ch == CHR_DLE)
00293 {
00294 stmp[rcv_n] = ch;
00295 ucBCC ^= ch;
00296 rcv_n++;
00297 RcvSts = STS_DATA;
00298 }
00299 else if (ch == CHR_ETX)
00300 {
00301 RcvSts = STS_WAIT_BCC;
00302 ucBCC ^= ch;
00303 }
00304 else if (ch == CHR_STX)
00305 {
00306 ucBCC = 0;
00307 rcv_n = 0;
00308 RcvSts = STS_DATA;
00309 }
00310 else
00311 {
00312 ucBCC = 0;
00313 rcv_n = 0;
00314 RcvSts = STS_IDLE;
00315 }
00316 break;
00317 case STS_WAIT_BCC:
00318 if (ucBCC == ch)
00319 {
00320
00321 memcpy(rcv_buff[p_wr], stmp, rcv_n);
00322 p_wr++;
00323 if (p_wr >= MAX_BUFF)
00324 p_wr = 0;
00325 }
00326
00327 ucBCC = 0;
00328 rcv_n = 0;
00329 RcvSts = STS_IDLE;
00330 break;
00331 default:
00332 RcvSts = STS_IDLE;
00333 break;
00334 }
00335
00336 if (rcv_n > MAX_LENGTH)
00337 {
00338 ucBCC = 0;
00339 rcv_n = 0;
00340 RcvSts = STS_IDLE;
00341 }
00342 }
00343 else
00344 {
00345 break;
00346 }
00347
00348
00349 if (p_rd != p_wr)
00350 {
00351 Comm_RcvF = 1;
00352 }
00353 else
00354 {
00355 Comm_RcvF = 0;
00356 }
00357 }
00358 }