15.10.2012 - version 0.2.0 - PIIODevice - base of file, ethernet, serial and packets extractor. PIEthernet now support also TCP (client and server). PIConsole now can align labels in each column individually.
This commit is contained in:
85
piserial.h
Normal file → Executable file
85
piserial.h
Normal file → Executable file
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
COM
|
||||
Copyright (C) 2011 Ivan Pelipenko peri4ko@gmail.com, Bychkov Andrey wapmobil@gmail.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
|
||||
@@ -20,8 +20,8 @@
|
||||
#ifndef PISERIAL_H
|
||||
#define PISERIAL_H
|
||||
|
||||
#include "pithread.h"
|
||||
#include "pistring.h"
|
||||
#include "pitimer.h"
|
||||
#include "piiodevice.h"
|
||||
#ifndef WINDOWS
|
||||
# include <termios.h>
|
||||
# include <fcntl.h>
|
||||
@@ -42,16 +42,12 @@
|
||||
# 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 {
|
||||
class PISerial: public PIIODevice {
|
||||
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(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};
|
||||
@@ -69,31 +65,59 @@ public:
|
||||
S115200 = 115200
|
||||
};
|
||||
|
||||
void setSlot(SerialFunc func) {ret_func = func;}
|
||||
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) {devName = dev;}
|
||||
void setDevice(const PIString & dev) {path_ = 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;}
|
||||
void setParameter(PISerial::Parameters parameter, bool on = true) {params.setFlag(parameter, on);}
|
||||
void setVTime(int t) {vtime = t;}
|
||||
|
||||
bool send(uchar * data, int size);
|
||||
bool init();
|
||||
bool initialized() const {return fd != -1;}
|
||||
void terminate();
|
||||
#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
|
||||
|
||||
PIByteArray lastHeader() {return mheader;}
|
||||
ullong missedBytes() const {return missed;}
|
||||
ullong missedPackets() const {return (packetSize == 0 ? 0 : missed / packetSize);}
|
||||
const PIString & device() const {return path_;}
|
||||
PISerial::Speed outSpeed() const {return ospeed;}
|
||||
PISerial::Speed inSpeed() const {return ispeed;}
|
||||
int VTime() const {return vtime;}
|
||||
|
||||
private:
|
||||
#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;}
|
||||
|
||||
void begin();
|
||||
void run();
|
||||
void end();
|
||||
bool openDevice();
|
||||
bool closeDevice();
|
||||
|
||||
#ifdef WINDOWS
|
||||
DCB desc, sdesc;
|
||||
@@ -103,18 +127,11 @@ private:
|
||||
termios desc, sdesc;
|
||||
uint readed;
|
||||
#endif
|
||||
int fd;
|
||||
int fd, vtime;
|
||||
PISerial::Speed ospeed, ispeed;
|
||||
PIString devName;
|
||||
SerialFunc ret_func;
|
||||
SerialHeaderFunc ret_func_header;
|
||||
uchar buffer[SERIAL_BUFFER_SIZE], sbuffer[SERIAL_BUFFER_SIZE];
|
||||
PIByteArray mheader;
|
||||
PITimer timer;
|
||||
void * headerPtr, * data;
|
||||
int dataSize, headerSize, packetSize, allReaded, addSize, curInd;
|
||||
ullong missed;
|
||||
PIFlags<PISerial::Parameters> params;
|
||||
bool first;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user