Rev 270 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 270 | Rev 800 | ||
|---|---|---|---|
| 1 | /*************************************************************************** |
1 | /*************************************************************************** |
| 2 | rlserial.cpp - description |
2 | rlserial.cpp - description |
| 3 | ------------------- |
3 | ------------------- |
| 4 | begin : Sat Dec 21 2002 |
4 | begin : Sat Dec 21 2002 |
| 5 | copyright : (C) 2002 by Rainer Lehrig |
5 | copyright : (C) 2002 by Rainer Lehrig |
| 6 | email : lehrig@t-online.de |
6 | email : lehrig@t-online.de |
| 7 | |
7 | |
| 8 | RMOS implementation: |
8 | RMOS implementation: |
| 9 | Copyright : (C) 2004 Zertrox GbR |
9 | Copyright : (C) 2004 Zertrox GbR |
| 10 | Written by : Alexander Feller |
10 | Written by : Alexander Feller |
| 11 | Email : feller@zertrox.de |
11 | Email : feller@zertrox.de |
| 12 | ***************************************************************************/ |
12 | ***************************************************************************/ |
| 13 | 13 | ||
| 14 | /*************************************************************************** |
14 | /*************************************************************************** |
| 15 | * * |
15 | * * |
| 16 | * This library is free software; you can redistribute it and/or modify * |
16 | * This library is free software; you can redistribute it and/or modify * |
| 17 | * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as * |
17 | * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as * |
| 18 | * published by the Free Software Foundation * |
18 | * published by the Free Software Foundation * |
| 19 | * * |
19 | * * |
| 20 | ***************************************************************************/ |
20 | ***************************************************************************/ |
| 21 | #include "rlserial.h" |
21 | #include "rlserial.h" |
| 22 | 22 | ||
| 23 | #ifdef unix |
23 | #ifdef unix |
| 24 | #include <stdio.h> |
24 | #include <stdio.h> |
| 25 | #include <sys/types.h> |
25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
26 | #include <sys/stat.h> |
| 27 | #include <fcntl.h> |
27 | #include <fcntl.h> |
| 28 | #include <termios.h> |
28 | #include <termios.h> |
| 29 | #include <unistd.h> |
29 | #include <unistd.h> |
| 30 | #include <signal.h> |
30 | #include <signal.h> |
| 31 | #endif |
31 | #endif |
| 32 | 32 | ||
| 33 | #ifdef __VMS |
33 | #ifdef __VMS |
| 34 | #include <stdio.h> |
34 | #include <stdio.h> |
| 35 | #include <string.h> |
35 | #include <string.h> |
| 36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
| 37 | #include <starlet.h> |
37 | #include <starlet.h> |
| 38 | #include <descrip.h> |
38 | #include <descrip.h> |
| 39 | #include <lib$routines.h> |
39 | #include <lib$routines.h> |
| 40 | #include <ssdef.h> |
40 | #include <ssdef.h> |
| 41 | #include <iodef.h> |
41 | #include <iodef.h> |
| 42 | #endif |
42 | #endif |
| 43 | 43 | ||
| 44 | #ifdef _WIN32 |
44 | #ifdef _WIN32 |
| 45 | #include <windows.h> |
45 | #include <windows.h> |
| 46 | #include <stdio.h> |
46 | #include <stdio.h> |
| 47 | #endif |
47 | #endif |
| 48 | 48 | ||
| 49 | #ifdef RM3 |
49 | #ifdef RM3 |
| 50 | #include <stdio.h> |
50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
51 | #include <stdlib.h> |
| 52 | #include <rmcomp.h> |
52 | #include <rmcomp.h> |
| 53 | #include <rmapi.h> |
53 | #include <rmapi.h> |
| 54 | #include <rio.h> |
54 | #include <rio.h> |
| 55 | #include <drvspec.h> |
55 | #include <drvspec.h> |
| 56 | #include <rm3cav.h> |
56 | #include <rm3cav.h> |
| 57 | //#include <clean.h> |
57 | //#include <clean.h> |
| 58 | #include <rcinc.h> |
58 | #include <rcinc.h> |
| 59 | #define RTS_TIME_WAIT 0x008 /* Verzoerungszeit fuer RTS - Signal */ |
59 | #define RTS_TIME_WAIT 0x008 /* Verzoerungszeit fuer RTS - Signal */ |
| 60 | #endif |
60 | #endif |
| 61 | 61 | ||
| 62 | #include "rlthread.h" |
62 | #include "rlthread.h" |
| 63 | 63 | ||
| 64 | /* |
64 | /* |
| 65 | static void sighandler(int sig) |
65 | static void sighandler(int sig) |
| 66 | { |
66 | { |
| 67 | if(sig == SIGINT) |
67 | if(sig == SIGINT) |
| 68 | { |
68 | { |
| 69 | closeDevice(); |
69 | closeDevice(); |
| 70 | closeDatabase(); |
70 | closeDatabase(); |
| 71 | } |
71 | } |
| 72 | } |
72 | } |
| 73 | */ |
73 | */ |
| 74 | 74 | ||
| 75 | rlSerial::rlSerial() |
75 | rlSerial::rlSerial() |
| 76 | { |
76 | { |
| 77 | ttysavefd = -1; |
77 | ttysavefd = -1; |
| 78 | ttystate = RESET; |
78 | ttystate = RESET; |
| 79 | fd = -1; |
79 | fd = -1; |
| 80 | trace = 0; |
80 | trace = 0; |
| 81 | } |
81 | } |
| 82 | 82 | ||
| 83 | rlSerial::~rlSerial() |
83 | rlSerial::~rlSerial() |
| 84 | { |
84 | { |
| 85 | closeDevice(); |
85 | closeDevice(); |
| 86 | } |
86 | } |
| 87 | 87 | ||
| 88 | void rlSerial::setTrace(int on) |
88 | void rlSerial::setTrace(int on) |
| 89 | { |
89 | { |
| 90 | if(on == 1) trace = 1; |
90 | if(on == 1) trace = 1; |
| 91 | else trace = 0; |
91 | else trace = 0; |
| 92 | } |
92 | } |
| 93 | 93 | ||
| 94 | int rlSerial::openDevice(const char *devicename, int speed, int block, int rtscts, int bits, int stopbits, int parity) |
94 | int rlSerial::openDevice(const char *devicename, int speed, int block, int rtscts, int bits, int stopbits, int parity) |
| 95 | { |
95 | { |
| 96 | #ifdef unix |
96 | #ifdef unix |
| 97 | struct termios buf; |
97 | struct termios buf; |
| 98 | 98 | ||
| 99 | if(fd != -1) return -1; |
99 | if(fd != -1) return -1; |
| 100 | fd = open(devicename, O_RDWR | O_NOCTTY | O_NDELAY); |
100 | fd = open(devicename, O_RDWR | O_NOCTTY | O_NDELAY); |
| 101 | if(fd < 0) { return -1; } |
101 | if(fd < 0) { return -1; } |
| 102 | 102 | ||
| 103 | //signal(SIGINT, sighandler); |
103 | //signal(SIGINT, sighandler); |
| 104 | 104 | ||
| 105 | if(tcgetattr(fd, &save_termios) < 0) { return -1; } |
105 | if(tcgetattr(fd, &save_termios) < 0) { return -1; } |
| 106 | buf = save_termios; |
106 | buf = save_termios; |
| 107 | buf.c_cflag = speed | CLOCAL | CREAD; |
107 | buf.c_cflag = speed | CLOCAL | CREAD; |
| 108 | if(rtscts == 1) buf.c_cflag |= CRTSCTS; |
108 | if(rtscts == 1) buf.c_cflag |= CRTSCTS; |
| 109 | if(bits == 7) buf.c_cflag |= CS7; |
109 | if(bits == 7) buf.c_cflag |= CS7; |
| 110 | else buf.c_cflag |= CS8; |
110 | else buf.c_cflag |= CS8; |
| 111 | if(stopbits == 2) buf.c_cflag |= CSTOPB; |
111 | if(stopbits == 2) buf.c_cflag |= CSTOPB; |
| 112 | if(parity == rlSerial::ODD) buf.c_cflag |= (PARENB | PARODD); |
112 | if(parity == rlSerial::ODD) buf.c_cflag |= (PARENB | PARODD); |
| 113 | if(parity == rlSerial::EVEN) buf.c_cflag |= PARENB; |
113 | if(parity == rlSerial::EVEN) buf.c_cflag |= PARENB; |
| 114 | buf.c_lflag = IEXTEN; //ICANON; |
114 | buf.c_lflag = IEXTEN; //ICANON; |
| 115 | buf.c_oflag = OPOST; |
115 | buf.c_oflag = OPOST; |
| 116 | buf.c_cc[VMIN] = 1; |
116 | buf.c_cc[VMIN] = 1; |
| 117 | buf.c_cc[VTIME] = 0; |
117 | buf.c_cc[VTIME] = 0; |
| 118 | buf.c_line = 0; |
118 | buf.c_line = 0; |
| 119 | buf.c_iflag = IGNBRK | IGNPAR | IXANY; |
119 | buf.c_iflag = IGNBRK | IGNPAR | IXANY; |
| 120 | if(tcsetattr(fd, TCSAFLUSH, &buf) < 0) { return -1; } |
120 | if(tcsetattr(fd, TCSAFLUSH, &buf) < 0) { return -1; } |
| 121 | //if(tcsetattr(fd, TCSANOW, &buf) < 0) { return -1; } |
121 | //if(tcsetattr(fd, TCSANOW, &buf) < 0) { return -1; } |
| 122 | ttystate = RAW; |
122 | ttystate = RAW; |
| 123 | ttysavefd = fd; |
123 | ttysavefd = fd; |
| 124 | if(block == 1) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK); |
124 | if(block == 1) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK); |
| 125 | tcflush(fd,TCIOFLUSH); |
125 | tcflush(fd,TCIOFLUSH); |
| 126 | #endif |
126 | #endif |
| 127 | 127 | ||
| 128 | #ifdef __VMS |
128 | #ifdef __VMS |
| 129 | // Please set com parameters at DCL level |
129 | // Please set com parameters at DCL level |
| 130 | struct dsc$descriptor_s dsc; |
130 | struct dsc$descriptor_s dsc; |
| 131 | int status; |
131 | int status; |
| 132 | 132 | ||
| 133 | dsc.dsc$w_length = strlen(devicename); |
133 | dsc.dsc$w_length = strlen(devicename); |
| 134 | dsc.dsc$a_pointer = devicename; |
134 | dsc.dsc$a_pointer = devicename; |
| 135 | dsc.dsc$b_class = DSC$K_CLASS_S; |
135 | dsc.dsc$b_class = DSC$K_CLASS_S; |
| 136 | dsc.dsc$b_dtype = DSC$K_DTYPE_T; |
136 | dsc.dsc$b_dtype = DSC$K_DTYPE_T; |
| 137 | status = SYS$ASSIGN(&dsc,&vms_channel,0,0); |
137 | status = SYS$ASSIGN(&dsc,&vms_channel,0,0); |
| 138 | if(status != SS$_NORMAL) return -1; |
138 | if(status != SS$_NORMAL) return -1; |
| 139 | #endif |
139 | #endif |
| 140 | 140 | ||
| 141 | #ifdef _WIN32 |
141 | #ifdef _WIN32 |
| 142 | DWORD ccsize; |
142 | DWORD ccsize; |
| 143 | COMMCONFIG cc; |
143 | COMMCONFIG cc; |
| 144 | int baudrate,ret; |
144 | int baudrate,ret; |
| 145 | 145 | ||
| 146 | hdl = CreateFile( |
146 | hdl = CreateFile( |
| 147 | (LPCWSTR)devicename, // pointer to name of the file |
147 | (LPCWSTR)devicename, // pointer to name of the file |
| 148 | GENERIC_READ | GENERIC_WRITE, // access (read-write) mode |
148 | GENERIC_READ | GENERIC_WRITE, // access (read-write) mode |
| 149 | 0, // share mode |
149 | 0, // share mode |
| 150 | 0, // pointer to security attributes |
150 | 0, // pointer to security attributes |
| 151 | OPEN_EXISTING, // how to create |
151 | OPEN_EXISTING, // how to create |
| 152 | 0, // not overlapped I/O |
152 | 0, // not overlapped I/O |
| 153 | 0 // handle to file with attributes to copy |
153 | 0 // handle to file with attributes to copy |
| 154 | ); |
154 | ); |
| 155 | if(hdl == INVALID_HANDLE_VALUE) |
155 | if(hdl == INVALID_HANDLE_VALUE) |
| 156 | { |
156 | { |
| 157 | printf("CreateFile(%s) failed\n",devicename); |
157 | printf("CreateFile(%s) failed\n",devicename); |
| 158 | return -1; |
158 | return -1; |
| 159 | } |
159 | } |
| 160 | 160 | ||
| 161 | baudrate = CBR_9600; |
161 | baudrate = CBR_9600; |
| 162 | if(speed == B50 ) baudrate = 50; |
162 | if(speed == B50 ) baudrate = 50; |
| 163 | if(speed == B75 ) baudrate = 75; |
163 | if(speed == B75 ) baudrate = 75; |
| 164 | if(speed == B110 ) baudrate = CBR_110; |
164 | if(speed == B110 ) baudrate = CBR_110; |
| 165 | if(speed == B134 ) baudrate = 134; |
165 | if(speed == B134 ) baudrate = 134; |
| 166 | if(speed == B150 ) baudrate = 150; |
166 | if(speed == B150 ) baudrate = 150; |
| 167 | if(speed == B200 ) baudrate = 200; |
167 | if(speed == B200 ) baudrate = 200; |
| 168 | if(speed == B300 ) baudrate = CBR_300; |
168 | if(speed == B300 ) baudrate = CBR_300; |
| 169 | if(speed == B600 ) baudrate = CBR_600; |
169 | if(speed == B600 ) baudrate = CBR_600; |
| 170 | if(speed == B1200 ) baudrate = CBR_1200; |
170 | if(speed == B1200 ) baudrate = CBR_1200; |
| 171 | if(speed == B1800 ) baudrate = 1800; |
171 | if(speed == B1800 ) baudrate = 1800; |
| 172 | if(speed == B2400 ) baudrate = CBR_2400; |
172 | if(speed == B2400 ) baudrate = CBR_2400; |
| 173 | if(speed == B4800 ) baudrate = CBR_4800; |
173 | if(speed == B4800 ) baudrate = CBR_4800; |
| 174 | if(speed == B9600 ) baudrate = CBR_9600; |
174 | if(speed == B9600 ) baudrate = CBR_9600; |
| 175 | if(speed == B19200 ) baudrate = CBR_19200; |
175 | if(speed == B19200 ) baudrate = CBR_19200; |
| 176 | if(speed == B38400 ) baudrate = CBR_38400; |
176 | if(speed == B38400 ) baudrate = CBR_38400; |
| 177 | if(speed == B57600 ) baudrate = CBR_57600; |
177 | if(speed == B57600 ) baudrate = CBR_57600; |
| 178 | if(speed == B115200 ) baudrate = CBR_115200; |
178 | if(speed == B115200 ) baudrate = CBR_115200; |
| 179 | if(speed == B230400 ) baudrate = 230400; |
179 | if(speed == B230400 ) baudrate = 230400; |
| 180 | if(speed == B460800 ) baudrate = 460800; |
180 | if(speed == B460800 ) baudrate = 460800; |
| 181 | if(speed == B500000 ) baudrate = 500000; |
181 | if(speed == B500000 ) baudrate = 500000; |
| 182 | if(speed == B576000 ) baudrate = 576000; |
182 | if(speed == B576000 ) baudrate = 576000; |
| 183 | if(speed == B921600 ) baudrate = 921600; |
183 | if(speed == B921600 ) baudrate = 921600; |
| 184 | if(speed == B1000000) baudrate = 1000000; |
184 | if(speed == B1000000) baudrate = 1000000; |
| 185 | if(speed == B1152000) baudrate = 1152000; |
185 | if(speed == B1152000) baudrate = 1152000; |
| 186 | if(speed == B1500000) baudrate = 1500000; |
186 | if(speed == B1500000) baudrate = 1500000; |
| 187 | if(speed == B2000000) baudrate = 2000000; |
187 | if(speed == B2000000) baudrate = 2000000; |
| 188 | if(speed == B2500000) baudrate = 2500000; |
188 | if(speed == B2500000) baudrate = 2500000; |
| 189 | if(speed == B3000000) baudrate = 3000000; |
189 | if(speed == B3000000) baudrate = 3000000; |
| 190 | if(speed == B3500000) baudrate = 3500000; |
190 | if(speed == B3500000) baudrate = 3500000; |
| 191 | if(speed == B4000000) baudrate = 4000000; |
191 | if(speed == B4000000) baudrate = 4000000; |
| 192 | 192 | ||
| 193 | GetCommConfig(hdl,&cc,&ccsize); |
193 | GetCommConfig(hdl,&cc,&ccsize); |
| 194 | //cc.dwSize = sizeof(cc); // size of structure |
194 | //cc.dwSize = sizeof(cc); // size of structure |
| 195 | //cc.wVersion = 1; // version of structure |
195 | //cc.wVersion = 1; // version of structure |
| 196 | //cc.wReserved = 0; // reserved |
196 | //cc.wReserved = 0; // reserved |
| 197 | // DCB dcb; // device-control block |
197 | // DCB dcb; // device-control block |
| 198 | cc.dcb.DCBlength = sizeof(DCB); // sizeof(DCB) |
198 | cc.dcb.DCBlength = sizeof(DCB); // sizeof(DCB) |
| 199 | cc.dcb.BaudRate = baudrate; // current baud rate |
199 | cc.dcb.BaudRate = baudrate; // current baud rate |
| 200 | cc.dcb.fBinary = 1; // binary mode, no EOF check |
200 | cc.dcb.fBinary = 1; // binary mode, no EOF check |
| 201 | cc.dcb.fParity = 1; // enable parity checking |
201 | cc.dcb.fParity = 1; // enable parity checking |
| 202 | cc.dcb.fOutxCtsFlow = 0; // CTS output flow control |
202 | cc.dcb.fOutxCtsFlow = 0; // CTS output flow control |
| 203 | if(rtscts == 1) cc.dcb.fOutxCtsFlow = 1; |
203 | if(rtscts == 1) cc.dcb.fOutxCtsFlow = 1; |
| 204 | cc.dcb.fOutxDsrFlow = 0; // DSR output flow control |
204 | cc.dcb.fOutxDsrFlow = 0; // DSR output flow control |
| 205 | cc.dcb.fDtrControl = DTR_CONTROL_DISABLE; // DTR flow control type |
205 | cc.dcb.fDtrControl = DTR_CONTROL_DISABLE; // DTR flow control type |
| 206 | cc.dcb.fDsrSensitivity = 0; // DSR sensitivity |
206 | cc.dcb.fDsrSensitivity = 0; // DSR sensitivity |
| 207 | cc.dcb.fTXContinueOnXoff = 1; // XOFF continues Tx |
207 | cc.dcb.fTXContinueOnXoff = 1; // XOFF continues Tx |
| 208 | //cc.dcb.fOutX = 0; // XON/XOFF out flow control |
208 | //cc.dcb.fOutX = 0; // XON/XOFF out flow control |
| 209 | //cc.dcb.fInX = 0; // XON/XOFF in flow control |
209 | //cc.dcb.fInX = 0; // XON/XOFF in flow control |
| 210 | //cc.dcb.fErrorChar = 0; // enable error replacement |
210 | //cc.dcb.fErrorChar = 0; // enable error replacement |
| 211 | cc.dcb.fNull = 0; // enable null stripping |
211 | cc.dcb.fNull = 0; // enable null stripping |
| 212 | cc.dcb.fRtsControl = RTS_CONTROL_DISABLE; |
212 | cc.dcb.fRtsControl = RTS_CONTROL_DISABLE; |
| 213 | if(rtscts == 1) cc.dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; // RTS flow control |
213 | if(rtscts == 1) cc.dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; // RTS flow control |
| 214 | cc.dcb.fAbortOnError = 0; // abort reads/writes on error |
214 | cc.dcb.fAbortOnError = 0; // abort reads/writes on error |
| 215 | //cc.dcb.fDummy2 = 0; // reserved |
215 | //cc.dcb.fDummy2 = 0; // reserved |
| 216 | //cc.dcb.wReserved = 0; // not currently used |
216 | //cc.dcb.wReserved = 0; // not currently used |
| 217 | //cc.dcb.XonLim = 0; // transmit XON threshold |
217 | //cc.dcb.XonLim = 0; // transmit XON threshold |
| 218 | //cc.dcb.XoffLim = 0; // transmit XOFF threshold |
218 | //cc.dcb.XoffLim = 0; // transmit XOFF threshold |
| 219 | cc.dcb.ByteSize = bits; // number of bits/byte, 4-8 |
219 | cc.dcb.ByteSize = bits; // number of bits/byte, 4-8 |
| 220 | cc.dcb.Parity = 0; // 0-4=no,odd,even,mark,space |
220 | cc.dcb.Parity = 0; // 0-4=no,odd,even,mark,space |
| 221 | if(parity == rlSerial::ODD) cc.dcb.Parity = 1; |
221 | if(parity == rlSerial::ODD) cc.dcb.Parity = 1; |
| 222 | if(parity == rlSerial::EVEN) cc.dcb.Parity = 2; |
222 | if(parity == rlSerial::EVEN) cc.dcb.Parity = 2; |
| 223 | cc.dcb.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 |
223 | cc.dcb.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 |
| 224 | if(stopbits==2) cc.dcb.StopBits = TWOSTOPBITS; |
224 | if(stopbits==2) cc.dcb.StopBits = TWOSTOPBITS; |
| 225 | //cc.dcb.XonChar = 0; // Tx and Rx XON character |
225 | //cc.dcb.XonChar = 0; // Tx and Rx XON character |
| 226 | //cc.dcb.XoffChar = 0; // Tx and Rx XOFF character |
226 | //cc.dcb.XoffChar = 0; // Tx and Rx XOFF character |
| 227 | //cc.dcb.ErrorChar = 0; // error replacement character |
227 | //cc.dcb.ErrorChar = 0; // error replacement character |
| 228 | //cc.dcb.EofChar = 0; // end of input character |
228 | //cc.dcb.EofChar = 0; // end of input character |
| 229 | //cc.dcb.EvtChar = 0; // received event character |
229 | //cc.dcb.EvtChar = 0; // received event character |
| 230 | //cc.dcb.wReserved1 = 0; // reserved; do not use |
230 | //cc.dcb.wReserved1 = 0; // reserved; do not use |
| 231 | cc.dwProviderSubType = PST_RS232; // type of provider-specific data |
231 | cc.dwProviderSubType = PST_RS232; // type of provider-specific data |
| 232 | //cc.dwProviderOffset = 0; // offset of provider-specific data |
232 | //cc.dwProviderOffset = 0; // offset of provider-specific data |
| 233 | //cc.dwProviderSize = 0; // size of provider-specific data |
233 | //cc.dwProviderSize = 0; // size of provider-specific data |
| 234 | //cc.wcProviderData[0] = 0; // provider-specific data |
234 | //cc.wcProviderData[0] = 0; // provider-specific data |
| 235 | 235 | ||
| 236 | ret = SetCommConfig(hdl,&cc,sizeof(cc)); |
236 | ret = SetCommConfig(hdl,&cc,sizeof(cc)); |
| 237 | if(ret == 0) |
237 | if(ret == 0) |
| 238 | { |
238 | { |
| 239 | printf("SetCommConfig ret=%d devicename=%s LastError=%d\n",ret,devicename,GetLastError()); |
239 | printf("SetCommConfig ret=%d devicename=%s LastError=%d\n",ret,devicename,GetLastError()); |
| 240 | return -1; |
240 | return -1; |
| 241 | } |
241 | } |
| 242 | #endif |
242 | #endif |
| 243 | 243 | ||
| 244 | #ifdef RM3 |
244 | #ifdef RM3 |
| 245 | RmEntryStruct CatEntry; /* Struktur der Deiviceinformationen */ |
245 | RmEntryStruct CatEntry; /* Struktur der Deiviceinformationen */ |
| 246 | int iStatus; /* Rckgabewert */ |
246 | int iStatus; /* Rckgabewert */ |
| 247 | RmIOStatusStruct DrvSts; /* Struktur der Rckgabewerte fr RmIO - Funktion */ |
247 | RmIOStatusStruct DrvSts; /* Struktur der Rckgabewerte fr RmIO - Funktion */ |
| 248 | RmBytParmStruct PBlock; /* Parameterstruktur fr RmIO - Funktion */ |
248 | RmBytParmStruct PBlock; /* Parameterstruktur fr RmIO - Funktion */ |
| 249 | static UCD_BYT_PORT Ucd_byt_drv; /* Struktur zum Setzen der UCD - Werte */ |
249 | static UCD_BYT_PORT Ucd_byt_drv; /* Struktur zum Setzen der UCD - Werte */ |
| 250 | ushort uTimeBd; /* Timing - Wert der �ertragungsgeschwindigkeit */ |
250 | ushort uTimeBd; /* Timing - Wert der �ertragungsgeschwindigkeit */ |
| 251 | uint uMode; /* Portsteuerungsparameter */ |
251 | uint uMode; /* Portsteuerungsparameter */ |
| 252 | unsigned char cByte; /* Byte - Parameter */ |
252 | unsigned char cByte; /* Byte - Parameter */ |
| 253 | /* Timing = 748800 / Baudrate; */ |
253 | /* Timing = 748800 / Baudrate; */ |
| 254 | /**************************************************/ |
254 | /**************************************************/ |
| 255 | char byt_com[32]; |
255 | char byt_com[32]; |
| 256 | 256 | ||
| 257 | /* COM1=0x3F8 COM2=0x2F8 - Port Adresse */ |
257 | /* COM1=0x3F8 COM2=0x2F8 - Port Adresse */ |
| 258 | if (strcmp(devicename,"COM1") == 0) |
258 | if (strcmp(devicename,"COM1") == 0) |
| 259 | { |
259 | { |
| 260 | strcpy(byt_com,"BYT_COM1"); |
260 | strcpy(byt_com,"BYT_COM1"); |
| 261 | com = 0x3f8; |
261 | com = 0x3f8; |
| 262 | } |
262 | } |
| 263 | else if(strcmp(devicename,"COM2") == 0) |
263 | else if(strcmp(devicename,"COM2") == 0) |
| 264 | { |
264 | { |
| 265 | strcpy(byt_com,"BYT_COM2"); |
265 | strcpy(byt_com,"BYT_COM2"); |
| 266 | com = 0x2f8; |
266 | com = 0x2f8; |
| 267 | } |
267 | } |
| 268 | else |
268 | else |
| 269 | { |
269 | { |
| 270 | printf("Error: devicename=%s unknown\n",devicename); |
270 | printf("Error: devicename=%s unknown\n",devicename); |
| 271 | return -1; |
271 | return -1; |
| 272 | } |
272 | } |
| 273 | //printf("Open COM port - inside\n"); |
273 | //printf("Open COM port - inside\n"); |
| 274 | 274 | ||
| 275 | /* |
275 | /* |
| 276 | * Device und Unit - Id auslesen |
276 | * Device und Unit - Id auslesen |
| 277 | */ |
277 | */ |
| 278 | if( RmGetEntry( RM_WAIT, byt_com, &CatEntry ) != RM_OK ) /* RM_CONTINUE */ |
278 | if( RmGetEntry( RM_WAIT, byt_com, &CatEntry ) != RM_OK ) /* RM_CONTINUE */ |
| 279 | { |
279 | { |
| 280 | printf( "Error: %s device not found\n", byt_com); |
280 | printf( "Error: %s device not found\n", byt_com); |
| 281 | return -1; |
281 | return -1; |
| 282 | } |
282 | } |
| 283 | 283 | ||
| 284 | device = (int) ((ushort) CatEntry.ide); |
284 | device = (int) ((ushort) CatEntry.ide); |
| 285 | unit = (int) CatEntry.id; |
285 | unit = (int) CatEntry.id; |
| 286 | 286 | ||
| 287 | /* |
287 | /* |
| 288 | * Ger� reservieren |
288 | * Ger� reservieren |
| 289 | */ |
289 | */ |
| 290 | if( RmIO( BYT_RESERVE, (unsigned)(device), (unsigned)(unit), 0u, 0u, &DrvSts, &PBlock ) < 0 ) |
290 | if( RmIO( BYT_RESERVE, (unsigned)(device), (unsigned)(unit), 0u, 0u, &DrvSts, &PBlock ) < 0 ) |
| 291 | { |
291 | { |
| 292 | printf( "Error: Unable to reserve %s device\n", byt_com); |
292 | printf( "Error: Unable to reserve %s device\n", byt_com); |
| 293 | return -1; |
293 | return -1; |
| 294 | } |
294 | } |
| 295 | 295 | ||
| 296 | /* |
296 | /* |
| 297 | * Baudrate ausrechnen |
297 | * Baudrate ausrechnen |
| 298 | */ |
298 | */ |
| 299 | baudrate = 9600; |
299 | baudrate = 9600; |
| 300 | if(speed == B50 ) baudrate = 50; |
300 | if(speed == B50 ) baudrate = 50; |
| 301 | if(speed == B75 ) baudrate = 75; |
301 | if(speed == B75 ) baudrate = 75; |
| 302 | if(speed == B110 ) baudrate = 110; |
302 | if(speed == B110 ) baudrate = 110; |
| 303 | if(speed == B134 ) baudrate = 134; |
303 | if(speed == B134 ) baudrate = 134; |
| 304 | if(speed == B150 ) baudrate = 150; |
304 | if(speed == B150 ) baudrate = 150; |
| 305 | if(speed == B200 ) baudrate = 200; |
305 | if(speed == B200 ) baudrate = 200; |
| 306 | if(speed == B300 ) baudrate = 300; |
306 | if(speed == B300 ) baudrate = 300; |
| 307 | if(speed == B600 ) baudrate = 600; |
307 | if(speed == B600 ) baudrate = 600; |
| 308 | if(speed == B1200 ) baudrate = 1200; |
308 | if(speed == B1200 ) baudrate = 1200; |
| 309 | if(speed == B1800 ) baudrate = 1800; |
309 | if(speed == B1800 ) baudrate = 1800; |
| 310 | if(speed == B2400 ) baudrate = 2400; |
310 | if(speed == B2400 ) baudrate = 2400; |
| 311 | if(speed == B4800 ) baudrate = 4800; |
311 | if(speed == B4800 ) baudrate = 4800; |
| 312 | if(speed == B9600 ) baudrate = 9600; |
312 | if(speed == B9600 ) baudrate = 9600; |
| 313 | if(speed == B19200 ) baudrate = 19200; |
313 | if(speed == B19200 ) baudrate = 19200; |
| 314 | if(speed == B38400 ) baudrate = 38400; |
314 | if(speed == B38400 ) baudrate = 38400; |
| 315 | if(speed == B57600 ) baudrate = 57600; |
315 | if(speed == B57600 ) baudrate = 57600; |
| 316 | if(speed == B115200 ) baudrate = 115200; |
316 | if(speed == B115200 ) baudrate = 115200; |
| 317 | if(speed == B230400 ) baudrate = 230400; |
317 | if(speed == B230400 ) baudrate = 230400; |
| 318 | if(speed == B460800 ) baudrate = 460800; |
318 | if(speed == B460800 ) baudrate = 460800; |
| 319 | if(speed == B500000 ) baudrate = 500000; |
319 | if(speed == B500000 ) baudrate = 500000; |
| 320 | if(speed == B576000 ) baudrate = 576000; |
320 | if(speed == B576000 ) baudrate = 576000; |
| 321 | if(speed == B921600 ) baudrate = 921600; |
321 | if(speed == B921600 ) baudrate = 921600; |
| 322 | if(speed == B1000000) baudrate = 1000000; |
322 | if(speed == B1000000) baudrate = 1000000; |
| 323 | if(speed == B1152000) baudrate = 1152000; |
323 | if(speed == B1152000) baudrate = 1152000; |
| 324 | if(speed == B1500000) baudrate = 1500000; |
324 | if(speed == B1500000) baudrate = 1500000; |
| 325 | if(speed == B2000000) baudrate = 2000000; |
325 | if(speed == B2000000) baudrate = 2000000; |
| 326 | if(speed == B2500000) baudrate = 2500000; |
326 | if(speed == B2500000) baudrate = 2500000; |
| 327 | if(speed == B3000000) baudrate = 3000000; |
327 | if(speed == B3000000) baudrate = 3000000; |
| 328 | if(speed == B3500000) baudrate = 3500000; |
328 | if(speed == B3500000) baudrate = 3500000; |
| 329 | if(speed == B4000000) baudrate = 4000000; |
329 | if(speed == B4000000) baudrate = 4000000; |
| 330 | uTimeBd = 748800 / baudrate; |
330 | uTimeBd = 748800 / baudrate; |
| 331 | 331 | ||
| 332 | /* |
332 | /* |
| 333 | * Portsteuerungsparameter setzen |
333 | * Portsteuerungsparameter setzen |
| 334 | */ |
334 | */ |
| 335 | uMode = 0x1000 | DATA_8 | STOP_1 | NOPARITY; |
335 | uMode = 0x1000 | DATA_8 | STOP_1 | NOPARITY; |
| 336 | 336 | ||
| 337 | /* |
337 | /* |
| 338 | * UCD des seriellen Ports auslesen |
338 | * UCD des seriellen Ports auslesen |
| 339 | */ |
339 | */ |
| 340 | PBlock.string = 0; |
340 | PBlock.string = 0; |
| 341 | PBlock.strlen = 0; |
341 | PBlock.strlen = 0; |
| 342 | PBlock.buffer = (char *)&Ucd_byt_drv; |
342 | PBlock.buffer = (char *)&Ucd_byt_drv; |
| 343 | PBlock.timlen = sizeof(UCD_BYT_PORT); |
343 | PBlock.timlen = sizeof(UCD_BYT_PORT); |
| 344 | PBlock.status = 0; |
344 | PBlock.status = 0; |
| 345 | 345 | ||
| 346 | iStatus = RmIO( BYT_CREATE_NEW, (unsigned)(device), (unsigned)(unit), 0u, 0u, &DrvSts, &PBlock ); |
346 | iStatus = RmIO( BYT_CREATE_NEW, (unsigned)(device), (unsigned)(unit), 0u, 0u, &DrvSts, &PBlock ); |
| 347 | 347 | ||
| 348 | /* |
348 | /* |
| 349 | * Modus �dern |
349 | * Modus �dern |
| 350 | */ |
350 | */ |
| 351 | Ucd_byt_drv.mobyte[5] |= (ushort) (uMode & 0xFFu); |
351 | Ucd_byt_drv.mobyte[5] |= (ushort) (uMode & 0xFFu); |
| 352 | 352 | ||
| 353 | /* |
353 | /* |
| 354 | * Timeout setzen |
354 | * Timeout setzen |
| 355 | */ |
355 | */ |
| 356 | Ucd_byt_drv.header.timout = timeout; |
356 | Ucd_byt_drv.header.timout = timeout; |
| 357 | 357 | ||
| 358 | /* |
358 | /* |
| 359 | * Werte zuweisen |
359 | * Werte zuweisen |
| 360 | */ |
360 | */ |
| 361 | PBlock.string = (char*) &Ucd_byt_drv; |
361 | PBlock.string = (char*) &Ucd_byt_drv; |
| 362 | PBlock.strlen = sizeof(UCD_BYT_PORT); |
362 | PBlock.strlen = sizeof(UCD_BYT_PORT); |
| 363 | PBlock.buffer = 0; |
363 | PBlock.buffer = 0; |
| 364 | PBlock.timlen = 0; |
364 | PBlock.timlen = 0; |
| 365 | PBlock.status = 0; |
365 | PBlock.status = 0; |
| 366 | 366 | ||
| 367 | iStatus = RmIO( BYT_CREATE_NEW, (unsigned)(device), (unsigned)(unit), 0u, 0u, &DrvSts, &PBlock ); |
367 | iStatus = RmIO( BYT_CREATE_NEW, (unsigned)(device), (unsigned)(unit), 0u, 0u, &DrvSts, &PBlock ); |
| 368 | 368 | ||
| 369 | /* |
369 | /* |
| 370 | * Register 0 und 1 zum Schreiben freigeben |
370 | * Register 0 und 1 zum Schreiben freigeben |
| 371 | */ |
371 | */ |
| 372 | cByte = inbyte( com + 0x03 ); |
372 | cByte = inbyte( com + 0x03 ); |
| 373 | outbyte( com + 0x03, (unsigned char)(cByte | 0x80) ); |
373 | outbyte( com + 0x03, (unsigned char)(cByte | 0x80) ); |
| 374 | 374 | ||
| 375 | /* |
375 | /* |
| 376 | * Baudrate setzen |
376 | * Baudrate setzen |
| 377 | */ |
377 | */ |
| 378 | outbyte( com + 0x00, (ushort) LOW (uTimeBd) ); |
378 | outbyte( com + 0x00, (ushort) LOW (uTimeBd) ); |
| 379 | outbyte( com + 0x01, (ushort) HIGH (uTimeBd) ); |
379 | outbyte( com + 0x01, (ushort) HIGH (uTimeBd) ); |
| 380 | 380 | ||
| 381 | /* |
381 | /* |
| 382 | * Register 0 und 1 sperren |
382 | * Register 0 und 1 sperren |
| 383 | */ |
383 | */ |
| 384 | outbyte( com + 0x03, cByte ); |
384 | outbyte( com + 0x03, cByte ); |
| 385 | 385 | ||
| 386 | if( iStatus ) printf( "BYT_CREATE_NEW (set ucb): Error status = %X\n", iStatus ); |
386 | if( iStatus ) printf( "BYT_CREATE_NEW (set ucb): Error status = %X\n", iStatus ); |
| 387 | #endif |
387 | #endif |
| 388 | 388 | ||
| 389 | return 0; |
389 | return 0; |
| 390 | } |
390 | } |
| 391 | 391 | ||
| 392 | int rlSerial::readChar() |
392 | int rlSerial::readChar() |
| 393 | { |
393 | { |
| 394 | #ifdef unix |
394 | #ifdef unix |
| 395 | int ret; |
395 | int ret; |
| 396 | unsigned char buf[2]; |
396 | unsigned char buf[2]; |
| 397 | 397 | ||
| 398 | if(fd == -1) return -1; |
398 | if(fd == -1) return -1; |
| 399 | ret = read(fd,buf,1); |
399 | ret = read(fd,buf,1); |
| 400 | if(ret == 1) return buf[0]; |
400 | if(ret == 1) return buf[0]; |
| 401 | if(ret == 0) return -2; |
401 | if(ret == 0) return -2; |
| 402 | return -1; |
402 | return -1; |
| 403 | #endif |
403 | #endif |
| 404 | 404 | ||
| 405 | #ifdef __VMS |
405 | #ifdef __VMS |
| 406 | unsigned char buf[2]; |
406 | unsigned char buf[2]; |
| 407 | 407 | ||
| 408 | if(readBlock(buf, 1) < 0) return -1; |
408 | if(readBlock(buf, 1) < 0) return -1; |
| 409 | return buf[0]; |
409 | return buf[0]; |
| 410 | #endif |
410 | #endif |
| 411 | 411 | ||
| 412 | #ifdef _WIN32 |
412 | #ifdef _WIN32 |
| 413 | BOOL ret; |
413 | BOOL ret; |
| 414 | unsigned char buf[2]; |
414 | unsigned char buf[2]; |
| 415 | unsigned long len; |
415 | unsigned long len; |
| 416 | 416 | ||
| 417 | ret = ReadFile( |
417 | ret = ReadFile( |
| 418 | hdl, // handle of file to read |
418 | hdl, // handle of file to read |
| 419 | buf, // pointer to buffer that receives data |
419 | buf, // pointer to buffer that receives data |
| 420 | 1, // number of bytes to read |
420 | 1, // number of bytes to read |
| 421 | &len, // pointer to number of bytes read |
421 | &len, // pointer to number of bytes read |
| 422 | NULL // pointer to structure for data |
422 | NULL // pointer to structure for data |
| 423 | ); |
423 | ); |
| 424 | if(len > 0) |
424 | if(len > 0) |
| 425 | { |
425 | { |
| 426 | if(trace == 1) printf("readChar %d\n",(int) buf[0]); |
426 | if(trace == 1) printf("readChar %d\n",(int) buf[0]); |
| 427 | return buf[0]; |
427 | return buf[0]; |
| 428 | } |
428 | } |
| 429 | return -1; |
429 | return -1; |
| 430 | #endif |
430 | #endif |
| 431 | 431 | ||
| 432 | #ifdef RM3 |
432 | #ifdef RM3 |
| 433 | int ret; |
433 | int ret; |
| 434 | unsigned char buf[2]; |
434 | unsigned char buf[2]; |
| 435 | 435 | ||
| 436 | ret = readBlock(buf,1); |
436 | ret = readBlock(buf,1); |
| 437 | if(ret < 0) return ret; |
437 | if(ret < 0) return ret; |
| 438 | return buf[0]; |
438 | return buf[0]; |
| 439 | #endif |
439 | #endif |
| 440 | } |
440 | } |
| 441 | 441 | ||
| 442 | int rlSerial::writeChar(unsigned char uchar) |
442 | int rlSerial::writeChar(unsigned char uchar) |
| 443 | { |
443 | { |
| 444 | #ifdef unix |
444 | #ifdef unix |
| 445 | int ret; |
445 | int ret; |
| 446 | if(fd == -1) return -1; |
446 | if(fd == -1) return -1; |
| 447 | if(trace == 1) printf("writeChar %d\n",(int)uchar); |
447 | if(trace == 1) printf("writeChar %d\n",(int)uchar); |
| 448 | ret = write(fd,&uchar,1); |
448 | ret = write(fd,&uchar,1); |
| 449 | if(ret < 0) return -1; |
449 | if(ret < 0) return -1; |
| 450 | //tcflush(fd, TCIOFLUSH); |
450 | //tcflush(fd, TCIOFLUSH); |
| 451 | return ret; |
451 | return ret; |
| 452 | #endif |
452 | #endif |
| 453 | 453 | ||
| 454 | #ifdef __VMS |
454 | #ifdef __VMS |
| 455 | int status; |
455 | int status; |
| 456 | IOSB iosb; |
456 | IOSB iosb; |
| 457 | 457 | ||
| 458 | status = SYS$QIOW(0,vms_channel,IO$_WRITEVBLK | IO$M_CANCTRLO | IO$M_NOFORMAT, |
458 | status = SYS$QIOW(0,vms_channel,IO$_WRITEVBLK | IO$M_CANCTRLO | IO$M_NOFORMAT, |
| 459 | &iosb,0,0,&uchar,1,0,0,0,0); |
459 | &iosb,0,0,&uchar,1,0,0,0,0); |
| 460 | if(status != SS$_NORMAL) return -1; |
460 | if(status != SS$_NORMAL) return -1; |
| 461 | return 1; |
461 | return 1; |
| 462 | #endif |
462 | #endif |
| 463 | 463 | ||
| 464 | #ifdef _WIN32 |
464 | #ifdef _WIN32 |
| 465 | BOOL ret; |
465 | BOOL ret; |
| 466 | unsigned long len; |
466 | unsigned long len; |
| 467 | 467 | ||
| 468 | if(trace == 1) printf("writeChar %d\n",(int)uchar); |
468 | if(trace == 1) printf("writeChar %d\n",(int)uchar); |
| 469 | ret = WriteFile( |
469 | ret = WriteFile( |
| 470 | hdl, // handle to file to write to |
470 | hdl, // handle to file to write to |
| 471 | &uchar, // pointer to data to write to file |
471 | &uchar, // pointer to data to write to file |
| 472 | 1, // number of bytes to write |
472 | 1, // number of bytes to write |
| 473 | &len, // pointer to number of bytes written |
473 | &len, // pointer to number of bytes written |
| 474 | NULL // pointer to structure for overlapped I/O |
474 | NULL // pointer to structure for overlapped I/O |
| 475 | ); |
475 | ); |
| 476 | 476 | ||
| 477 | if(ret) return (int) len; |
477 | if(ret) return (int) len; |
| 478 | return -1; |
478 | return -1; |
| 479 | #endif |
479 | #endif |
| 480 | 480 | ||
| 481 | #ifdef RM3 |
481 | #ifdef RM3 |
| 482 | return writeBlock(&uchar, 1); |
482 | return writeBlock(&uchar, 1); |
| 483 | #endif |
483 | #endif |
| 484 | } |
484 | } |
| 485 | 485 | ||
| 486 | int rlSerial::readBlock(unsigned char *buf, int len) |
486 | int rlSerial::readBlock(unsigned char *buf, int len) |
| 487 | { |
487 | { |
| 488 | #ifdef unix |
488 | #ifdef unix |
| 489 | int c; |
489 | int c; |
| 490 | 490 | ||
| 491 | for(int i=0; i<len; i++) |
491 | for(int i=0; i<len; i++) |
| 492 | { |
492 | { |
| 493 | c = readChar(); |
493 | c = readChar(); |
| 494 | if(c < 0) return c; |
494 | if(c < 0) return c; |
| 495 | buf[i] = (unsigned char) c; |
495 | buf[i] = (unsigned char) c; |
| 496 | } |
496 | } |
| 497 | return len; |
497 | return len; |
| 498 | #endif |
498 | #endif |
| 499 | 499 | ||
| 500 | #ifdef __VMS |
500 | #ifdef __VMS |
| 501 | int status; |
501 | int status; |
| 502 | int timeout = 1; // second |
502 | int timeout = 1; // second |
| 503 | short iosb[4]; |
503 | short iosb[4]; |
| 504 | 504 | ||
| 505 | status = SYS$QIOW(0,vms_channel, |
505 | status = SYS$QIOW(0,vms_channel, |
| 506 | IO$_READVBLK | IO$M_NOFILTR | IO$M_NOECHO | IO$M_TIMED, |
506 | IO$_READVBLK | IO$M_NOFILTR | IO$M_NOECHO | IO$M_TIMED, |
| 507 | iosb,0,0,buf,len,timeout,0,0,0); |
507 | iosb,0,0,buf,len,timeout,0,0,0); |
| 508 | if(status != SS$_NORMAL) return -1; |
508 | if(status != SS$_NORMAL) return -1; |
| 509 | len=iosb[1]; |
509 | len=iosb[1]; |
| 510 | if(iosb[2] != 0) |
510 | if(iosb[2] != 0) |
| 511 | { |
511 | { |
| 512 | len++; |
512 | len++; |
| 513 | buf[len] = iosb[2]; |
513 | buf[len] = iosb[2]; |
| 514 | } |
514 | } |
| 515 | return len; |
515 | return len; |
| 516 | #endif |
516 | #endif |
| 517 | 517 | ||
| 518 | #ifdef _WIN32 |
518 | #ifdef _WIN32 |
| 519 | BOOL ret; |
519 | BOOL ret; |
| 520 | unsigned long retlen; |
520 | unsigned long retlen; |
| 521 | 521 | ||
| 522 | ret = ReadFile( |
522 | ret = ReadFile( |
| 523 | hdl, // handle of file to read |
523 | hdl, // handle of file to read |
| 524 | buf, // pointer to buffer that receives data |
524 | buf, // pointer to buffer that receives data |
| 525 | len, // number of bytes to read |
525 | len, // number of bytes to read |
| 526 | &retlen, // pointer to number of bytes read |
526 | &retlen, // pointer to number of bytes read |
| 527 | NULL // pointer to structure for data |
527 | NULL // pointer to structure for data |
| 528 | ); |
528 | ); |
| 529 | if(retlen > 0) |
529 | if(retlen > 0) |
| 530 | { |
530 | { |
| 531 | if(trace == 1) printf("readBlock retlen=%d\n",retlen); |
531 | if(trace == 1) printf("readBlock retlen=%d\n",retlen); |
| 532 | return (int) retlen; |
532 | return (int) retlen; |
| 533 | } |
533 | } |
| 534 | return -1; |
534 | return -1; |
| 535 | #endif |
535 | #endif |
| 536 | 536 | ||
| 537 | #ifdef RM3 |
537 | #ifdef RM3 |
| 538 | RmBytParmStruct PBlock; /* Parameterstruktur fr RmIO - Funktion */ |
538 | RmBytParmStruct PBlock; /* Parameterstruktur fr RmIO - Funktion */ |
| 539 | RmIOStatusStruct DrvSts; /* Struktur der Rckgabewerte fr RmIO - Funktion */ |
539 | RmIOStatusStruct DrvSts; /* Struktur der Rckgabewerte fr RmIO - Funktion */ |
| 540 | int iStatus; /* Rckgabewert */ |
540 | int iStatus; /* Rckgabewert */ |
| 541 | int i; /* Schleifenz�ler */ |
541 | int i; /* Schleifenz�ler */ |
| 542 | /**************************************************/ |
542 | /**************************************************/ |
| 543 | 543 | ||
| 544 | /* |
544 | /* |
| 545 | * Schreibparameter setzen |
545 | * Schreibparameter setzen |
| 546 | */ |
546 | */ |
| 547 | PBlock.string = 0; |
547 | PBlock.string = 0; |
| 548 | PBlock.strlen = 0; |
548 | PBlock.strlen = 0; |
| 549 | PBlock.buffer = (char*)buf; |
549 | PBlock.buffer = (char*)buf; |
| 550 | PBlock.timlen = len; |
550 | PBlock.timlen = len; |
| 551 | PBlock.status = 0; |
551 | PBlock.status = 0; |
| 552 | 552 | ||
| 553 | /* |
553 | /* |
| 554 | * Lesevorgang einleiten |
554 | * Lesevorgang einleiten |
| 555 | */ |
555 | */ |
| 556 | iStatus = RmIO( BYT_POLL_XBUF_WAIT, (unsigned)device, (unsigned)unit, 0u, 0u, &DrvSts, &PBlock ); |
556 | iStatus = RmIO( BYT_POLL_XBUF_WAIT, (unsigned)device, (unsigned)unit, 0u, 0u, &DrvSts, &PBlock ); |
| 557 | 557 | ||
| 558 | if( iStatus ) printf( "BYT_POLL_XBUF_WAIT (set ucb): Error status = %X\n", iStatus ); |
558 | if( iStatus ) printf( "BYT_POLL_XBUF_WAIT (set ucb): Error status = %X\n", iStatus ); |
| 559 | 559 | ||
| 560 | if( !iStatus ) return len; |
560 | if( !iStatus ) return len; |
| 561 | return -1; |
561 | return -1; |
| 562 | #endif |
562 | #endif |
| 563 | } |
563 | } |
| 564 | 564 | ||
| 565 | int rlSerial::writeBlock(const unsigned char *buf, int len) |
565 | int rlSerial::writeBlock(const unsigned char *buf, int len) |
| 566 | { |
566 | { |
| 567 | #ifdef unix |
567 | #ifdef unix |
| 568 | int ret; |
568 | int ret; |
| 569 | 569 | ||
| 570 | if(fd == -1) return -1; |
570 | if(fd == -1) return -1; |
| 571 | if(trace == 1) |
571 | if(trace == 1) |
| 572 | { |
572 | { |
| 573 | printf("writeBlock:"); |
573 | printf("writeBlock:"); |
| 574 | for(int i=0; i<len; i++) printf(" %d",(int) buf[i]); |
574 | for(int i=0; i<len; i++) printf(" %d",(int) buf[i]); |
| 575 | printf("\n"); |
575 | printf("\n"); |
| 576 | } |
576 | } |
| 577 | ret = write(fd,buf,len); |
577 | ret = write(fd,buf,len); |
| 578 | //tcflush(fd, TCIOFLUSH); |
578 | //tcflush(fd, TCIOFLUSH); |
| 579 | return ret; |
579 | return ret; |
| 580 | #endif |
580 | #endif |
| 581 | 581 | ||
| 582 | #ifdef __VMS |
582 | #ifdef __VMS |
| 583 | int status; |
583 | int status; |
| 584 | IOSB iosb; |
584 | IOSB iosb; |
| 585 | 585 | ||
| 586 | 586 | ||
| 587 | status = SYS$QIOW(0,vms_channel,IO$_WRITEVBLK | IO$M_CANCTRLO | IO$M_NOFORMAT, |
587 | status = SYS$QIOW(0,vms_channel,IO$_WRITEVBLK | IO$M_CANCTRLO | IO$M_NOFORMAT, |
| 588 | &iosb,0,0,buf,len,0,0,0,0); |
588 | &iosb,0,0,buf,len,0,0,0,0); |
| 589 | if(status != SS$_NORMAL) return -1; |
589 | if(status != SS$_NORMAL) return -1; |
| 590 | return len; |
590 | return len; |
| 591 | #endif |
591 | #endif |
| 592 | 592 | ||
| 593 | #ifdef _WIN32 |
593 | #ifdef _WIN32 |
| 594 | BOOL ret; |
594 | BOOL ret; |
| 595 | unsigned long retlen; |
595 | unsigned long retlen; |
| 596 | 596 | ||
| 597 | if(trace == 1) |
597 | if(trace == 1) |
| 598 | { |
598 | { |
| 599 | printf("writeBlock:"); |
599 | printf("writeBlock:"); |
| 600 | for(int i=0; i<len; i++) printf(" %d",(int) buf[i]); |
600 | for(int i=0; i<len; i++) printf(" %d",(int) buf[i]); |
| 601 | printf("\n"); |
601 | printf("\n"); |
| 602 | } |
602 | } |
| 603 | retlen = len; |
603 | retlen = len; |
| 604 | ret = WriteFile( |
604 | ret = WriteFile( |
| 605 | hdl, // handle to file to write to |
605 | hdl, // handle to file to write to |
| 606 | buf, // pointer to data to write to file |
606 | buf, // pointer to data to write to file |
| 607 | len, // number of bytes to write |
607 | len, // number of bytes to write |
| 608 | &retlen, // pointer to number of bytes written |
608 | &retlen, // pointer to number of bytes written |
| 609 | NULL // pointer to structure for overlapped I/O |
609 | NULL // pointer to structure for overlapped I/O |
| 610 | ); |
610 | ); |
| 611 | 611 | ||
| 612 | if(ret) return (int) retlen; |
612 | if(ret) return (int) retlen; |
| 613 | return -1; |
613 | return -1; |
| 614 | #endif |
614 | #endif |
| 615 | 615 | ||
| 616 | #ifdef RM3 |
616 | #ifdef RM3 |
| 617 | RmBytParmStruct PBlock; /* Parameterstruktur fr RmIO - Funktion */ |
617 | RmBytParmStruct PBlock; /* Parameterstruktur fr RmIO - Funktion */ |
| 618 | RmIOStatusStruct DrvSts; /* Struktur der Rckgabewerte fr RmIO - Funktion */ |
618 | RmIOStatusStruct DrvSts; /* Struktur der Rckgabewerte fr RmIO - Funktion */ |
| 619 | unsigned char cByte; /* Rckgabewert von ibyte - Funktion */ |
619 | unsigned char cByte; /* Rckgabewert von ibyte - Funktion */ |
| 620 | int iStatus; /* Rckgabewert */ |
620 | int iStatus; /* Rckgabewert */ |
| 621 | int i; /* Schleifenz�ler */ |
621 | int i; /* Schleifenz�ler */ |
| 622 | /**************************************************/ |
622 | /**************************************************/ |
| 623 | 623 | ||
| 624 | /* |
624 | /* |
| 625 | * Schreibparameter setzen |
625 | * Schreibparameter setzen |
| 626 | */ |
626 | */ |
| 627 | PBlock.string = (char*)buf; |
627 | PBlock.string = (char*)buf; |
| 628 | PBlock.strlen = len; |
628 | PBlock.strlen = len; |
| 629 | PBlock.buffer = 0; |
629 | PBlock.buffer = 0; |
| 630 | PBlock.timlen = 0; |
630 | PBlock.timlen = 0; |
| 631 | PBlock.status = 0; |
631 | PBlock.status = 0; |
| 632 | 632 | ||
| 633 | /****************************************************** |
633 | /****************************************************** |
| 634 | * * |
634 | * * |
| 635 | * Toggle mode wird emuliert indem an dieser Stelle * |
635 | * Toggle mode wird emuliert indem an dieser Stelle * |
| 636 | * RTS-Signal gesetzt und sp�er wieder gel�cht wird * |
636 | * RTS-Signal gesetzt und sp�er wieder gel�cht wird * |
| 637 | * * |
637 | * * |
| 638 | ******************************************************/ |
638 | ******************************************************/ |
| 639 | 639 | ||
| 640 | cByte = inbyte( com + 0x04u ); |
640 | cByte = inbyte( com + 0x04u ); |
| 641 | outbyte( com + 0x04, (unsigned char)(cByte | 0x02u) ); |
641 | outbyte( com + 0x04, (unsigned char)(cByte | 0x02u) ); |
| 642 | 642 | ||
| 643 | /* |
643 | /* |
| 644 | * Schreibvorgang einleiten |
644 | * Schreibvorgang einleiten |
| 645 | */ |
645 | */ |
| 646 | iStatus = RmIO( BYT_WRITE_WAIT, (unsigned)device, (unsigned)unit, 0u, 0u, &DrvSts, &PBlock ); |
646 | iStatus = RmIO( BYT_WRITE_WAIT, (unsigned)device, (unsigned)unit, 0u, 0u, &DrvSts, &PBlock ); |
| 647 | 647 | ||
| 648 | /****************************************************************** |
648 | /****************************************************************** |
| 649 | * * |
649 | * * |
| 650 | * 8ms warten.Bei der Aenderung der Uebertragungsgeschwindigkeit, * |
650 | * 8ms warten.Bei der Aenderung der Uebertragungsgeschwindigkeit, * |
| 651 | * sollte dieser Wert angepasst werden. Hier fuer 9600 * |
651 | * sollte dieser Wert angepasst werden. Hier fuer 9600 * |
| 652 | * * |
652 | * * |
| 653 | ******************************************************************/ |
653 | ******************************************************************/ |
| 654 | RmPauseTask((RTS_TIME_WAIT*9600)/baudrate); |
654 | RmPauseTask((RTS_TIME_WAIT*9600)/baudrate); |
| 655 | 655 | ||
| 656 | /* |
656 | /* |
| 657 | * RTS-Signal l�chen |
657 | * RTS-Signal l�chen |
| 658 | */ |
658 | */ |
| 659 | outbyte( com + 0x04, (unsigned char)(cByte & 0xFDu) ); |
659 | outbyte( com + 0x04, (unsigned char)(cByte & 0xFDu) ); |
| 660 | 660 | ||
| 661 | if( iStatus ) printf( "BYT_WRITE_WAIT (write block): Error status = %X\n", iStatus ); |
661 | if( iStatus ) printf( "BYT_WRITE_WAIT (write block): Error status = %X\n", iStatus ); |
| 662 | 662 | ||
| 663 | if( !iStatus ) return len; |
663 | if( !iStatus ) return len; |
| 664 | return -1; |
664 | return -1; |
| 665 | #endif |
665 | #endif |
| 666 | } |
666 | } |
| 667 | 667 | ||
| 668 | int rlSerial::readLine(unsigned char *buf, int maxlen) |
668 | int rlSerial::readLine(unsigned char *buf, int maxlen) |
| 669 | { |
669 | { |
| 670 | int i,c,ret; |
670 | int i,c,ret; |
| 671 | 671 | ||
| 672 | if(maxlen <= 1) return -1; |
672 | if(maxlen <= 1) return -1; |
| 673 | ret = 0; |
673 | ret = 0; |
| 674 | buf[maxlen-1] = '\0'; |
674 | buf[maxlen-1] = '\0'; |
| 675 | for(i=0; i<maxlen-2; i++) |
675 | for(i=0; i<maxlen-2; i++) |
| 676 | { |
676 | { |
| 677 | ret = i; |
677 | ret = i; |
| 678 | c = readChar(); |
678 | c = readChar(); |
| 679 | if(c < 0) |
679 | if(c < 0) |
| 680 | { |
680 | { |
| 681 | buf[i] = '\0'; |
681 | buf[i] = '\0'; |
| 682 | ret = c; |
682 | ret = c; |
| 683 | break; |
683 | break; |
| 684 | } |
684 | } |
| 685 | buf[i] = (unsigned char) c; |
685 | buf[i] = (unsigned char) c; |
| 686 | if(c < ' ') |
686 | if(c < ' ') |
| 687 | { |
687 | { |
| 688 | buf[i+1] = '\0'; |
688 | buf[i+1] = '\0'; |
| 689 | break; |
689 | break; |
| 690 | } |
690 | } |
| 691 | } |
691 | } |
| 692 | return ret; |
692 | return ret; |
| 693 | } |
693 | } |
| 694 | 694 | ||
| 695 | int rlSerial::select(int timeout) |
695 | int rlSerial::select(int timeout) |
| 696 | { |
696 | { |
| 697 | #ifdef unix |
697 | #ifdef unix |
| 698 | struct timeval timout; |
698 | struct timeval timout; |
| 699 | fd_set wset,rset,eset; |
699 | fd_set wset,rset,eset; |
| 700 | int ret,maxfdp1; |
700 | int ret,maxfdp1; |
| 701 | 701 | ||
| 702 | if(timeout <= 0) return 1; |
702 | if(timeout <= 0) return 1; |
| 703 | /* setup sockets to read */ |
703 | /* setup sockets to read */ |
| 704 | maxfdp1 = fd+1; |
704 | maxfdp1 = fd+1; |
| 705 | FD_ZERO(&rset); |
705 | FD_ZERO(&rset); |
| 706 | FD_SET (fd,&rset); |
706 | FD_SET (fd,&rset); |
| 707 | FD_ZERO(&wset); |
707 | FD_ZERO(&wset); |
| 708 | FD_ZERO(&eset); |
708 | FD_ZERO(&eset); |
| 709 | timout.tv_sec = timeout / 1000; |
709 | timout.tv_sec = timeout / 1000; |
| 710 | timout.tv_usec = timeout % 1000; |
710 | timout.tv_usec = timeout % 1000; |
| 711 | 711 | ||
| 712 | ret = ::select(maxfdp1,&rset,&wset,&eset,&timout); |
712 | ret = ::select(maxfdp1,&rset,&wset,&eset,&timout); |
| 713 | if(ret == 0) return 0; /* timeout */ |
713 | if(ret == 0) return 0; /* timeout */ |
| 714 | return 1; |
714 | return 1; |
| 715 | #endif |
715 | #endif |
| 716 | 716 | ||
| 717 | #ifdef __VMS |
717 | #ifdef __VMS |
| 718 | return 1; |
718 | return 1; |
| 719 | #endif |
719 | #endif |
| 720 | 720 | ||
| 721 | #ifdef _WIN32 |
721 | #ifdef _WIN32 |
| 722 | COMMTIMEOUTS ctimeout; |
722 | COMMTIMEOUTS ctimeout; |
| 723 | 723 | ||
| 724 | ctimeout.ReadIntervalTimeout = timeout; |
724 | ctimeout.ReadIntervalTimeout = timeout; |
| 725 | ctimeout.ReadTotalTimeoutMultiplier = 1; |
725 | ctimeout.ReadTotalTimeoutMultiplier = 1; |
| 726 | ctimeout.ReadTotalTimeoutConstant = timeout; |
726 | ctimeout.ReadTotalTimeoutConstant = timeout; |
| 727 | ctimeout.WriteTotalTimeoutMultiplier = 1; |
727 | ctimeout.WriteTotalTimeoutMultiplier = 1; |
| 728 | ctimeout.WriteTotalTimeoutConstant = timeout; |
728 | ctimeout.WriteTotalTimeoutConstant = timeout; |
| 729 | 729 | ||
| 730 | SetCommTimeouts(hdl, &ctimeout); |
730 | SetCommTimeouts(hdl, &ctimeout); |
| 731 | return 1; |
731 | return 1; |
| 732 | #endif |
732 | #endif |
| 733 | 733 | ||
| 734 | #ifdef RM3 |
734 | #ifdef RM3 |
| 735 | return 1; |
735 | return 1; |
| 736 | #endif |
736 | #endif |
| 737 | } |
737 | } |
| 738 | 738 | ||
| 739 | int rlSerial::closeDevice() |
739 | int rlSerial::closeDevice() |
| 740 | { |
740 | { |
| 741 | #ifdef unix |
741 | #ifdef unix |
| 742 | if(fd == -1) return -1; |
742 | if(fd == -1) return -1; |
| 743 | //if(::tcsetattr(fd,TCSAFLUSH,&save_termios) < 0) return -1; |
743 | //if(::tcsetattr(fd,TCSAFLUSH,&save_termios) < 0) return -1; |
| 744 | if(::tcsetattr(fd,TCSANOW,&save_termios) < 0) return -1; |
744 | if(::tcsetattr(fd,TCSANOW,&save_termios) < 0) return -1; |
| 745 | ::close(fd); |
745 | ::close(fd); |
| 746 | ttystate = RESET; |
746 | ttystate = RESET; |
| 747 | fd = -1; |
747 | fd = -1; |
| 748 | return 0; |
748 | return 0; |
| 749 | #endif |
749 | #endif |
| 750 | 750 | ||
| 751 | #ifdef __VMS |
751 | #ifdef __VMS |
| 752 | sys$dassgn(vms_channel); |
752 | sys$dassgn(vms_channel); |
| 753 | return 0; |
753 | return 0; |
| 754 | #endif |
754 | #endif |
| 755 | 755 | ||
| 756 | #ifdef _WIN32 |
756 | #ifdef _WIN32 |
| 757 | CloseHandle(hdl); |
757 | CloseHandle(hdl); |
| 758 | return 0; |
758 | return 0; |
| 759 | #endif |
759 | #endif |
| 760 | 760 | ||
| 761 | #ifdef RM3 |
761 | #ifdef RM3 |
| 762 | RmBytParmStruct PBlock; /* Parameterstruktur fr RmIO - Funktion */ |
762 | RmBytParmStruct PBlock; /* Parameterstruktur fr RmIO - Funktion */ |
| 763 | RmIOStatusStruct DrvSts; /* Struktur der Rckgabewerte fr RmIO - Funktion */ |
763 | RmIOStatusStruct DrvSts; /* Struktur der Rckgabewerte fr RmIO - Funktion */ |
| 764 | /**************************************************/ |
764 | /**************************************************/ |
| 765 | if( RmIO( BYT_RELEASE, (unsigned)(device), (unsigned)(unit), 0u, 0u, &DrvSts, &PBlock ) < 0 ) |
765 | if( RmIO( BYT_RELEASE, (unsigned)(device), (unsigned)(unit), 0u, 0u, &DrvSts, &PBlock ) < 0 ) |
| 766 | { |
766 | { |
| 767 | printf( "Error: Unable to release device. device: %i, unit: %i\n",device, unit ); |
767 | printf( "Error: Unable to release device. device: %i, unit: %i\n",device, unit ); |
| 768 | return -1; |
768 | return -1; |
| 769 | } |
769 | } |
| 770 | return 0; |
770 | return 0; |
| 771 | #endif |
771 | #endif |
| 772 | } |
772 | } |
| 773 | 773 | ||