Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
145 | f9daq | 1 | #include <sys/stat.h> |
2 | #include <fcntl.h> |
||
3 | #include <string.h> |
||
4 | #include <termios.h> |
||
5 | |||
6 | #include "rs232.h" |
||
7 | //#define DEBUG |
||
8 | |||
9 | const int debug=0; |
||
10 | |||
11 | struct termios tattr; |
||
12 | |||
13 | int Delay(double sec){ |
||
14 | return usleep((int) (sec*1e6) ); |
||
15 | } |
||
16 | |||
17 | int SetComTime(int fd, double timeout_seconds){ |
||
18 | |||
19 | int ntenth; |
||
20 | |||
21 | ntenth = (int)(timeout_seconds*10); |
||
22 | tattr.c_cc[VTIME] = ntenth; |
||
23 | tcsetattr(fd, TCSANOW, &tattr); |
||
24 | #ifdef DEBUG |
||
25 | printf("SetComTime: %d\n", ntenth); |
||
26 | #endif |
||
27 | return 0; |
||
28 | } |
||
29 | |||
30 | int FlushInQ(int fd){ |
||
31 | return 0; |
||
32 | } |
||
33 | |||
34 | int FlushOutQ(int fd){ |
||
35 | return 0; |
||
36 | } |
||
37 | |||
38 | int ComWrt (int fd, char* cmd, int Count ){ |
||
39 | |||
40 | int nwr; |
||
41 | |||
42 | nwr=write(fd, cmd, Count); |
||
43 | #ifdef DEBUG |
||
44 | printf ( "ComWrt: %d, %d, %s\n", Count, nwr, cmd); |
||
45 | #endif |
||
46 | return nwr; |
||
47 | } |
||
48 | |||
49 | int ComRdTerm (int fd, char *response, int nb, int termchar) |
||
50 | { |
||
51 | |||
52 | int nread, nloop; |
||
53 | |||
54 | nread=0; |
||
55 | nloop=0; |
||
56 | |||
57 | #ifdef DEBUG |
||
58 | printf ( "ComRdTerm start\n") ; |
||
59 | #endif |
||
60 | // rewind(fpr); |
||
61 | while (1) { |
||
62 | if (nloop++ == nb) return -1; |
||
63 | // nread += read ( fd, response+nread, nb-nread ); |
||
64 | nread += read ( fd, response+nread, 1 ); |
||
65 | #ifdef DEBUG |
||
66 | response[nread]=0; |
||
67 | printf ("ComRdTerm nread: %d %d %s\n", nloop, nread, response) ; |
||
68 | #endif |
||
69 | if (nread>1) if(response[nread-1] == termchar) break; |
||
70 | } |
||
71 | |||
72 | nread = nread - 2; |
||
73 | response[nread]=0; |
||
74 | #ifdef DEBUG |
||
75 | printf("CmdRdTerm: %s\n",response); |
||
76 | #endif |
||
77 | return nread; |
||
78 | } |
||
79 | |||
80 | int OpenComConfig( char *dev, char * device_name, long Baud_Rate, int Parity, int Data_Bits, |
||
81 | int Stop_Bits, int Input_Queue_Size, int Output_Queue_Size ){ |
||
82 | |||
83 | int fd; |
||
84 | |||
85 | memset (&tattr, 0, sizeof tattr); |
||
86 | |||
87 | fd=open(dev, O_RDWR | O_NOCTTY | O_SYNC); |
||
88 | // see 'man tcsetattr' |
||
89 | tcgetattr (fd, &tattr); |
||
90 | cfmakeraw(&tattr); |
||
91 | cfsetspeed(&tattr, B38400); |
||
92 | // cfsetispeed(&tattr, B38400); |
||
93 | // cfsetospeed(&tattr, B38400); |
||
94 | // input modes |
||
95 | tattr.c_iflag&=~IGNBRK; |
||
96 | tattr.c_iflag&=~(IGNCR | ICRNL | INLCR); |
||
97 | tattr.c_iflag&=~(IXON | IXOFF | IXANY); |
||
98 | // output modess |
||
99 | tattr.c_oflag=0; |
||
100 | // local modes |
||
101 | tattr.c_lflag=0; |
||
102 | tattr.c_lflag &= ~(ICANON|ECHO) ; // canonical mode and echo input char |
||
103 | // control modes |
||
104 | tattr.c_cflag |= (CLOCAL | CREAD); // ignore modem controls, |
||
105 | // enable reading |
||
106 | tattr.c_cflag &= ~(PARENB | PARODD); // shut off parity |
||
107 | tattr.c_cflag &= ~CSTOPB; // set two stop bits |
||
108 | tattr.c_cflag &= ~CRTSCTS; // enable RTS/CTS flow control |
||
109 | |||
110 | tattr.c_cc[VMIN] = 0; |
||
111 | tattr.c_cc[VTIME] = 2; |
||
112 | tcsetattr(fd, TCSAFLUSH, &tattr); |
||
113 | |||
114 | #ifdef DEBUG |
||
115 | printf("OpenComConfig\n"); |
||
116 | #endif |
||
117 | return fd; ; |
||
118 | } |
||
119 | |||
120 | |||
121 | int CloseCom (int fd){ |
||
122 | |||
123 | close(fd); |
||
124 | return; |
||
125 | } |