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:
229
piprotocol.cpp
Normal file → Executable file
229
piprotocol.cpp
Normal file → Executable file
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Protocol, input/output channel (COM, UDP)
|
||||
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
|
||||
@@ -24,14 +24,15 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
init();
|
||||
protName = name;
|
||||
PIObject::setName(name);
|
||||
PIConfig conf(config, PIFile::Read);
|
||||
PIConfig conf(config, PIIODevice::ReadOnly);
|
||||
if (!conf.isOpened()) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t open \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Can`t open \"" << config << "\"!" << endl;
|
||||
devReceiverState = devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
int ps, gps;
|
||||
bool ok, gok, flag, gflag, has_dev = false;
|
||||
float freq, gfreq;
|
||||
PIFlags<PISerial::Parameters> pp;
|
||||
PIConfig::Entry & b(conf.getValue(name)),
|
||||
& rb(b.getValue("receiver")),
|
||||
@@ -40,7 +41,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
|
||||
/// receiver section
|
||||
if (rb.isEntryExists("ip") && rb.isEntryExists("device")) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receiver type in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous receiver type in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -49,7 +50,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) dev = gdev;
|
||||
if (gok && ok && (dev != gdev)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receiver type in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous receiver type in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -58,20 +59,44 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) ps = gps;
|
||||
if (gok && ok && (ps != gps)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receive port in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous receive port in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
type_rec = PIProtocol::Ethernet;
|
||||
eth = new PIEthernet(dev, ps, this, receiveEvent);
|
||||
eth = new PIEthernet();
|
||||
packet_ext.setDevice(eth);
|
||||
//setSenderAddress(dev, ps);
|
||||
setReceiverAddress(dev, ps);
|
||||
has_dev = true;
|
||||
flag = rb.getValue("reconnectEnabled", true, &ok);
|
||||
gflag = b.getValue("reconnectEnabled", true, &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) flag = gflag;
|
||||
if (gok && ok && (flag != gflag)) {
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous \"reconnectEnabled\" flag in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
eth->setReopenEnabled(flag);
|
||||
}
|
||||
freq = rb.getValue("reconnectTimeout", 1., &ok);
|
||||
gfreq = b.getValue("reconnectTimeout", 1., &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) freq = gfreq;
|
||||
if (gok && ok && (freq != gfreq)) {
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous \"reconnectTimeout\" value in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
eth->setReopenTimeout(freq * 1000);
|
||||
}
|
||||
if (recDataPtr == 0)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data pointer!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: null receive data pointer!" << endl;
|
||||
if (recDataSize == 0)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
|
||||
} else {
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.port\" or \"" << name << ".port\" in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.port\" or \"" << name << ".port\" in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -81,7 +106,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) dev = gdev;
|
||||
if (gok && ok && (dev != gdev)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receiver type in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous receiver type in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -90,7 +115,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) ps = gps;
|
||||
if (gok && ok && (ps != gps)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receive \"speed\" in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous receive \"speed\" in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -99,7 +124,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) flag = gflag;
|
||||
if (gok && ok && (flag != gflag)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receive \"parity\" in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous receive \"parity\" in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -110,7 +135,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) flag = gflag;
|
||||
if (gok && ok && (flag != gflag)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receive \"twoStopBits\" parity in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous receive \"twoStopBits\" parity in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -118,19 +143,30 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
}
|
||||
type_rec = PIProtocol::Serial;
|
||||
type_send = PIProtocol::Serial;
|
||||
ser = new PISerial(dev, this, receiveEvent, headerValidateEvent);
|
||||
ser = new PISerial(dev);
|
||||
packet_ext.setDevice(ser);
|
||||
//setSenderDevice(dev, (PISerial::Speed)ps);
|
||||
setReceiverDevice(dev, (PISerial::Speed)ps);
|
||||
setSenderDevice(dev, (PISerial::Speed)ps);
|
||||
ser->setInSpeed((PISerial::Speed)ps);
|
||||
ser->setParameters(pp);
|
||||
ser->setReadData(recHeaderPtr, recHeaderSize, recDataSize);
|
||||
ps = rb.getValue("vtime", 1, &ok);
|
||||
gps = b.getValue("vtime", 1, &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) ps = gps;
|
||||
if (gok && ok && (ps != gps)) {
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous receive \"vtime\" in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
ser->setVTime(ps);
|
||||
}
|
||||
has_dev = true;
|
||||
if (recDataPtr == 0)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data pointer!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: null receive data pointer!" << endl;
|
||||
if (recDataSize == 0)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
|
||||
} else {
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.speed\" or \"" << name << ".speed\" in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.speed\" or \"" << name << ".speed\" in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -140,7 +176,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) history_write_rec = ghist;
|
||||
if (gok && ok && (history_write_rec != ghist)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receiver history in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous receiver history in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -151,29 +187,30 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
history_id_rec = rb.getValue("historyID", 0, &ok);
|
||||
if (!ok) {
|
||||
history_id_rec = protName.toByteArray().checksumCRC16();
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: no receiver history ID defined, write with ID = " << history_id_rec << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: no receiver history ID defined, write with ID = " << history_id_rec << endl;
|
||||
}
|
||||
history_file_rec.open(history_path_rec, PIFile::Write | PIFile::New);
|
||||
history_file_rec.open(history_path_rec, PIIODevice::WriteOnly);
|
||||
}
|
||||
}
|
||||
float freq = rb.getValue("frequency", -1.f, &ok), gfreq = b.getValue("frequency", -1.f, &gok);
|
||||
freq = rb.getValue("frequency", -1.f, &ok);
|
||||
gfreq = b.getValue("frequency", -1.f, &gok);
|
||||
if (gok && !ok) freq = gfreq;
|
||||
if (gok && ok && (freq != gfreq)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous expected frequency in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous expected frequency in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
if (freq > 0.f && !has_dev)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: no receiver device and not null expected frequency!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: no receiver device and not null expected frequency!" << endl;
|
||||
float tm = b.getValue("disconnectTimeout", 3.f);
|
||||
if (tm <= 0.f)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: diconnect timeout <= 0 s!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: diconnect timeout <= 0 s!" << endl;
|
||||
timeout_ = (tm < 0.f) ? 0.f : tm;
|
||||
setExpectedFrequency(freq);
|
||||
|
||||
/// sender section
|
||||
if (sb.isEntryExists("ip") && sb.isEntryExists("device")) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender type in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous sender type in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -183,7 +220,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) dev = gdev;
|
||||
if (gok && ok && (dev != gdev)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender type in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous sender type in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -192,20 +229,43 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) ps = gps;
|
||||
if (gok && ok && (ps != gps)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous send port in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous send port in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
type_send = PIProtocol::Ethernet;
|
||||
if (eth == 0) eth = new PIEthernet(dev, ps, this, receiveEvent);
|
||||
if (eth == 0) eth = new PIEthernet();
|
||||
setSenderAddress(dev, ps);
|
||||
//setReceiverAddress(dev, ps);
|
||||
has_dev = true;
|
||||
flag = sb.getValue("reconnectEnabled", true, &ok);
|
||||
gflag = b.getValue("reconnectEnabled", true, &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) flag = gflag;
|
||||
if (gok && ok && (flag != gflag)) {
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous \"reconnectEnabled\" flag in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
eth->setReopenEnabled(flag);
|
||||
}
|
||||
freq = sb.getValue("reconnectTimeout", 1., &ok);
|
||||
gfreq = b.getValue("reconnectTimeout", 1., &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) freq = gfreq;
|
||||
if (gok && ok && (freq != gfreq)) {
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous \"reconnectTimeout\" value in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
eth->setReopenTimeout(freq * 1000);
|
||||
}
|
||||
if (sendDataPtr_ == 0)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null send data pointer!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: null send data pointer!" << endl;
|
||||
if (sendDataSize_ == 0)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null send data size!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: null send data size!" << endl;
|
||||
} else {
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.port\" or \"" << name << ".port\" in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.port\" or \"" << name << ".port\" in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -215,7 +275,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) dev = gdev;
|
||||
if (gok && ok && (dev != gdev)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender type in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous sender type in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -224,7 +284,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) ps = gps;
|
||||
if (gok && ok && (ps != gps)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous send \"speed\" in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous send \"speed\" in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -233,7 +293,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) flag = gflag;
|
||||
if (gok && ok && (flag != gflag)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous send \"parity\" in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous send \"parity\" in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -244,35 +304,34 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) flag = gflag;
|
||||
if (gok && ok && (flag != gflag)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous send \"twoStopBits\" parity in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous send \"twoStopBits\" parity in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
pp |= PISerial::TwoStopBits;
|
||||
}
|
||||
} else {
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.speed\" or \"" << name << ".speed\" in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.speed\" or \"" << name << ".speed\" in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
type_send = PIProtocol::Serial;
|
||||
if (ser == 0) ser = new PISerial(dev, this, receiveEvent, headerValidateEvent);
|
||||
if (ser == 0) ser = new PISerial(dev);
|
||||
setSenderDevice(dev, (PISerial::Speed)ps);
|
||||
ser->setOutSpeed((PISerial::Speed)ps);
|
||||
ser->setParameters(pp);
|
||||
ser->setReadData(recHeaderPtr, recHeaderSize, recDataSize);
|
||||
has_dev = true;
|
||||
if (sendDataPtr_ == 0)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null send data pointer!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: null send data pointer!" << endl;
|
||||
if (sendDataSize_ == 0)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null send data size!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: null send data size!" << endl;
|
||||
}
|
||||
history_write_send = sb.getValue("writeHistory", false, &ok);
|
||||
ghist = b.getValue("writeHistory", false, &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) history_write_send = ghist;
|
||||
if (gok && ok && (history_write_send != ghist)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender history in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous sender history in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
@@ -283,21 +342,21 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
history_id_send = sb.getValue("historyID", 0, &ok);
|
||||
if (!ok) {
|
||||
history_id_send = protName.toByteArray().checksumCRC16() + 1;
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: no sender history ID defined, write with ID = " << history_id_send << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: no sender history ID defined, write with ID = " << history_id_send << endl;
|
||||
}
|
||||
history_file_send.open(history_path_send, PIFile::Write | PIFile::New);
|
||||
history_file_send.open(history_path_send, PIIODevice::WriteOnly);
|
||||
}
|
||||
}
|
||||
freq = sb.getValue("frequency", -1.f, &ok);
|
||||
gfreq = b.getValue("frequency", -1.f, &gok);
|
||||
if (gok && !ok) freq = gfreq;
|
||||
if (gok && ok && (freq != gfreq)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender frequency in \"" << config << "\"!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Ambiguous sender frequency in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
if (freq > 0.f && !has_dev)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: no sender device and not null send frequency!" << endl;
|
||||
piCout << "[PIProtocol \"" << name << "\"] Warning: no sender device and not null send frequency!" << endl;
|
||||
setSenderFrequency(freq);
|
||||
|
||||
headerPtr = (uchar * )recHeaderPtr;
|
||||
@@ -306,6 +365,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
dataSize = recDataSize;
|
||||
sendDataPtr = (uchar * )sendDataPtr_;
|
||||
sendDataSize = sendDataSize_;
|
||||
packet_ext.setPacketData(recHeaderPtr, recHeaderSize, recDataSize);
|
||||
if (type_rec == PIProtocol::Ethernet) {
|
||||
if (recHeaderPtr != 0) {
|
||||
dataPtr = (uchar * )recHeaderPtr;
|
||||
@@ -341,6 +401,9 @@ PIProtocol::~PIProtocol() {
|
||||
|
||||
|
||||
void PIProtocol::init() {
|
||||
packet_ext.setThreadedReadData(this);
|
||||
packet_ext.setThreadedReadSlot(receiveEvent);
|
||||
packet_ext.setHeaderCheckSlot(headerValidateEvent);
|
||||
work = new_mp_prot = history_write_rec = history_write_send = false;
|
||||
eth = 0;
|
||||
ser = 0;
|
||||
@@ -379,7 +442,10 @@ void PIProtocol::init() {
|
||||
void PIProtocol::setReceiverDevice(const PIString & device, PISerial::Speed speed, bool force) {
|
||||
if (force) {
|
||||
type_send = type_rec = PIProtocol::Serial;
|
||||
if (ser == 0) ser = new PISerial("", this, receiveEvent, headerValidateEvent);
|
||||
if (ser == 0) {
|
||||
ser = new PISerial();
|
||||
packet_ext.setDevice(ser);
|
||||
}
|
||||
}
|
||||
if (type_rec == PIProtocol::Serial && ser != 0) {
|
||||
ser->setDevice(device);
|
||||
@@ -393,7 +459,10 @@ void PIProtocol::setReceiverDevice(const PIString & device, PISerial::Speed spee
|
||||
void PIProtocol::setReceiverAddress(const PIString & ip, int port, bool force) {
|
||||
if (force) {
|
||||
type_rec = PIProtocol::Ethernet;
|
||||
if (eth == 0) eth = new PIEthernet("", 0, this, receiveEvent);
|
||||
if (eth == 0) {
|
||||
eth = new PIEthernet();
|
||||
packet_ext.setDevice(eth);
|
||||
}
|
||||
}
|
||||
if (type_rec == PIProtocol::Ethernet && eth != 0) {
|
||||
eth->setReadAddress(ip, port);
|
||||
@@ -406,11 +475,12 @@ void PIProtocol::setReceiverAddress(const PIString & ip, int port, bool force) {
|
||||
void PIProtocol::setSenderDevice(const PIString & device, PISerial::Speed speed, bool force) {
|
||||
if (force) {
|
||||
type_send = type_rec = PIProtocol::Serial;
|
||||
if (ser == 0) ser = new PISerial("", this, receiveEvent, headerValidateEvent);
|
||||
if (ser == 0) ser = new PISerial();
|
||||
}
|
||||
if (type_send == PIProtocol::Serial && ser != 0) {
|
||||
ser->setDevice(device);
|
||||
ser->setSpeed(speed);
|
||||
ser->open();
|
||||
devSenderName = device;
|
||||
}
|
||||
}
|
||||
@@ -419,10 +489,11 @@ void PIProtocol::setSenderDevice(const PIString & device, PISerial::Speed speed,
|
||||
void PIProtocol::setSenderAddress(const PIString & ip, int port, bool force) {
|
||||
if (force) {
|
||||
type_send = PIProtocol::Ethernet;
|
||||
if (eth == 0) eth = new PIEthernet("", 0, this, receiveEvent);
|
||||
if (eth == 0) eth = new PIEthernet();
|
||||
}
|
||||
if (type_send == PIProtocol::Ethernet && eth != 0) {
|
||||
eth->setSendAddress(ip, port);
|
||||
eth->open();
|
||||
if (ip.isEmpty()) devSenderName = "no ip";
|
||||
else devSenderName = ip + ":" + PIString::fromNumber(port);
|
||||
}
|
||||
@@ -444,9 +515,10 @@ void PIProtocol::changeDisconnectTimeout() {
|
||||
|
||||
void PIProtocol::startReceive(float exp_frequency) {
|
||||
if (exp_frequency > 0.f) exp_freq = exp_frequency;
|
||||
if (type_rec == PIProtocol::Serial) ser->start();
|
||||
if (type_rec == PIProtocol::Ethernet) eth->start();
|
||||
//if (type_rec == PIProtocol::Serial) ser->start();
|
||||
//if (type_rec == PIProtocol::Ethernet) eth->start();
|
||||
if (exp_freq <= 0.f) return;
|
||||
packet_ext.startThreadedRead();
|
||||
setExpectedFrequency(exp_freq);
|
||||
diagTimer->start(1000. / exp_freq);
|
||||
diagTimer->reset();
|
||||
@@ -463,8 +535,9 @@ void PIProtocol::startSend(float frequency) {
|
||||
|
||||
|
||||
void PIProtocol::stopReceive() {
|
||||
if (type_rec == PIProtocol::Serial) ser->stop();
|
||||
if (type_rec == PIProtocol::Ethernet) eth->stop();
|
||||
//if (type_rec == PIProtocol::Serial) ser->stop();
|
||||
//if (type_rec == PIProtocol::Ethernet) eth->stop();
|
||||
packet_ext.stop();
|
||||
diagTimer->stop();
|
||||
raiseEvent(this, "receiver stopped");
|
||||
}
|
||||
@@ -508,7 +581,7 @@ void PIProtocol::diagEvent(void * t, int) {
|
||||
p->calc_freq();
|
||||
p->calc_diag();
|
||||
p->check_state();
|
||||
if (p->ser != 0) p->missed_count = p->ser->missedPackets();
|
||||
if (p->ser != 0) p->missed_count = p->packet_ext.missedPackets();
|
||||
}
|
||||
|
||||
|
||||
@@ -558,47 +631,49 @@ void PIProtocol::calc_freq() {
|
||||
void PIProtocol::check_state() {
|
||||
if (type_rec == PIProtocol::Serial) {
|
||||
if (ser != 0) {
|
||||
if (ser->initialized()) devReceiverState = "Initialized";
|
||||
else devReceiverState = "Uninitialized";
|
||||
if (ser->isOpened()) devReceiverState = "Opened";
|
||||
else devReceiverState = "Not opened";
|
||||
}
|
||||
else devReceiverState = "Uninitialized";
|
||||
else devReceiverState = "Not exists";
|
||||
}
|
||||
if (type_rec == PIProtocol::Ethernet) {
|
||||
if (eth != 0) {
|
||||
if (eth->receiverInitialized()) devReceiverState = "Initialized";
|
||||
else devReceiverState = "Uninitialized";
|
||||
if (eth->isOpened()) devReceiverState = "Opened";
|
||||
else devReceiverState = "Not opened";
|
||||
}
|
||||
else devReceiverState = "Uninitialized";
|
||||
else devReceiverState = "Not exists";
|
||||
}
|
||||
if (type_send == PIProtocol::Serial) {
|
||||
if (ser != 0) {
|
||||
if (ser->initialized()) devSenderState = "Initialized";
|
||||
else devSenderState = "Uninitialized";
|
||||
if (ser->isOpened()) devSenderState = "Opened";
|
||||
else devSenderState = "Not opened";
|
||||
}
|
||||
else devSenderState = "Uninitialized";
|
||||
else devSenderState = "Not exists";
|
||||
}
|
||||
if (type_send == PIProtocol::Ethernet) {
|
||||
if (eth != 0) {
|
||||
if (eth->senderInitialized()) devSenderState = "Initialized";
|
||||
else devSenderState = "Uninitialized";
|
||||
if (eth->isOpened()) devSenderState = "Opened";
|
||||
else devSenderState = "Not opened";
|
||||
}
|
||||
else devSenderState = "Uninitialized";
|
||||
else devSenderState = "Not exists";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PIProtocol::send(const void * data, int size) {
|
||||
if (data == 0 || size == 0) return;
|
||||
if (!aboutSend()) return;
|
||||
void PIProtocol::send(const void * data, int size, bool direct) {
|
||||
if (!direct) {
|
||||
if (data == 0 || size == 0) return;
|
||||
if (!aboutSend()) return;
|
||||
}
|
||||
if (history_write_send) {
|
||||
history_file_send.writeToBinLog(history_id_send, data, size);
|
||||
history_rsize_send.setReadableSize(history_file_send.pos());
|
||||
}
|
||||
if (type_send == PIProtocol::Serial)
|
||||
if (ser->send((uchar * )data, size))
|
||||
if (ser->send(data, size))
|
||||
send_count++;
|
||||
if (type_send == PIProtocol::Ethernet)
|
||||
if (eth->send((uchar * )data, size))
|
||||
if (eth->send(data, size))
|
||||
send_count++;
|
||||
}
|
||||
|
||||
@@ -607,16 +682,16 @@ void PIProtocol::send() {
|
||||
//lock();
|
||||
//memcpy(packet, sendDataPtr, sendDataSize);
|
||||
//unlock();
|
||||
if (sendDataPtr == 0 || sendDataSize == 0) return;
|
||||
if (!aboutSend()) return;
|
||||
if (sendDataPtr == 0 || sendDataSize == 0) return;
|
||||
if (history_write_send) {
|
||||
history_file_send.writeToBinLog(history_id_send, sendDataPtr, sendDataSize);
|
||||
history_rsize_send.setReadableSize(history_file_send.pos());
|
||||
}
|
||||
if (type_send == PIProtocol::Serial)
|
||||
if (ser->send((uchar * )sendDataPtr, sendDataSize))
|
||||
if (ser->send(sendDataPtr, sendDataSize))
|
||||
send_count++;
|
||||
if (type_send == PIProtocol::Ethernet)
|
||||
if (eth->send((uchar * )sendDataPtr, sendDataSize))
|
||||
if (eth->send(sendDataPtr, sendDataSize))
|
||||
send_count++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user