/* PIP - Platform Independent Primitives COM Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com, Bychkov Andrey wapmobil@gmail.com 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 3 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, see . */ #ifndef PISERIAL_H #define PISERIAL_H #include "pitimer.h" #include "piiodevice.h" #ifndef WINDOWS # include # include #else # define B110 110 # define B300 300 # define B600 600 # define B1200 1200 # define B2400 2400 # define B4800 4800 # define B9600 9600 # define B14400 14400 # define B19200 19200 # define B38400 38400 # define B57600 57600 # define B115200 115200 # define B128000 128000 # define B256000 256000 #endif class PISerial: public PIIODevice { public: // slot is any function format "bool (void*, uchar*, int)" // slot_header is any function format "bool (void*, uchar*, uchar*, int)" PISerial(const PIString & device, void * data = 0, ReadRetFunc slot = 0); PISerial(void * data = 0, ReadRetFunc slot = 0); ~PISerial(); enum Parameters {ParityControl = 0x01, ParityOdd = 0x02, TwoStopBits = 0x04}; enum Speed { S110 = 110, S300 = 300, S600 = 600, S1200 = 1200, S2400 = 2400, S4800 = 4800, S9600 = 9600, S19200 = 19200, S38400 = 38400, S57600 = 57600, S115200 = 115200 }; void setData(void * d) {data = d;} //void setSlot(SerialFunc func) {ret_func = func;} void setSpeed(PISerial::Speed speed) {ospeed = ispeed = speed;} void setOutSpeed(PISerial::Speed speed) {ospeed = speed;} void setInSpeed(PISerial::Speed speed) {ispeed = speed;} void setDevice(const PIString & dev) {path_ = dev;} void setParameters(PIFlags parameters) {params = parameters;} void setParameter(PISerial::Parameters parameter, bool on = true) {params.setFlag(parameter, on);} void setVTime(int t) {vtime = t;} #ifdef WINDOWS void setReadIsBlocking(bool yes) { COMMTIMEOUTS times; times.ReadIntervalTimeout = yes ? vtime : MAXDWORD; times.ReadTotalTimeoutConstant = yes ? 1 : 0; times.ReadTotalTimeoutMultiplier = 0; times.WriteTotalTimeoutConstant = 1; times.WriteTotalTimeoutMultiplier = 0; if (isOpened()) SetCommTimeouts(hCom, ×); } #else void setReadIsBlocking(bool yes) {if (isOpened()) fcntl(fd, F_SETFL, yes ? 0 : O_NONBLOCK);} #endif const PIString & device() const {return path_;} PISerial::Speed outSpeed() const {return ospeed;} PISerial::Speed inSpeed() const {return ispeed;} int VTime() const {return vtime;} #ifdef WINDOWS int read(void * read_to, int max_size) { if (!canRead()) return -1; WaitCommEvent(hCom, 0, 0); ReadFile(hCom, read_to, max_size, &readed, 0); return readed; } #else int read(void * read_to, int max_size) {if (!canRead()) return -1; return ::read(fd, read_to, max_size);} #endif bool read(void * data, int size, double timeout_ms); int write(const void * data, int max_size, bool wait); int write(const void * data, int max_size) {return write(data, max_size, false);} bool send(const void * data, int size, bool wait = false) {return (write(data, size, wait) == size);} protected: int convertSpeed(PISerial::Speed speed); static bool headerValidate(void * d, uchar * src, uchar * rec, int size) {for (int i = 0; i < size; ++i) if (src[i] != rec[i]) return false; return true;} bool openDevice(); bool closeDevice(); #ifdef WINDOWS DCB desc, sdesc; void * hCom; DWORD readed, mask; #else termios desc, sdesc; uint readed; #endif int fd, vtime; PISerial::Speed ospeed, ispeed; PITimer timer; void * headerPtr, * data; PIFlags params; }; #endif // PISERIAL_H