git-svn-id: svn://db.shs.com.ru/pip@254 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2016-09-06 05:29:56 +00:00
parent f5e90f7a0f
commit 73cc277f23
4 changed files with 91 additions and 157 deletions

175
main.cpp
View File

@@ -7,149 +7,62 @@
#include "piintrospection.h" #include "piintrospection.h"
#include "pifile.h" #include "pifile.h"
#include "piterminal.h" #include "piterminal.h"
#include "piserial.h"
//struct MS {
// //MS() {i = 0; f = 0.;}
// int i;
// float f;
// PIString s;
//};
//PIByteArray & operator<<(PIByteArray & ba, const MS & v) {ba << v.i << v.f << v.s; return ba;} PIScreen screen;
//PIByteArray & operator>>(PIByteArray & ba, MS & v) {ba >> v.i >> v.f >> v.s; return ba;} PIConnection conn;
//PICout operator <<(PICout c, const MS & v) {c << "(" << v.i << v.f << v.s << ")"; return c;}
//REGISTER_VARIANT(MS)
//REGISTER_VARIANT_CAST(MS, int) {return v.i;}
//REGISTER_VARIANT_CAST(MS, float) {return v.f;}
//REGISTER_VARIANT_CAST(MS, PIString) {return v.s;}
template <uint N = 3u, typename T = double>
class VC {
public:
T v[N];
VC(T x) {for (uint i=0; i<N; i++) v[i] = x;}
T dot(VC<N, T> a) {T res = T(); for (uint i=0; i<N; i++) res += v[i] * a.v[N - i - 1]; return res;}
};
class TileTerminal: public PIScreenTile { const char conn_config[] =
public: "[connection]\n\
TileTerminal(PITerminal * t = 0): term(t) { device.test = ser://COM54:115200 #s\n\
focus_flags = PIScreenTypes::CanHasFocus; device.test.disconnectTimeout = 3.00000000 #f\n\
} []\n";
private: class Obj : public PIObject {
void drawEvent(PIScreenDrawer * d) { PIOBJECT(Obj)
//piCout << "draw" << visible; public:
if (!term) return; Obj() {
PIVector<PIVector<PIScreenTypes::Cell> > c = term->content(); CONNECTU(&conn, dataReceivedEvent, this, recv);
d->fillRect(x_, y_, x_ + width_, y_ + height_, c);
}
bool keyEvent(PIKbdListener::KeyEvent key) {
//piCout << "key";
if (!term) return false;
if (PITerminal::isSpecialKey(key.key)) term->write((PIKbdListener::SpecialKey)key.key, key.modifiers);
else {
PIByteArray ba;
ba << PIChar(key.key).toConcole1Byte();
term->write(ba);
} }
return true; EVENT_HANDLER1(void, keyEv, PIKbdListener::KeyEvent, k) {
} piCoutObj << k.key;
void resizeEvent(int w, int h) { if (k.key == -20) {
if (!term) return; PIKbdListener::exiting = true;
term->resize(w, h);
}
}
if (k.key == 's') {
PISerial * ser = (PISerial *)conn.deviceByName("test");
if (ser) {
PIByteArray ba;
ba << uchar(31) << uchar(230) << uchar(0xd) << uchar(0xa);
piCoutObj << "send";
ser->writeThreaded(ba);
}
}
}
PITerminal * term; EVENT_HANDLER2(void, recv, const PIString &, from, const PIByteArray &, data) {
}; piCoutObj << from << PIString(data) << PICoutManipulators::Hex << data;
}
};
int main (int argc, char * argv[]) { int main (int argc, char * argv[]) {
int in[8], out[8]; screen.rootTile()->addTile(new TilePICout());
memset(in, 0, 4*8);
memset(out, 0, 4*8);
for (int i = 0; i <= 0xF; ++i) {
for (int b = 0; b < 4; ++b) out[b] = (i >> b) & 1;
for (int b = 0; b < 4; ++b) PICout(0) << " " << out[b];
PICout(0) << PICoutManipulators::NewLine;
}
return 0;
PIScreen screen;
PITerminal term;
TileTerminal tt(&term);
screen.rootTile()->addTile(&tt);
tt.setFocus();
screen.enableExitCapture(); screen.enableExitCapture();
term.initialize(); screen.rootTile()->children().front()->setFocus();
piMSleep(100); Obj * o = new Obj();
term.write(PIString("pisd -h").toByteArray()); CONNECTU(&screen, keyPressed, o, keyEv)
screen.start(); PIString s = conn_config;
conn.configureFromString(&s);
PISerial * ser = (PISerial *)conn.deviceByName("test");
if (ser) {
piCout << ser->constructFullPath() << ser->isOpened();
ser->startThreadedWrite();
}
conn.start();
screen.waitForFinish(); screen.waitForFinish();
term.destroy();
return 0;
/*PISingleApplication sa("app");
if (!sa.isFirst())
sa.sendMessage(PIString("message to first").toByteArray());
else
piMSleep(5000);*/
//piCout << PIString(argv[1]).toInt();
//sh.destroy();
//proc.terminate();
/*PIByteArray v = PIString("Mandfg;kjngel").toByteArray();
PIByteArray b64 = convertToBase64(v);
piCout << PIString(v);
piCout << PIString(v.toBase64());
piCout << PIString(b64);
piCout << PIString(PIByteArray::fromBase64(v.toBase64()));
piCout << PIString(convertFromBase64(b64));*/
/*uint v = 0;
v |= (uchar(77) << 8);
piCout << PICoutManipulators::Dec << v;
piCout << PICoutManipulators::Hex << v;*/
//piCout << PIString(PIByteArray::fromBase64(v.toBase64()));
return 0;
/*VC<30, double> x(1.5), y(3.3);
double r = 0.0;
PITimeMeasurer tm;
for (int i=0; i<10000; i++)
r = x.dot(y);
piCout << r << tm.elapsed_m();*/
//PIByteArray ba;
/*PIKbdListener kbd;
kbd.enableExitCapture();
PIString str;
str.reserve(1024*1024);
PIINTROSPECTION_START
//piCout << PIINTROSPECTION_CONTAINERS->count;
WAIT_FOR_EXIT*/
// PIVector<PIVariant> vl;
// vl << PIVariant('2') << PIVariant(-5.5) << PIVariant(10) << PIVariant(complexd(2,3)) << PIVariant("text") << PIVariant(PIByteArray("bytearray", 9)) << PIVariant(PIDateTime::current());
// piForeachC (PIVariant v, vl)
// piCout << v << v.value<PIString>();
// PIByteArray ba; ba << PIString("500");
// piCout << PIVariant::fromValue(ba, "PIString");
// piCout << PIVariant("PIString");
// piCout << __PIVariantFunctions__<int>::typeNameHelper();
// piCout << __PIVariantInfoStorage__::get()->map->size();
// for (PIMap<PIString, __PIVariantInfo__*>::iterator i = __PIVariantInfoStorage__::get()->map->begin(); i != __PIVariantInfoStorage__::get()->map->end(); ++i)
// piCout << i.key() << i.value()->cast.size();
// PIEthernet eth;
// piCout << eth.properties();
// MS ms = (MS){-1, 3.3, "str"};
// PIVariant mv = PIVariant::fromValue<MS>(ms);
// piCout << mv << mv.type();
// piCout << mv.toInt();
// piCout << mv.toFloat();
// piCout << mv.toString();
// piCout << ms << mv.value<MS>() << vl[0].value<MS>();
/*PIScreen screen;
TileSimple window;
window.back_format.color_back = PIScreenTypes::Red;
window.content << TileSimple::Row("label", PIScreenTypes::CellFormat(PIScreenTypes::Black, PIScreenTypes::Transparent));
screen.rootTile()->addTile(&window);
screen.enableExitCapture(PIKbdListener::F10);
screen.start();
screen.waitForFinish();*/
return 0; return 0;
} }

View File

@@ -150,7 +150,6 @@ PIPeer::PIPeer(const PIString & n): PIIODevice() {
no_timer = false; no_timer = false;
// initNetwork(); // initNetwork();
sync_timer.addDelimiter(5); sync_timer.addDelimiter(5);
sync_timer.start(1000);
} }
@@ -846,6 +845,7 @@ void PIPeer::reinit() {
// timer.addDelimiter(5); // timer.addDelimiter(5);
// timer.start(1000); // timer.start(1000);
no_timer = false; no_timer = false;
if (!sync_timer.isRunning()) sync_timer.start(1000);
} }

View File

@@ -165,8 +165,10 @@ void PISerial::construct() {
fd = -1; fd = -1;
piMonitor.serials++; piMonitor.serials++;
setPriority(piHigh); setPriority(piHigh);
block_read = block_write = true; block_read = false;
vtime = 1; block_write = true;
vtime = 10;
sending = false;
setParameters(0); setParameters(0);
setSpeed(S115200); setSpeed(S115200);
setDataBitsCount(8); setDataBitsCount(8);
@@ -520,17 +522,11 @@ bool PISerial::openDevice() {
void PISerial::applySettings() { void PISerial::applySettings() {
#ifdef WINDOWS #ifdef WINDOWS
if (fd == -1) return; if (fd == -1) return;
COMMTIMEOUTS times; setTimeouts();
times.ReadIntervalTimeout = block_read ? vtime : MAXDWORD;
times.ReadTotalTimeoutConstant = block_read ? 0 : 1;
times.ReadTotalTimeoutMultiplier = block_read ? 0 : MAXDWORD;
times.WriteTotalTimeoutConstant = 10;
times.WriteTotalTimeoutMultiplier = 1;// block_write ? 0 : 1;
if (SetCommTimeouts(PRIVATE->hCom, &times) == -1)
piCoutObj << "Unable to set timeouts for \"" << path() << "\"";
GetCommMask(PRIVATE->hCom, &PRIVATE->mask); GetCommMask(PRIVATE->hCom, &PRIVATE->mask);
SetCommMask(PRIVATE->hCom, EV_RXCHAR); SetCommMask(PRIVATE->hCom, EV_RXCHAR);
GetCommState(PRIVATE->hCom, &PRIVATE->sdesc); GetCommState(PRIVATE->hCom, &PRIVATE->sdesc);
// piCoutObj << PRIVATE->sdesc.fBinary << PRIVATE->sdesc.fAbortOnError << PRIVATE->sdesc.fDsrSensitivity << PRIVATE->sdesc.fDtrControl << PRIVATE->sdesc.fDummy2 << PRIVATE->sdesc.fErrorChar;
PRIVATE->desc = PRIVATE->sdesc; PRIVATE->desc = PRIVATE->sdesc;
PRIVATE->desc.DCBlength = sizeof(PRIVATE->desc); PRIVATE->desc.DCBlength = sizeof(PRIVATE->desc);
PRIVATE->desc.BaudRate = convertSpeed(outSpeed()); PRIVATE->desc.BaudRate = convertSpeed(outSpeed());
@@ -544,6 +540,16 @@ void PISerial::applySettings() {
PRIVATE->desc.Parity = params[PISerial::ParityOdd] ? 1 : 2; PRIVATE->desc.Parity = params[PISerial::ParityOdd] ? 1 : 2;
} }
PRIVATE->desc.StopBits = params[PISerial::TwoStopBits] ? TWOSTOPBITS : ONESTOPBIT; PRIVATE->desc.StopBits = params[PISerial::TwoStopBits] ? TWOSTOPBITS : ONESTOPBIT;
PRIVATE->desc.fOutxCtsFlow = 0;
PRIVATE->desc.fOutxDsrFlow = 0;
PRIVATE->desc.fDtrControl = 0;
PRIVATE->desc.fRtsControl = 0;
PRIVATE->desc.fInX = 0;
PRIVATE->desc.fOutX = 0;
PRIVATE->desc.fBinary = 1;
PRIVATE->desc.fAbortOnError = 0;
PRIVATE->desc.fNull = 0;
if (SetCommState(PRIVATE->hCom, &PRIVATE->desc) == -1) { if (SetCommState(PRIVATE->hCom, &PRIVATE->desc) == -1) {
piCoutObj << "Unable to set comm state for \"" << path() << "\""; piCoutObj << "Unable to set comm state for \"" << path() << "\"";
return; return;
@@ -575,7 +581,7 @@ void PISerial::applySettings() {
cfsetospeed(&PRIVATE->desc, convertSpeed(outSpeed())); cfsetospeed(&PRIVATE->desc, convertSpeed(outSpeed()));
tcflush(fd, TCIOFLUSH); tcflush(fd, TCIOFLUSH);
fcntl(fd, F_SETFL, block_read ? 0 : O_NONBLOCK); setTimeouts();
if(tcsetattr(fd, TCSANOW, &PRIVATE->desc) < 0) { if(tcsetattr(fd, TCSANOW, &PRIVATE->desc) < 0) {
piCoutObj << "Can`t set attributes for \"" << path() << "\""; piCoutObj << "Can`t set attributes for \"" << path() << "\"";
@@ -585,22 +591,28 @@ void PISerial::applySettings() {
} }
void PISerial::setReadIsBlocking(bool yes) { void PISerial::setTimeouts() {
block_read = yes;
#ifdef WINDOWS #ifdef WINDOWS
COMMTIMEOUTS times; COMMTIMEOUTS times;
times.ReadIntervalTimeout = block_read ? vtime : MAXDWORD; times.ReadIntervalTimeout = block_read ? vtime : MAXDWORD;
times.ReadTotalTimeoutConstant = block_read ? 0 : 1; times.ReadTotalTimeoutConstant = block_read ? 0 : 1;
times.ReadTotalTimeoutMultiplier = block_read ? 0 : MAXDWORD; times.ReadTotalTimeoutMultiplier = block_read ? 0 : MAXDWORD;
times.WriteTotalTimeoutConstant = 10; times.WriteTotalTimeoutConstant = block_write ? 0 : 1;
times.WriteTotalTimeoutMultiplier = 1;//block_write ? 0 : 1; times.WriteTotalTimeoutMultiplier = 0;
if (isOpened()) SetCommTimeouts(PRIVATE->hCom, &times); if (SetCommTimeouts(PRIVATE->hCom, &times) == -1)
piCoutObj << "Unable to set timeouts for \"" << path() << "\"";
#else #else
if (isOpened()) fcntl(fd, F_SETFL, yes ? 0 : O_NONBLOCK); fcntl(fd, F_SETFL, block_read ? 0 : O_NONBLOCK);
#endif #endif
} }
void PISerial::setReadIsBlocking(bool yes) {
block_read = yes;
if (isOpened()) setTimeouts();
}
/** \brief Basic read function /** \brief Basic read function
* \details Read to pointer "read_to" no more than "max_size". If read is * \details Read to pointer "read_to" no more than "max_size". If read is
* set to blocking this function will be wait at least one byte. * set to blocking this function will be wait at least one byte.
@@ -609,8 +621,12 @@ void PISerial::setReadIsBlocking(bool yes) {
int PISerial::read(void * read_to, int max_size) { int PISerial::read(void * read_to, int max_size) {
#ifdef WINDOWS #ifdef WINDOWS
if (!canRead()) return -1; if (!canRead()) return -1;
if (sending) return -1;
// piCoutObj << "com event ...";
WaitCommEvent(PRIVATE->hCom, 0, 0); WaitCommEvent(PRIVATE->hCom, 0, 0);
// piCoutObj << "read ...";
ReadFile(PRIVATE->hCom, read_to, max_size, &PRIVATE->readed, 0); ReadFile(PRIVATE->hCom, read_to, max_size, &PRIVATE->readed, 0);
// piCoutObj << "read ok";
return PRIVATE->readed; return PRIVATE->readed;
#else #else
if (!canRead()) return -1; if (!canRead()) return -1;
@@ -620,7 +636,6 @@ int PISerial::read(void * read_to, int max_size) {
int PISerial::write(const void * data, int max_size, bool wait) { int PISerial::write(const void * data, int max_size, bool wait) {
piCoutObj << "send " << max_size;// << ": " << PIString((char*)data, max_size);
if (fd == -1 || !canWrite()) { if (fd == -1 || !canWrite()) {
//piCoutObj << "Can`t write to uninitialized COM"; //piCoutObj << "Can`t write to uninitialized COM";
return -1; return -1;
@@ -628,16 +643,21 @@ int PISerial::write(const void * data, int max_size, bool wait) {
#ifdef WINDOWS #ifdef WINDOWS
if (block_write != wait) { if (block_write != wait) {
block_write = wait; block_write = wait;
// piCoutObj << "set timeout ...";
setReadIsBlocking(block_read); setReadIsBlocking(block_read);
// piCoutObj << "set timeout ok";
} }
DWORD wrote; DWORD wrote;
// piCoutObj << "send ...";// << max_size;// << ": " << PIString((char*)data, max_size);
sending = true;
WriteFile(PRIVATE->hCom, data, max_size, &wrote, 0); WriteFile(PRIVATE->hCom, data, max_size, &wrote, 0);
sending = false;
// piCoutObj << "send ok";// << wrote << " bytes in " << path();
#else #else
int wrote; int wrote;
wrote = ::write(fd, data, max_size); wrote = ::write(fd, data, max_size);
if (wait) tcdrain(fd); if (wait) tcdrain(fd);
#endif #endif
piCoutObj << "Wrote " << wrote << " bytes in " << path();
return (int)wrote; return (int)wrote;
//piCoutObj << "Error while sending"; //piCoutObj << "Error while sending";
} }
@@ -757,8 +777,8 @@ PIStringList PISerial::availableDevices(bool test) {
if (test) { if (test) {
for (int i = 0; i < dl.size_s(); ++i) { for (int i = 0; i < dl.size_s(); ++i) {
#ifdef WINDOWS #ifdef WINDOWS
void * hCom = CreateFileA(dl[i].data(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0); void * hComm = CreateFileA(dl[i].data(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
if (hCom == INVALID_HANDLE_VALUE) { if (hComm == INVALID_HANDLE_VALUE) {
#else #else
int fd = ::open(dl[i].data(), O_NOCTTY | O_RDONLY); int fd = ::open(dl[i].data(), O_NOCTTY | O_RDONLY);
if (fd == -1) { if (fd == -1) {
@@ -776,8 +796,8 @@ PIStringList PISerial::availableDevices(bool test) {
times.ReadTotalTimeoutMultiplier = 0; times.ReadTotalTimeoutMultiplier = 0;
times.WriteTotalTimeoutConstant = 1; times.WriteTotalTimeoutConstant = 1;
times.WriteTotalTimeoutMultiplier = 0; times.WriteTotalTimeoutMultiplier = 0;
SetCommTimeouts(hCom, &times); SetCommTimeouts(hComm, &times);
if (ReadFile(hCom, &void_, 1, &readed_, 0) == 0) if (ReadFile(hComm, &void_, 1, &readed_, 0) == 0)
rok = GetLastError() == ;*/ rok = GetLastError() == ;*/
#else #else
fcntl(fd, F_SETFL, O_NONBLOCK); fcntl(fd, F_SETFL, O_NONBLOCK);
@@ -791,7 +811,7 @@ PIStringList PISerial::availableDevices(bool test) {
continue; continue;
} }
#ifdef WINDOWS #ifdef WINDOWS
CloseHandle(hCom); CloseHandle(hComm);
#else #else
::close(fd); ::close(fd);
#endif #endif

View File

@@ -225,6 +225,7 @@ protected:
void construct(); void construct();
void applySettings(); void applySettings();
void setTimeouts();
int convertSpeed(PISerial::Speed speed); int convertSpeed(PISerial::Speed speed);
bool setBit(int bit, bool on, const PIString & bname); bool setBit(int bit, bool on, const PIString & bname);
bool isBit(int bit, const PIString & bname) const; bool isBit(int bit, const PIString & bname) const;
@@ -234,7 +235,7 @@ protected:
PRIVATE_DECLARATION PRIVATE_DECLARATION
int fd, vtime; int fd, vtime;
bool block_read, block_write; bool block_read, block_write, sending;
PITimeMeasurer tm_; PITimeMeasurer tm_;
}; };