##// END OF EJS Templates
Works on linux, need to be ported to Windows
jeandet -
r0:1e2e24b083a8 default
parent child
Show More
@@ -0,0 +1,58
1
2 VERSIONMJ=1
3 VERSIONM=0
4 BUILD=1
5 lIBFILE=liblppserial.so.$(VERSIONMJ).$(VERSIONM).$(BUILD)
6 CurrentPath="$(shell pwd)"
7
8
9
10 all:dirs
11 gcc -c RS232.c -fPIC -Wall -o ./tmp/RS232.o
12 gcc -shared -Wl,-soname,liblppserial.so.$(VERSIONMJ) -o ./bin/$(lIBFILE) ./tmp/RS232.o
13 ln -sf $(CurrentPath)/bin/$(lIBFILE) $(CurrentPath)/bin/liblppserial.so
14 ln -sf $(CurrentPath)/bin/$(lIBFILE) $(CurrentPath)/bin/liblppserial.so.1
15 ar rs $(CurrentPath)/bin/liblppserial.a ./tmp/RS232.o
16 cp RS232.h ./include/lppserial.h
17 @echo "lppserial compiled"
18
19
20
21 install:
22 sudo cp ./bin/$(lIBFILE) /usr/lib/$(lIBFILE)
23 sudo cp include/* /usr/include/
24 ln -sf /usr/lib/$(lIBFILE) /usr/lib/liblppserial.so
25 ln -sf /usr/lib/$(lIBFILE) /usr/lib/liblppserial.so.1
26 cp $(CurrentPath)/bin/liblppserial.a /usr/lib/liblppserial.a
27 ldconfig
28
29
30
31 dist:
32 tar -cvzf ./../lppserial.tgz ../lppserial
33
34
35
36
37 clean:
38 rm bin/*
39 rm tmp/*
40 rm include/*
41
42
43
44 distclean:
45 rm -f -R tmp
46 rm -f -R bin
47 rm -f -R include
48
49
50
51
52 dirs:
53 mkdir -p tmp
54 mkdir -p bin
55 mkdir -p include
56
57
58
@@ -0,0 +1,242
1 // SOPSUYSI_RS232.c - Function to communicate on Serial Port
2
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <termios.h>
7 #include <string.h>
8 #include <errno.h>
9 #include "RS232.h"
10
11
12 speed_t rs232cfspeed(unsigned int BaudeRate)
13 {
14 if(BaudeRate<25)
15 return B0;
16
17 if(BaudeRate<67)
18 return B50;
19
20 if(BaudeRate<93)
21 return B75;
22
23 if(BaudeRate<123)
24 return B110;
25
26 if(BaudeRate<142)
27 return B134;
28
29 if(BaudeRate<175)
30 return B150;
31
32 if(BaudeRate<250)
33 return B200;
34
35 if(BaudeRate<450)
36 return B300;
37
38 if(BaudeRate<900)
39 return B600;
40
41 if(BaudeRate<1500)
42 return B1200;
43
44 if(BaudeRate<2100)
45 return B1800;
46
47 if(BaudeRate<3600)
48 return B2400;
49
50 if(BaudeRate<7200)
51 return B4800;
52
53 if(BaudeRate<1400)
54 return B9600;
55
56 if(BaudeRate<28800)
57 return B19200;
58
59 if(BaudeRate<48000)
60 return B38400;
61
62 if(BaudeRate<86400)
63 return B57600;
64
65 if(BaudeRate<172800)
66 return B115200;
67 else
68 // if(172799<BaudeRate)
69 return B230400;
70 }
71
72
73 int rs232cfparity(int fd, struct termios *terminos, int Parity)
74 {
75 if (fd == -1)
76 {
77 return fd;
78 }
79 else
80 {
81 switch(Parity)
82 {
83 case 1:
84 terminos->c_cflag |= PARENB;
85 break;
86 default:
87 terminos->c_cflag &= ~PARENB;
88 break;
89 }
90 return 0;
91 }
92 }
93
94
95 int rs232cfnbstop(int fd, struct termios *terminos, int NbStop)
96 {
97 if (fd == -1)
98 {
99 return fd;
100 }
101 else
102 {
103 switch(NbStop)
104 {
105 case 2:
106 terminos->c_cflag |= CSTOPB;
107 break;
108 default:
109 terminos->c_cflag &= ~CSTOPB;
110 break;
111 }
112 return 0;
113 }
114 }
115
116
117 int rs232cfcsize(int fd, struct termios *terminos, int ChSize)
118 {
119 if (fd == -1)
120 {
121 return fd;
122 }
123 else
124 {
125 switch(ChSize)
126 {
127 case 5:
128 terminos->c_cflag |= CS5;
129 break;
130 case 6:
131 terminos->c_cflag |= CS6;
132 break;
133 case 7:
134 terminos->c_cflag |= CS7;
135 break;
136 default:
137 terminos->c_cflag |= CS8;
138 break;
139 }
140 return 0;
141 }
142 }
143
144
145 int rs232open(char* psPortName)
146 {
147
148 int fd = open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY);
149
150 return fd;
151 }
152
153
154 int rs232setup(int fd, int ChSize, int BaudeRate, int Parity, int NbStop)
155 {
156 if (fd == -1)
157 {
158 return fd;
159 }
160 else
161 {
162 struct termios terminos;
163 tcgetattr(fd, &terminos);
164
165 cfsetispeed(&terminos, rs232cfspeed(BaudeRate));
166 cfsetospeed(&terminos, rs232cfspeed(BaudeRate));
167
168 terminos.c_cflag |= (CLOCAL | CREAD);
169 rs232cfparity(fd, &terminos, Parity);
170 rs232cfnbstop(fd, &terminos, NbStop);
171 rs232cfcsize(fd, &terminos, ChSize);
172
173
174 tcsetattr(fd, TCSANOW, &terminos);
175 tcgetattr(fd, &terminos);
176 return 0;
177 }
178 }
179
180
181 int rs232write(int fd,char *psWrite)
182 {
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
206 if (fd == -1)
207 {
208 return fd;
209 }
210 else
211 {
212 read(fd, ReadBuff, ReadBufferSize);
213 while(CharsRead<(ReadBufferSize-1))
214 {
215 for(i=0;i<ReadBufferSize;i++)
216 ReadBuff[i]='\0';
217
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;
227 }
228 }
229
230
231 int rs232close(int fd)
232 {
233 if (fd == -1)
234 {
235 return fd;
236 }
237 else
238 {
239 close(fd);
240 return 0;
241 }
242 }
@@ -0,0 +1,11
1 // SOPSUYSI_RS232.h
2
3
4 #define ReadBufferSize 16*1024
5
6
7 int rs232open(char* psPortName);
8 int rs232close(int fd);
9 int rs232setup(int fd, int ChSize, int BaudeRate, int Parity, int NbStop);
10 int rs232write(int fd,char *psWrite);
11 int rs232read(int fd,char *psReadHex);
General Comments 0
You need to be logged in to leave comments. Login now