git-svn-id: svn://db.shs.com.ru/pip@254 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
167
main.cpp
167
main.cpp
@@ -7,149 +7,62 @@
|
||||
#include "piintrospection.h"
|
||||
#include "pifile.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;}
|
||||
//PIByteArray & operator>>(PIByteArray & ba, MS & v) {ba >> v.i >> v.f >> v.s; return ba;}
|
||||
//PICout operator <<(PICout c, const MS & v) {c << "(" << v.i << v.f << v.s << ")"; return c;}
|
||||
PIScreen screen;
|
||||
PIConnection conn;
|
||||
|
||||
//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 {
|
||||
public:
|
||||
TileTerminal(PITerminal * t = 0): term(t) {
|
||||
focus_flags = PIScreenTypes::CanHasFocus;
|
||||
const char conn_config[] =
|
||||
"[connection]\n\
|
||||
device.test = ser://COM54:115200 #s\n\
|
||||
device.test.disconnectTimeout = 3.00000000 #f\n\
|
||||
[]\n";
|
||||
|
||||
class Obj : public PIObject {
|
||||
PIOBJECT(Obj)
|
||||
public:
|
||||
Obj() {
|
||||
CONNECTU(&conn, dataReceivedEvent, this, recv);
|
||||
}
|
||||
EVENT_HANDLER1(void, keyEv, PIKbdListener::KeyEvent, k) {
|
||||
piCoutObj << k.key;
|
||||
if (k.key == -20) {
|
||||
PIKbdListener::exiting = true;
|
||||
|
||||
private:
|
||||
void drawEvent(PIScreenDrawer * d) {
|
||||
//piCout << "draw" << visible;
|
||||
if (!term) return;
|
||||
PIVector<PIVector<PIScreenTypes::Cell> > c = term->content();
|
||||
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 {
|
||||
if (k.key == 's') {
|
||||
PISerial * ser = (PISerial *)conn.deviceByName("test");
|
||||
if (ser) {
|
||||
PIByteArray ba;
|
||||
ba << PIChar(key.key).toConcole1Byte();
|
||||
term->write(ba);
|
||||
ba << uchar(31) << uchar(230) << uchar(0xd) << uchar(0xa);
|
||||
piCoutObj << "send";
|
||||
ser->writeThreaded(ba);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void resizeEvent(int w, int h) {
|
||||
if (!term) return;
|
||||
term->resize(w, h);
|
||||
}
|
||||
|
||||
|
||||
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 in[8], out[8];
|
||||
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.rootTile()->addTile(new TilePICout());
|
||||
screen.enableExitCapture();
|
||||
term.initialize();
|
||||
piMSleep(100);
|
||||
term.write(PIString("pisd -h").toByteArray());
|
||||
screen.start();
|
||||
screen.rootTile()->children().front()->setFocus();
|
||||
Obj * o = new Obj();
|
||||
CONNECTU(&screen, keyPressed, o, keyEv)
|
||||
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();
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -150,7 +150,6 @@ PIPeer::PIPeer(const PIString & n): PIIODevice() {
|
||||
no_timer = false;
|
||||
// initNetwork();
|
||||
sync_timer.addDelimiter(5);
|
||||
sync_timer.start(1000);
|
||||
}
|
||||
|
||||
|
||||
@@ -846,6 +845,7 @@ void PIPeer::reinit() {
|
||||
// timer.addDelimiter(5);
|
||||
// timer.start(1000);
|
||||
no_timer = false;
|
||||
if (!sync_timer.isRunning()) sync_timer.start(1000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -165,8 +165,10 @@ void PISerial::construct() {
|
||||
fd = -1;
|
||||
piMonitor.serials++;
|
||||
setPriority(piHigh);
|
||||
block_read = block_write = true;
|
||||
vtime = 1;
|
||||
block_read = false;
|
||||
block_write = true;
|
||||
vtime = 10;
|
||||
sending = false;
|
||||
setParameters(0);
|
||||
setSpeed(S115200);
|
||||
setDataBitsCount(8);
|
||||
@@ -520,17 +522,11 @@ bool PISerial::openDevice() {
|
||||
void PISerial::applySettings() {
|
||||
#ifdef WINDOWS
|
||||
if (fd == -1) return;
|
||||
COMMTIMEOUTS times;
|
||||
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, ×) == -1)
|
||||
piCoutObj << "Unable to set timeouts for \"" << path() << "\"";
|
||||
setTimeouts();
|
||||
GetCommMask(PRIVATE->hCom, &PRIVATE->mask);
|
||||
SetCommMask(PRIVATE->hCom, EV_RXCHAR);
|
||||
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.DCBlength = sizeof(PRIVATE->desc);
|
||||
PRIVATE->desc.BaudRate = convertSpeed(outSpeed());
|
||||
@@ -544,6 +540,16 @@ void PISerial::applySettings() {
|
||||
PRIVATE->desc.Parity = params[PISerial::ParityOdd] ? 1 : 2;
|
||||
}
|
||||
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) {
|
||||
piCoutObj << "Unable to set comm state for \"" << path() << "\"";
|
||||
return;
|
||||
@@ -575,7 +581,7 @@ void PISerial::applySettings() {
|
||||
cfsetospeed(&PRIVATE->desc, convertSpeed(outSpeed()));
|
||||
|
||||
tcflush(fd, TCIOFLUSH);
|
||||
fcntl(fd, F_SETFL, block_read ? 0 : O_NONBLOCK);
|
||||
setTimeouts();
|
||||
|
||||
if(tcsetattr(fd, TCSANOW, &PRIVATE->desc) < 0) {
|
||||
piCoutObj << "Can`t set attributes for \"" << path() << "\"";
|
||||
@@ -585,22 +591,28 @@ void PISerial::applySettings() {
|
||||
}
|
||||
|
||||
|
||||
void PISerial::setReadIsBlocking(bool yes) {
|
||||
block_read = yes;
|
||||
void PISerial::setTimeouts() {
|
||||
#ifdef WINDOWS
|
||||
COMMTIMEOUTS times;
|
||||
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 (isOpened()) SetCommTimeouts(PRIVATE->hCom, ×);
|
||||
times.WriteTotalTimeoutConstant = block_write ? 0 : 1;
|
||||
times.WriteTotalTimeoutMultiplier = 0;
|
||||
if (SetCommTimeouts(PRIVATE->hCom, ×) == -1)
|
||||
piCoutObj << "Unable to set timeouts for \"" << path() << "\"";
|
||||
#else
|
||||
if (isOpened()) fcntl(fd, F_SETFL, yes ? 0 : O_NONBLOCK);
|
||||
fcntl(fd, F_SETFL, block_read ? 0 : O_NONBLOCK);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void PISerial::setReadIsBlocking(bool yes) {
|
||||
block_read = yes;
|
||||
if (isOpened()) setTimeouts();
|
||||
}
|
||||
|
||||
|
||||
/** \brief Basic read function
|
||||
* \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.
|
||||
@@ -609,8 +621,12 @@ void PISerial::setReadIsBlocking(bool yes) {
|
||||
int PISerial::read(void * read_to, int max_size) {
|
||||
#ifdef WINDOWS
|
||||
if (!canRead()) return -1;
|
||||
if (sending) return -1;
|
||||
// piCoutObj << "com event ...";
|
||||
WaitCommEvent(PRIVATE->hCom, 0, 0);
|
||||
// piCoutObj << "read ...";
|
||||
ReadFile(PRIVATE->hCom, read_to, max_size, &PRIVATE->readed, 0);
|
||||
// piCoutObj << "read ok";
|
||||
return PRIVATE->readed;
|
||||
#else
|
||||
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) {
|
||||
piCoutObj << "send " << max_size;// << ": " << PIString((char*)data, max_size);
|
||||
if (fd == -1 || !canWrite()) {
|
||||
//piCoutObj << "Can`t write to uninitialized COM";
|
||||
return -1;
|
||||
@@ -628,16 +643,21 @@ int PISerial::write(const void * data, int max_size, bool wait) {
|
||||
#ifdef WINDOWS
|
||||
if (block_write != wait) {
|
||||
block_write = wait;
|
||||
// piCoutObj << "set timeout ...";
|
||||
setReadIsBlocking(block_read);
|
||||
// piCoutObj << "set timeout ok";
|
||||
}
|
||||
DWORD wrote;
|
||||
// piCoutObj << "send ...";// << max_size;// << ": " << PIString((char*)data, max_size);
|
||||
sending = true;
|
||||
WriteFile(PRIVATE->hCom, data, max_size, &wrote, 0);
|
||||
sending = false;
|
||||
// piCoutObj << "send ok";// << wrote << " bytes in " << path();
|
||||
#else
|
||||
int wrote;
|
||||
wrote = ::write(fd, data, max_size);
|
||||
if (wait) tcdrain(fd);
|
||||
#endif
|
||||
piCoutObj << "Wrote " << wrote << " bytes in " << path();
|
||||
return (int)wrote;
|
||||
//piCoutObj << "Error while sending";
|
||||
}
|
||||
@@ -757,8 +777,8 @@ PIStringList PISerial::availableDevices(bool test) {
|
||||
if (test) {
|
||||
for (int i = 0; i < dl.size_s(); ++i) {
|
||||
#ifdef WINDOWS
|
||||
void * hCom = CreateFileA(dl[i].data(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
|
||||
if (hCom == INVALID_HANDLE_VALUE) {
|
||||
void * hComm = CreateFileA(dl[i].data(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
|
||||
if (hComm == INVALID_HANDLE_VALUE) {
|
||||
#else
|
||||
int fd = ::open(dl[i].data(), O_NOCTTY | O_RDONLY);
|
||||
if (fd == -1) {
|
||||
@@ -776,8 +796,8 @@ PIStringList PISerial::availableDevices(bool test) {
|
||||
times.ReadTotalTimeoutMultiplier = 0;
|
||||
times.WriteTotalTimeoutConstant = 1;
|
||||
times.WriteTotalTimeoutMultiplier = 0;
|
||||
SetCommTimeouts(hCom, ×);
|
||||
if (ReadFile(hCom, &void_, 1, &readed_, 0) == 0)
|
||||
SetCommTimeouts(hComm, ×);
|
||||
if (ReadFile(hComm, &void_, 1, &readed_, 0) == 0)
|
||||
rok = GetLastError() == ;*/
|
||||
#else
|
||||
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
@@ -791,7 +811,7 @@ PIStringList PISerial::availableDevices(bool test) {
|
||||
continue;
|
||||
}
|
||||
#ifdef WINDOWS
|
||||
CloseHandle(hCom);
|
||||
CloseHandle(hComm);
|
||||
#else
|
||||
::close(fd);
|
||||
#endif
|
||||
|
||||
@@ -225,6 +225,7 @@ protected:
|
||||
|
||||
void construct();
|
||||
void applySettings();
|
||||
void setTimeouts();
|
||||
int convertSpeed(PISerial::Speed speed);
|
||||
bool setBit(int bit, bool on, const PIString & bname);
|
||||
bool isBit(int bit, const PIString & bname) const;
|
||||
@@ -234,7 +235,7 @@ protected:
|
||||
|
||||
PRIVATE_DECLARATION
|
||||
int fd, vtime;
|
||||
bool block_read, block_write;
|
||||
bool block_read, block_write, sending;
|
||||
PITimeMeasurer tm_;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user