// $Id: RS232Link.c 1090 2008-11-18 15:09:43Z oli $ // Copyright (C) 2002 Dr. Georg Troska // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "RS232Link.h" #include #include #include #include #include #include using namespace std; /* string RS232Link::readLine(bool cullEmptyLines) { string s; while (in.good()) { char c=in.get(); if (!in.good()) { cerr << "ERROR: Serial connection died." << endl; exit(-1); } switch(c) { case 0x0D: break; case 0x0A: { if (!cullEmptyLines || (s.length()>0)) { if (inLog) *inLog << s << endl; return s; } break; } default: s+=c; break; } } if (inLog) *inLog << s << endl; return s; } * */ void RS232Link::closeDevice() { printf("Restoring tty settings... \n"); tcsetattr(_fd, TCSANOW, _oldtio); free(_oldtio); } RS232Link::RS232Link(string device, int baudrate) { _device=device; _baudrate=baudrate; _oldtio = (termios*)malloc( sizeof(struct termios) ); printf("Opening serial port...\n"); _fd = open(_device.c_str(), O_RDWR | O_NOCTTY); if (_fd <0) {perror(_device.c_str()); exit(-1); /* throw... */ } printf("Setting up serial port...\n"); struct termios newtio; tcgetattr(_fd, _oldtio); /* save current serial port settings */ bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */ newtio.c_cflag = _baudrate | CS8 | CLOCAL | CREAD; //| CSTOPB newtio.c_iflag = IGNPAR; newtio.c_oflag = 0; newtio.c_lflag = 0; //ICANON; tcflush(_fd, TCIFLUSH); tcsetattr(_fd, TCSANOW, &newtio); //usleep(1000); } void RS232Link::writeChar (char * buffer) { write(_fd, buffer, 1); } void RS232Link::writeLine(char * buffer) { int end; end = strlen(buffer); char buf[end+2]; strcpy(buf, buffer); end = strlen(buf); buf[end] = '\n'; /* Add a newline to the command */ buf[end + 1] = 0; //usleep(9600); write(_fd, buf, strlen(buf)); //usleep(10000); /* char *ptr_buffer; ptr_buffer = &buf[0]; while (strlen(ptr_buffer) > 0) { write(_fd, ptr_buffer,1); ptr_buffer++; usleep(960); } */ } /* int SerialDevice_test(int argc, char *argv[], char *envp[]) { SerialDevice modem; modem.init("/dev/modem", B57600); char dendl [] = {0x0d,0x0a,0x00}; modem.out << "AT" << dendl; string s; do { s=modem.readLine(); cout << s << endl; } while (s!="OK"); return 0; } * */