122 lines
3.7 KiB
C++
122 lines
3.7 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
COM
|
|
Copyright (C) 2011 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef PISERIAL_H
|
|
#define PISERIAL_H
|
|
|
|
#include "pithread.h"
|
|
#include "pistring.h"
|
|
#ifndef WINDOWS
|
|
# include <termios.h>
|
|
# include <fcntl.h>
|
|
#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
|
|
|
|
#define SERIAL_BUFFER_SIZE 4096
|
|
|
|
typedef bool (*SerialFunc)(void * , uchar * , int );
|
|
typedef bool (*SerialHeaderFunc)(void * , uchar * , uchar * , int );
|
|
|
|
class PISerial: public PIThread {
|
|
public:
|
|
// slot is any function format "bool <func>(void*, uchar*, int)"
|
|
// slot_header is any function format "bool <func>(void*, uchar*, uchar*, int)"
|
|
PISerial(PIString name = "serial", void * data = 0, SerialFunc slot = 0, SerialHeaderFunc slot_header = headerValidate);
|
|
~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 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) {devName = dev;}
|
|
void setParameters(PIFlags<PISerial::Parameters> parameters) {params = parameters;}
|
|
void setData(void * d) {data = d;}
|
|
void setReadData(void * headerPtr, int headerSize, int dataSize) {this->headerPtr = headerPtr; this->headerSize = headerSize; this->dataSize = dataSize;}
|
|
|
|
bool send(uchar * data, int size);
|
|
bool init();
|
|
bool initialized() const {return fd != -1;}
|
|
void terminate();
|
|
|
|
PIByteArray lastHeader() {return mheader;}
|
|
ullong missedBytes() const {return missed;}
|
|
ullong missedPackets() const {return (packetSize == 0 ? 0 : missed / packetSize);}
|
|
|
|
private:
|
|
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;}
|
|
|
|
void begin();
|
|
void run();
|
|
void end();
|
|
|
|
#ifdef WINDOWS
|
|
DCB desc, sdesc;
|
|
void * hCom;
|
|
DWORD readed, mask;
|
|
#else
|
|
termios desc, sdesc;
|
|
uint readed;
|
|
#endif
|
|
int fd;
|
|
PISerial::Speed ospeed, ispeed;
|
|
PIString devName;
|
|
SerialFunc ret_func;
|
|
SerialHeaderFunc ret_func_header;
|
|
uchar buffer[SERIAL_BUFFER_SIZE], sbuffer[SERIAL_BUFFER_SIZE];
|
|
PIByteArray mheader;
|
|
void * headerPtr, * data;
|
|
int dataSize, headerSize, packetSize, allReaded, addSize, curInd;
|
|
ullong missed;
|
|
PIFlags<PISerial::Parameters> params;
|
|
bool first;
|
|
|
|
};
|
|
|
|
#endif // PISERIAL_H
|