//init uart int uart_init(char *sname, int baud) { struct termios /*otio,*/ ntio; tcflag_t BAUD; //check/get baud rate BAUD = get_baud(baud); if(BAUD==BAUD_ERROR) return -1; //open tty fd = open(sname, O_RDWR | O_NOCTTY); if(fd<0) return -1; //save current port settings //tcgetattr(fd,&otio); //set config bzero(&ntio, sizeof(ntio)); ntio.c_cflag = BAUD | //baud rate CS8 | //8n1 CLOCAL | //local connection, no modem control CREAD; //enable receiving characters ntio.c_iflag = IGNPAR; //ignore bytes with parity errors ntio.c_oflag = 0; //raw output ntio.c_lflag = 0; //non-canonical input ntio.c_cc[VMIN] = 1; //wait for 1 byte minimum //flush and apply new settings tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&ntio); return 0; }
// open serial port prior to calling this! bool SSerialInterface::setup_serial_port( ) { // ESTABLISH BAUD RATE : int baud = B9600; if (_cl_baud) baud = get_baud(_cl_baud); if (baud <= 0) { printf("NOTE: non standard baud rate, trying custom divisor\n"); set_baud_divisor(_cl_baud); } struct termios newtio; bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */ /* man termios get more info on below settings */ newtio.c_cflag = baud | CS8 | CLOCAL | CREAD; if (_cl_rts_cts) { newtio.c_cflag |= CRTSCTS; } newtio.c_iflag = 0; newtio.c_oflag = 0; newtio.c_lflag = 0; // block for up till 128 characters newtio.c_cc[VMIN] = 128; // 0.5 seconds read timeout newtio.c_cc[VTIME] = 5; /* now clean the modem line and activate the settings for the port */ tcflush(_fd, TCIOFLUSH); tcsetattr(_fd,TCSANOW,&newtio); /* enable rs485 direction control */ if (_cl_rs485_delay >= 0) { struct serial_rs485 rs485; if(ioctl(_fd, TIOCGRS485, &rs485) < 0) { printf("Error getting rs485 mode\n"); return false; } else { rs485.flags |= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND; rs485.delay_rts_after_send = _cl_rs485_delay; rs485.delay_rts_before_send = 0; if(ioctl(_fd, TIOCSRS485, &rs485) < 0) { printf("Error setting rs485 mode\n"); return false; } } } return true; }
int serial_init(char *port_name, unsigned int baud) { struct termios orig_termios, cur_termios; int br = get_baud(baud); fd = open(port_name, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd == -1) { printf("opening modem serial device : fd < 0\n"); return -1; } if (tcgetattr(fd, &orig_termios)) { printf("getting modem serial device attr\n"); return -2; } cur_termios = orig_termios; /* input modes - turn off input processing */ cur_termios.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF | IMAXBEL); /* pas IGNCR sinon il vire les 0x0D */ cur_termios.c_iflag |= BRKINT; /* output_flags - turn off output processing */ cur_termios.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET); /* control modes */ cur_termios.c_cflag &= ~(CSIZE | CSTOPB | CREAD | PARENB | PARODD | HUPCL | CLOCAL); cur_termios.c_cflag |= CREAD | CS8 | CLOCAL; cur_termios.c_cflag &= ~(CRTSCTS); /* local modes */ cur_termios.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | FLUSHO | PENDIN); cur_termios.c_lflag |= NOFLSH; if (cfsetspeed(&cur_termios, br)) { printf("setting modem serial device speed\n"); return -3; } if (tcsetattr(fd, TCSADRAIN, &cur_termios)) { printf("setting modem serial device attr\n"); return -4; } return 0; }
char modIn() { char getMod(), getCh(), KBReady(); char i, s[4], ch; unsigned hi, lo, zz, c=0; void get_baud(); time_t hh; hi = 20; /*(HITIMEOUT * ra.megaHz);*/ /* about 4 ra.minutes */ lo = 0xFF; startTimer(); hh = time(NULL); while (TRUE) { fix_rollover(); if ((ra.whichIO==MODEM) && (c=carrDet()) != ra.modStat) { /* carrier changed */ if (c) { /* carrier present */ doStatusLine(S_MSGS, "Carrier Detect"); ra.haveCarrier = TRUE; pause(40); ra.guess = 3; ra.modStat = c; ra.newCarrier = TRUE; ra.rtry = 5; getDaTime(); ra.lhour = ra.hour; ra.lminute = ra.minute; return(0); } else { sleep(1); /* confirm it's not a glitch */ if (!carrDet()) { /* check again */ doStatusLine(S_MSGS, "Carrier Loss"); ra.haveCarrier = FALSE; ra.modStat = FALSE; ra.justLostCarrier = TRUE; terminate(TRUE); /* hangUp();*/ return(0); } } } if (mIready()) { if (ra.haveCarrier) { c = (getMod() & 0x7F); if (ra.whichIO == MODEM) return(c); } else { /* no carrier, so it must be */ if (ra.whichIO == MODEM) { /* a modem result code. */ get_baud(); if (ra.baud == -1) /* return 0;*/ { if (ra.lockedport && carrDet() /* ra.haveCarrier*/ ) ra.baud = 2400; /* make believe it's 2400 */ else return 0; } if (ra.baud < RING && ra.baud > -1) { neverLoggedIn = TRUE; getDaTime(); sprintf(format, "%2d|%02d|%02d %02d:%02d ", ra.year, ra.month, ra.date, ra.hour, ra.minute ); sprintf(&ra.call_record.time_day,"%-18s", format); } timeout = time(NULL) + ra.timeout/4; } } } if (KBReady()) { c = bioskey(0); /* getCh(); */ if(!(c & 0xFF)) { /* must be a special key */ switch(c >> 8){ case F10 : for_sysop = TRUE; for ( zz = 0; zz < 7; zz++) { delay(500); oChar(BELL); } doCR(); doCR(); tutorial("warning.blb"); doCR(); doCR(); break; case F9 : mPrintf("%^&*(&^GYFGR^&{{}}{{{}}{{{}}}|+*(^}&^"); byeflag = FALSE; if (carrDet() )terminate(TRUE); break; } return(0); } ch = (char) c; if (ra.whichIO == CONSOLE) { if (ch != SPECIAL) return(ch); else { ra.whichIO = MODEM; timeout = time(NULL) + (ra.loggedIn ? ra.timeout : ra.timeout/4); doStatusLine(WHICH_IO, "MODEM "); if (!carrDet()) doOnhook(); if(!ra.haveCarrier)doStatusLine(S_MSGS, "Waiting..."); return(0); } } else { if (ch == SPECIAL) { doStatusLine(WHICH_IO, "console"); doStatusLine(S_MSGS, " "); ra.whichIO = CONSOLE; if (!ra.loggedIn) setUp(FALSE); if (!carrDet()) doOffhook(); timeout = time(NULL) + (ra.loggedIn ? ra.timeout : ra.timeout/4); return(0); } } } /* check for no input. (Short-circuit evaluation, remember!) */ if ( (ra.whichIO==MODEM && ra.haveCarrier) || ra.onConsole ) { if( (time(NULL)) > timeout) { terminate(TRUE); return(0); } } } /*while TRUE */ } /*finished*/