25 #include <sys/types.h> 39 #include <lib$routines.h> 45 unsigned short msg_len;
65 #define RTS_TIME_WAIT 0x008 96 if(on == 1)
trace = 1;
100 int rlSerial::openDevice(
const char *devicename,
int speed,
int block,
int rtscts,
int bits,
int stopbits,
int parity)
105 if(
fd != -1)
return -1;
106 fd = open(devicename, O_RDWR | O_NOCTTY | O_NDELAY);
107 if(
fd < 0) {
return -1; }
113 buf.c_cflag = speed | CLOCAL | CREAD;
114 if(rtscts == 1) buf.c_cflag |= CRTSCTS;
115 if(bits == 7) buf.c_cflag |= CS7;
116 else buf.c_cflag |= CS8;
117 if(stopbits == 2) buf.c_cflag |= CSTOPB;
118 if(parity ==
rlSerial::ODD) buf.c_cflag |= (PARENB | PARODD);
120 buf.c_lflag = IEXTEN;
127 buf.c_iflag = IGNBRK | IGNPAR | IXANY;
128 if(tcsetattr(
fd, TCSAFLUSH, &buf) < 0) {
return -1; }
132 if(block == 1) fcntl(
fd, F_SETFL, fcntl(
fd, F_GETFL, 0) & ~O_NONBLOCK);
133 tcflush(
fd,TCIOFLUSH);
138 struct dsc$descriptor_s dsc;
141 dsc.dsc$w_length = strlen(devicename);
142 dsc.dsc$a_pointer = (
char *) devicename;
143 dsc.dsc$b_class = DSC$K_CLASS_S;
144 dsc.dsc$b_dtype = DSC$K_DTYPE_T;
146 if(status != SS$_NORMAL)
return -1;
155 if(strlen(devicename) > 80)
return -1;
156 sprintf(devname,
"\\\\.\\%s",devicename);
159 GENERIC_READ | GENERIC_WRITE,
166 if(
hdl == INVALID_HANDLE_VALUE)
168 printf(
"CreateFile(%s) failed\n",devicename);
173 if(speed ==
B50 ) baudrate = 50;
174 if(speed ==
B75 ) baudrate = 75;
175 if(speed ==
B110 ) baudrate = CBR_110;
176 if(speed ==
B134 ) baudrate = 134;
177 if(speed ==
B150 ) baudrate = 150;
178 if(speed ==
B200 ) baudrate = 200;
179 if(speed ==
B300 ) baudrate = CBR_300;
180 if(speed ==
B600 ) baudrate = CBR_600;
181 if(speed ==
B1200 ) baudrate = CBR_1200;
182 if(speed ==
B1800 ) baudrate = 1800;
183 if(speed ==
B2400 ) baudrate = CBR_2400;
184 if(speed ==
B4800 ) baudrate = CBR_4800;
185 if(speed ==
B9600 ) baudrate = CBR_9600;
186 if(speed ==
B19200 ) baudrate = CBR_19200;
187 if(speed ==
B38400 ) baudrate = CBR_38400;
188 if(speed ==
B57600 ) baudrate = CBR_57600;
189 if(speed ==
B115200 ) baudrate = CBR_115200;
190 if(speed ==
B230400 ) baudrate = 230400;
191 if(speed ==
B460800 ) baudrate = 460800;
192 if(speed ==
B500000 ) baudrate = 500000;
193 if(speed ==
B576000 ) baudrate = 576000;
194 if(speed ==
B921600 ) baudrate = 921600;
195 if(speed ==
B1000000) baudrate = 1000000;
196 if(speed ==
B1152000) baudrate = 1152000;
197 if(speed ==
B1500000) baudrate = 1500000;
198 if(speed ==
B2000000) baudrate = 2000000;
199 if(speed ==
B2500000) baudrate = 2500000;
200 if(speed ==
B3000000) baudrate = 3000000;
201 if(speed ==
B3500000) baudrate = 3500000;
202 if(speed ==
B4000000) baudrate = 4000000;
205 GetCommConfig(
hdl,&cc,&ccsize);
210 cc.dcb.DCBlength =
sizeof(DCB);
211 cc.dcb.BaudRate = baudrate;
214 cc.dcb.fOutxCtsFlow = 0;
215 if(rtscts == 1) cc.dcb.fOutxCtsFlow = 1;
216 cc.dcb.fOutxDsrFlow = 0;
217 cc.dcb.fDtrControl = DTR_CONTROL_DISABLE;
218 cc.dcb.fDsrSensitivity = 0;
219 cc.dcb.fTXContinueOnXoff = 1;
224 cc.dcb.fRtsControl = RTS_CONTROL_DISABLE;
225 if(rtscts == 1) cc.dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
226 cc.dcb.fAbortOnError = 0;
231 cc.dcb.ByteSize = bits;
235 cc.dcb.StopBits = ONESTOPBIT;
236 if(stopbits==2) cc.dcb.StopBits = TWOSTOPBITS;
243 cc.dwProviderSubType = PST_RS232;
248 ret = SetCommConfig(
hdl,&cc,
sizeof(cc));
251 printf(
"SetCommConfig ret=%d devicename=%s LastError=%ld\n",ret,devicename,GetLastError());
258 RmEntryStruct CatEntry;
260 RmIOStatusStruct DrvSts;
261 RmBytParmStruct PBlock;
262 static UCD_BYT_PORT Ucd_byt_drv;
271 if (strcmp(devicename,
"COM1") == 0)
273 strcpy(byt_com,
"BYT_COM1");
276 else if(strcmp(devicename,
"COM2") == 0)
278 strcpy(byt_com,
"BYT_COM2");
283 printf(
"Error: devicename=%s unknown\n",devicename);
291 if( RmGetEntry( RM_WAIT, byt_com, &CatEntry ) != RM_OK )
293 printf(
"Error: %s device not found\n", byt_com);
297 device = (int) ((ushort) CatEntry.ide);
298 unit = (int) CatEntry.id;
303 if( RmIO( BYT_RESERVE, (
unsigned)(device), (
unsigned)(unit), 0u, 0u, &DrvSts, &PBlock ) < 0 )
305 printf(
"Error: Unable to reserve %s device\n", byt_com);
313 if(speed ==
B50 ) baudrate = 50;
314 if(speed ==
B75 ) baudrate = 75;
315 if(speed ==
B110 ) baudrate = 110;
316 if(speed ==
B134 ) baudrate = 134;
317 if(speed ==
B150 ) baudrate = 150;
318 if(speed ==
B200 ) baudrate = 200;
319 if(speed ==
B300 ) baudrate = 300;
320 if(speed ==
B600 ) baudrate = 600;
321 if(speed ==
B1200 ) baudrate = 1200;
322 if(speed ==
B1800 ) baudrate = 1800;
323 if(speed ==
B2400 ) baudrate = 2400;
324 if(speed ==
B4800 ) baudrate = 4800;
325 if(speed ==
B9600 ) baudrate = 9600;
326 if(speed ==
B19200 ) baudrate = 19200;
327 if(speed ==
B38400 ) baudrate = 38400;
328 if(speed ==
B57600 ) baudrate = 57600;
329 if(speed ==
B115200 ) baudrate = 115200;
330 if(speed ==
B230400 ) baudrate = 230400;
331 if(speed ==
B460800 ) baudrate = 460800;
332 if(speed ==
B500000 ) baudrate = 500000;
333 if(speed ==
B576000 ) baudrate = 576000;
334 if(speed ==
B921600 ) baudrate = 921600;
335 if(speed ==
B1000000) baudrate = 1000000;
336 if(speed ==
B1152000) baudrate = 1152000;
337 if(speed ==
B1500000) baudrate = 1500000;
338 if(speed ==
B2000000) baudrate = 2000000;
339 if(speed ==
B2500000) baudrate = 2500000;
340 if(speed ==
B3000000) baudrate = 3000000;
341 if(speed ==
B3500000) baudrate = 3500000;
342 if(speed ==
B4000000) baudrate = 4000000;
343 uTimeBd = 748800 / baudrate;
348 uMode = 0x1000 | DATA_8 | STOP_1 | NOPARITY;
355 PBlock.buffer = (
char *)&Ucd_byt_drv;
356 PBlock.timlen =
sizeof(UCD_BYT_PORT);
359 iStatus = RmIO( BYT_CREATE_NEW, (
unsigned)(device), (
unsigned)(unit), 0u, 0u, &DrvSts, &PBlock );
364 Ucd_byt_drv.mobyte[5] |= (ushort) (uMode & 0xFFu);
369 Ucd_byt_drv.header.timout = timeout;
374 PBlock.string = (
char*) &Ucd_byt_drv;
375 PBlock.strlen =
sizeof(UCD_BYT_PORT);
380 iStatus = RmIO( BYT_CREATE_NEW, (
unsigned)(device), (
unsigned)(unit), 0u, 0u, &DrvSts, &PBlock );
385 cByte = inbyte( com + 0x03 );
386 outbyte( com + 0x03, (
unsigned char)(cByte | 0x80) );
391 outbyte( com + 0x00, (ushort) LOW (uTimeBd) );
392 outbyte( com + 0x01, (ushort) HIGH (uTimeBd) );
397 outbyte( com + 0x03, cByte );
399 if( iStatus ) printf(
"BYT_CREATE_NEW (set ucb): Error status = %X\n", iStatus );
409 unsigned char buf[2];
411 if(
fd == -1)
return -1;
412 ret = read(
fd,buf,1);
413 if(ret == 1)
return buf[0];
414 if(ret == 0)
return -2;
419 unsigned char buf[2];
426 unsigned char buf[2];
438 if(
trace == 1) printf(
"readChar %d\n",(
int) buf[0]);
446 unsigned char buf[2];
449 if(ret < 0)
return ret;
458 if(
fd == -1)
return -1;
459 if(
trace == 1) printf(
"writeChar %d\n",(
int)uchar);
460 ret = write(
fd,&uchar,1);
461 if(ret < 0)
return -1;
470 status = SYS$QIOW(0,
vms_channel,IO$_WRITEVBLK | IO$M_CANCTRLO | IO$M_NOFORMAT,
471 &iosb,0,0,&uchar,1,0,0,0,0);
472 if(status != SS$_NORMAL)
return -1;
480 if(
trace == 1) printf(
"writeChar %d\n",(
int)uchar);
489 if(ret)
return (
int) len;
504 for(
int i=0; i<len; i++)
508 if(
select(timeout) == 0)
break;
512 buf[i] = (
unsigned char) c;
515 if(retlen <= 0)
return -1;
525 IO$_READVBLK | IO$M_NOFILTR | IO$M_NOECHO | IO$M_TIMED,
526 iosb,0,0,buf,len,_timeout,0,0,0);
527 if(status != SS$_NORMAL)
return -1;
538 unsigned long retlen;
540 if(timeout >= 0)
select(timeout);
550 if(
trace == 1) printf(
"readBlock retlen=%ld\n",retlen);
557 RmBytParmStruct PBlock;
558 RmIOStatusStruct DrvSts;
568 PBlock.buffer = (
char*)buf;
575 iStatus = RmIO( BYT_POLL_XBUF_WAIT, (
unsigned)device, (
unsigned)unit, 0u, 0u, &DrvSts, &PBlock );
577 if( iStatus ) printf(
"BYT_POLL_XBUF_WAIT (set ucb): Error status = %X\n", iStatus );
579 if( !iStatus )
return len;
589 if(
fd == -1)
return -1;
592 printf(
"writeBlock:");
593 for(
int i=0; i<len; i++) printf(
" %d",(
int) buf[i]);
596 ret = write(
fd,buf,len);
606 status = SYS$QIOW(0,
vms_channel,IO$_WRITEVBLK | IO$M_CANCTRLO | IO$M_NOFORMAT,
607 &iosb,0,0,buf,len,0,0,0,0);
608 if(status != SS$_NORMAL)
return -1;
614 unsigned long retlen;
618 printf(
"writeBlock:");
619 for(
int i=0; i<len; i++) printf(
" %d",(
int) buf[i]);
631 if(ret)
return (
int) retlen;
636 RmBytParmStruct PBlock;
637 RmIOStatusStruct DrvSts;
646 PBlock.string = (
char*)buf;
659 cByte = inbyte( com + 0x04u );
660 outbyte( com + 0x04, (
unsigned char)(cByte | 0x02u) );
665 iStatus = RmIO( BYT_WRITE_WAIT, (
unsigned)device, (
unsigned)unit, 0u, 0u, &DrvSts, &PBlock );
673 RmPauseTask((RTS_TIME_WAIT*9600)/baudrate);
678 outbyte( com + 0x04, (
unsigned char)(cByte & 0xFDu) );
680 if( iStatus ) printf(
"BYT_WRITE_WAIT (write block): Error status = %X\n", iStatus );
682 if( !iStatus )
return len;
691 if(maxlen <= 1)
return -1;
693 buf[maxlen-1] =
'\0';
694 for(i=0; i<maxlen-2; i++)
700 if(t == 0)
return -1;
709 buf[i] = (
unsigned char) c;
710 if(c <
' ' && c !=
'\t')
722 struct timeval timout;
723 fd_set wset,rset,eset;
726 if(timeout <= 0)
return 1;
733 timout.tv_sec = timeout / 1000;
734 timout.tv_usec = (timeout % 1000) * 1000;
736 ret =
::select(maxfdp1,&rset,&wset,&eset,&timout);
737 if(ret == 0)
return 0;
746 COMMTIMEOUTS ctimeout;
748 ctimeout.ReadIntervalTimeout = timeout;
749 ctimeout.ReadTotalTimeoutMultiplier = 1;
750 ctimeout.ReadTotalTimeoutConstant = timeout;
751 ctimeout.WriteTotalTimeoutMultiplier = 1;
752 ctimeout.WriteTotalTimeoutConstant = timeout;
754 SetCommTimeouts(
hdl, &ctimeout);
766 if(
fd == -1)
return -1;
786 RmBytParmStruct PBlock;
787 RmIOStatusStruct DrvSts;
789 if( RmIO( BYT_RELEASE, (
unsigned)(device), (
unsigned)(unit), 0u, 0u, &DrvSts, &PBlock ) < 0 )
791 printf(
"Error: Unable to release device. device: %i, unit: %i\n",device, unit );
int readBlock(unsigned char *buf, int len, int timeout=-1)
void setTrace(int on)
on := 0 | 1 (bool)
enum rlSerial::@4 ttystate
int readLine(unsigned char *buf, int maxlen, int timeout=1000)
int writeChar(unsigned char uchar)
int select(int timeout=500)
int openDevice(const char *devicename, int speed=B9600, int block=1, int rtscts=1, int bits=8, int stopbits=1, int parity=rlSerial::NONE)
struct termios save_termios
unsigned short int vms_channel
int writeBlock(const unsigned char *buf, int len)