9.10.2011 - stable backup commit
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * recHeaderPtr, int recHeaderSize, void * recDataPtr, int recDataSize, void * sendDataPtr_, int sendDataSize_) {
|
||||
init();
|
||||
protName = name;
|
||||
PIConfig conf(config, PIFile::Read);
|
||||
if (!conf.isOpened()) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t open \"" << config << "\"!" << endl;
|
||||
@@ -27,14 +28,13 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok) {
|
||||
ps = rb.getValue("port", 0, &ok);
|
||||
if (!ok) {
|
||||
type_rec = PIProtocol::None;
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.port\" in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
type_rec = PIProtocol::Ethernet;
|
||||
eth = new PIEthernet(dev, ps, this, receiveEvent);
|
||||
devReceiverName = dev + ":" + PIString::fromNumber(ps);
|
||||
setReceiverAddress(dev, ps);
|
||||
}
|
||||
dev = rb.getValue("device", "", &ok);
|
||||
if (ok) {
|
||||
@@ -47,12 +47,15 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (rb.getValue("parity", false)) pp |= PISerial::ParityControl;
|
||||
if (rb.getValue("twoStopBits", false)) pp |= PISerial::TwoStopBits;
|
||||
type_rec = PIProtocol::Serial;
|
||||
type_send = PIProtocol::Serial;
|
||||
ser = new PISerial(dev, this, receiveEvent);
|
||||
setReceiverDevice(dev, (PISerial::Speed)ps);
|
||||
setSenderDevice(dev, (PISerial::Speed)ps);
|
||||
ser->setInSpeed((PISerial::Speed)ps);
|
||||
ser->setParameters(pp);
|
||||
ser->setReadData(recHeaderPtr, recHeaderSize, recDataSize);
|
||||
devReceiverName = dev;
|
||||
}
|
||||
setExpectedFrequency(rb.getValue("frequency", -1.f));
|
||||
|
||||
/// sender section
|
||||
if (sb.isEntryExists("ip") && sb.isEntryExists("device")) {
|
||||
@@ -64,7 +67,6 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (ok) {
|
||||
ps = sb.getValue("port", 0, &ok);
|
||||
if (!ok) {
|
||||
type_send = PIProtocol::None;
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.port\" in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
@@ -72,7 +74,6 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
type_send = PIProtocol::Ethernet;
|
||||
if (eth == 0) eth = new PIEthernet(dev, ps, this, receiveEvent);
|
||||
setSenderAddress(dev, ps);
|
||||
devSenderName = dev + ":" + PIString::fromNumber(ps);
|
||||
}
|
||||
dev = sb.getValue("device", "", &ok);
|
||||
if (ok) {
|
||||
@@ -86,16 +87,13 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (sb.getValue("twoStopBits", false)) pp |= PISerial::TwoStopBits;
|
||||
type_send = PIProtocol::Serial;
|
||||
if (ser == 0) ser = new PISerial(dev, this, receiveEvent);
|
||||
setSenderDevice(dev, (PISerial::Speed)ps);
|
||||
ser->setOutSpeed((PISerial::Speed)ps);
|
||||
ser->setParameters(pp);
|
||||
ser->setReadData(recHeaderPtr, recHeaderSize, recDataSize);
|
||||
devSenderName = dev;
|
||||
}
|
||||
setSenderFrequency(sb.getValue("frequency", -1.f));
|
||||
|
||||
float fr = rb.getValue("frequency", 0.f, &ok);
|
||||
if (ok) setExpectedFrequency(fr);
|
||||
fr = sb.getValue("frequency", 0.f, &ok);
|
||||
if (ok) setSenderFrequency(fr);
|
||||
headerPtr = (uchar * )recHeaderPtr;
|
||||
headerSize = recHeaderSize;
|
||||
dataPtr = (uchar * )recDataPtr;
|
||||
@@ -113,6 +111,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
|
||||
|
||||
PIProtocol::~PIProtocol() {
|
||||
//cout << "prot " << protName << " delete\n";
|
||||
delete diagTimer;
|
||||
delete sendTimer;
|
||||
if (packet != 0) delete packet;
|
||||
@@ -122,8 +121,9 @@ PIProtocol::~PIProtocol() {
|
||||
|
||||
|
||||
void PIProtocol::init() {
|
||||
work = false;
|
||||
work = new_mp_prot = false;
|
||||
ret_func = 0;
|
||||
mp_owner = 0;
|
||||
net_diag = PIProtocol::Unknown;
|
||||
cur_pckt = 0;
|
||||
diagTimer = 0;
|
||||
@@ -131,19 +131,23 @@ void PIProtocol::init() {
|
||||
sendTimer = new PITimer(sendEvent, this);
|
||||
diagTimer = new PITimer(diagEvent, this);
|
||||
wrong_count = receive_count = send_count = 0;
|
||||
send_freq = -1.f;
|
||||
immediateFreq = integralFreq = exp_freq = 0.f;
|
||||
immediateFreq = integralFreq = 0.f;
|
||||
headerPtr = dataPtr = sendDataPtr = 0;
|
||||
headerSize = dataSize = sendDataSize = 0;
|
||||
eth = 0;
|
||||
ser = 0;
|
||||
type_rec = type_send = PIProtocol::None;
|
||||
devSenderState = devReceiverState = "Unknown";
|
||||
devSenderName = devReceiverName = "no device";
|
||||
}
|
||||
|
||||
|
||||
void PIProtocol::setReceiverDevice(const PIString & device, PISerial::Speed speed) {
|
||||
if (type_rec == PIProtocol::Serial) {
|
||||
void PIProtocol::setReceiverDevice(const PIString & device, PISerial::Speed speed, bool force) {
|
||||
if (force) {
|
||||
type_rec = PIProtocol::Serial;
|
||||
if (ser == 0) ser = new PISerial("", this, receiveEvent);
|
||||
}
|
||||
if (type_rec == PIProtocol::Serial && ser != 0) {
|
||||
ser->setDevice(device);
|
||||
ser->setSpeed(speed);
|
||||
devReceiverName = device;
|
||||
@@ -151,16 +155,25 @@ void PIProtocol::setReceiverDevice(const PIString & device, PISerial::Speed spee
|
||||
}
|
||||
|
||||
|
||||
void PIProtocol::setReceiverDevice(const PIString & ip, int port) {
|
||||
if (type_rec == PIProtocol::Ethernet) {
|
||||
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 (type_rec == PIProtocol::Ethernet && eth != 0) {
|
||||
eth->setReadAddress(ip, port);
|
||||
devReceiverName = ip + ":" + PIString::fromNumber(port);
|
||||
if (ip.trimmed().isEmpty()) devReceiverName = "no ip";
|
||||
else devReceiverName = ip + ":" + PIString::fromNumber(port);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PIProtocol::setSenderDevice(const PIString & device, PISerial::Speed speed) {
|
||||
if (type_send == PIProtocol::Serial) {
|
||||
void PIProtocol::setSenderDevice(const PIString & device, PISerial::Speed speed, bool force) {
|
||||
if (force) {
|
||||
type_send = PIProtocol::Serial;
|
||||
if (ser == 0) ser = new PISerial("", this, receiveEvent);
|
||||
}
|
||||
if (type_send == PIProtocol::Serial && ser != 0) {
|
||||
ser->setDevice(device);
|
||||
ser->setSpeed(speed);
|
||||
devSenderName = device;
|
||||
@@ -168,10 +181,15 @@ void PIProtocol::setSenderDevice(const PIString & device, PISerial::Speed speed)
|
||||
}
|
||||
|
||||
|
||||
void PIProtocol::setSenderDevice(const PIString & ip, int port) {
|
||||
if (type_send == PIProtocol::Ethernet) {
|
||||
eth->setReadAddress(ip, port);
|
||||
devSenderName = ip + ":" + PIString::fromNumber(port);
|
||||
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 (type_send == PIProtocol::Ethernet && eth != 0) {
|
||||
eth->setSendAddress(ip, port);
|
||||
if (ip.isEmpty()) devSenderName = "no ip";
|
||||
else devSenderName = ip + ":" + PIString::fromNumber(port);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,10 +238,12 @@ bool PIProtocol::receiveEvent(void * t, char * data, int size) {
|
||||
p->receive_count++;
|
||||
p->cur_pckt = 1;
|
||||
if (p->ret_func != 0) p->ret_func(p);
|
||||
if (p->mp_owner != 0) PIMultiProtocolBase::receiveEvent(p->mp_owner, p, true, data, size);
|
||||
return true;
|
||||
}
|
||||
//p->unlock();
|
||||
p->wrong_count++;
|
||||
if (p->mp_owner != 0) PIMultiProtocolBase::receiveEvent(p->mp_owner, p, false, data, size);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -247,7 +267,7 @@ void PIProtocol::calc_diag() {
|
||||
pckt_cnt++;
|
||||
} else {
|
||||
packets[(int)last_packets.back()]--;
|
||||
last_packets.pop_back();
|
||||
if (!last_packets.isEmpty()) last_packets.pop_back();
|
||||
last_packets.push_front(cur_pckt);
|
||||
}
|
||||
packets[(int)cur_pckt]++;
|
||||
@@ -277,25 +297,21 @@ void PIProtocol::calc_freq() {
|
||||
|
||||
|
||||
void PIProtocol::check_state() {
|
||||
if (type_send == PIProtocol::Serial) {
|
||||
if (ser->initialized()) devSenderState = "Initialized";
|
||||
else devSenderState = "Uninitialized";
|
||||
return;
|
||||
}
|
||||
if (type_send == PIProtocol::Ethernet) {
|
||||
if (eth->senderInitialized()) devSenderState = "Initialized";
|
||||
else devSenderState = "Uninitialized";
|
||||
return;
|
||||
}
|
||||
if (type_rec == PIProtocol::Serial) {
|
||||
if (ser->initialized()) devReceiverState = "Initialized";
|
||||
else devReceiverState = "Uninitialized";
|
||||
return;
|
||||
}
|
||||
if (type_rec == PIProtocol::Ethernet) {
|
||||
if (eth->receiverInitialized()) devReceiverState = "Initialized";
|
||||
else devReceiverState = "Uninitialized";
|
||||
return;
|
||||
}
|
||||
if (type_send == PIProtocol::Serial) {
|
||||
if (ser->initialized()) devSenderState = "Initialized";
|
||||
else devSenderState = "Uninitialized";
|
||||
}
|
||||
if (type_send == PIProtocol::Ethernet) {
|
||||
if (eth->senderInitialized()) devSenderState = "Initialized";
|
||||
else devSenderState = "Uninitialized";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,10 +319,10 @@ void PIProtocol::check_state() {
|
||||
void PIProtocol::send(const void * data, int size) {
|
||||
if (data == 0 || size == 0) return;
|
||||
if (!aboutSend()) return;
|
||||
if (type_rec == PIProtocol::Serial)
|
||||
if (type_send == PIProtocol::Serial)
|
||||
if (ser->send((char * )data, size))
|
||||
send_count++;
|
||||
if (type_rec == PIProtocol::Ethernet)
|
||||
if (type_send == PIProtocol::Ethernet)
|
||||
if (eth->send((char * )data, size))
|
||||
send_count++;
|
||||
}
|
||||
@@ -318,10 +334,10 @@ void PIProtocol::send() {
|
||||
//unlock();
|
||||
if (sendDataPtr == 0 || sendDataSize == 0) return;
|
||||
if (!aboutSend()) return;
|
||||
if (type_rec == PIProtocol::Serial)
|
||||
if (type_send == PIProtocol::Serial)
|
||||
if (ser->send((char * )sendDataPtr, sendDataSize))
|
||||
send_count++;
|
||||
if (type_rec == PIProtocol::Ethernet)
|
||||
if (type_send == PIProtocol::Ethernet)
|
||||
if (eth->send((char * )sendDataPtr, sendDataSize))
|
||||
send_count++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user