Add updated sources of thomas.
This commit is contained in:
282
src/prot.cpp
282
src/prot.cpp
@@ -9,9 +9,33 @@ T_prot::T_prot()
|
||||
|
||||
mySerialPort = new T_com();
|
||||
connect(mySerialPort, SIGNAL(receivingFinished()), this, SLOT( analyseRecData() ));
|
||||
//connect(mySerialPort, SIGNAL(receivingFinished()), this, SLOT( receivFin() ));
|
||||
//connect(mySerialPort, SIGNAL(sendingFinished()), this, SLOT(sendeFin()));
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
|
||||
for (int nn=0; nn<FRAME_DATALEN; nn++)
|
||||
{
|
||||
chOut_Data[nn]=0;
|
||||
ui8OutputData[nn]=0;
|
||||
InputData[nn]=0;
|
||||
}
|
||||
for (int nn=0; nn<BL_DATA_LEN; nn++)
|
||||
{
|
||||
ui8BLsendData[nn]=0;
|
||||
}
|
||||
WriteCommand=0;
|
||||
WriteAddr=0;
|
||||
WrDataLength=0;
|
||||
SendDataValid=0;
|
||||
kindOfData=0;
|
||||
slaveAddr=0;
|
||||
ReadCommand=0;
|
||||
ReadAddr=0;
|
||||
reserve =0;
|
||||
RecSlaveAddr =0;
|
||||
INdataValid=0;
|
||||
readSource =0;
|
||||
readAddress=0;
|
||||
RdDataLength=0;
|
||||
BLsendDataLength=0;
|
||||
|
||||
}
|
||||
|
||||
@@ -30,6 +54,19 @@ bool T_prot::isSerialFree(void)
|
||||
return true; // ohne HS's kann er nicht blockiert sein
|
||||
}
|
||||
|
||||
void T_prot::setRecLen(uint16_t WriteCmd)
|
||||
{
|
||||
if (WriteCmd<100)
|
||||
{
|
||||
RdDataLength=DATALEN_RECEIVE_LONG; // store here already because it's no longer
|
||||
// returned from slave
|
||||
mySerialPort->receiveFixLen(TELEGRAMLEN_RECEIVE_LONG);
|
||||
} else
|
||||
{
|
||||
RdDataLength=DATALEN_RECEIVE_FAST;
|
||||
mySerialPort->receiveFixLen(TELEGRAMLEN_RECEIVE_FAST);
|
||||
}
|
||||
}
|
||||
|
||||
void T_prot::setUserWriteData(uint16_t WriteCmd, uint16_t WrAddr, uint8_t WrDatLen, uint8_t *data)
|
||||
{
|
||||
@@ -43,6 +80,8 @@ void T_prot::setUserWriteData(uint16_t WriteCmd, uint16_t WrAddr, uint8_t WrDat
|
||||
ui8OutputData[nn]=data[nn];
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
this->setRecLen(WriteCmd);
|
||||
|
||||
}
|
||||
|
||||
void T_prot::setUserWriteData(uint16_t WriteCmd, uint16_t WrAddr)
|
||||
@@ -54,6 +93,8 @@ void T_prot::setUserWriteData(uint16_t WriteCmd, uint16_t WrAddr)
|
||||
ui8OutputData[nn]=0;
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
this->setRecLen(WriteCmd);
|
||||
|
||||
}
|
||||
|
||||
void T_prot::setUserWriteData(uint16_t WriteCmd)
|
||||
@@ -65,6 +106,8 @@ void T_prot::setUserWriteData(uint16_t WriteCmd)
|
||||
ui8OutputData[nn]=0;
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
this->setRecLen(WriteCmd);
|
||||
|
||||
}
|
||||
|
||||
void T_prot::setUserWrite1DB(uint16_t WriteCmd, uint16_t WrAddr, uint8_t val)
|
||||
@@ -76,6 +119,8 @@ void T_prot::setUserWrite1DB(uint16_t WriteCmd, uint16_t WrAddr, uint8_t val)
|
||||
ui8OutputData[0]=val;
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
this->setRecLen(WriteCmd);
|
||||
|
||||
}
|
||||
|
||||
void T_prot::setUserWrite2DB(uint16_t WriteCmd, uint16_t WrAddr, uint8_t val0, uint8_t val1)
|
||||
@@ -87,6 +132,8 @@ void T_prot::setUserWrite2DB(uint16_t WriteCmd, uint16_t WrAddr, uint8_t val0, u
|
||||
ui8OutputData[1]=val1;
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
this->setRecLen(WriteCmd);
|
||||
|
||||
}
|
||||
|
||||
void T_prot::setUserWriteText(uint16_t WriteCmd, uint16_t WrAddr, uint8_t WrDatLen, char *data)
|
||||
@@ -101,6 +148,8 @@ void T_prot::setUserWriteText(uint16_t WriteCmd, uint16_t WrAddr, uint8_t WrDat
|
||||
chOut_Data[nn]=data[nn];
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=1; // 0: binaries, 1:text
|
||||
this->setRecLen(WriteCmd);
|
||||
|
||||
}
|
||||
|
||||
void T_prot::setUserReadData( uint16_t ReadCmd, uint16_t RdAddr, uint16_t reserv)
|
||||
@@ -109,6 +158,8 @@ void T_prot::setUserReadData( uint16_t ReadCmd, uint16_t RdAddr, uint16_t reser
|
||||
ReadAddr=RdAddr;
|
||||
reserve=reserv;
|
||||
SendDataValid |=2;
|
||||
readAddress=RdAddr; // store here already because it's no longer returned from slave
|
||||
readSource=ReadCmd;
|
||||
}
|
||||
|
||||
void T_prot::setUserReadData( uint16_t ReadCmd, uint16_t RdAddr)
|
||||
@@ -117,6 +168,8 @@ void T_prot::setUserReadData( uint16_t ReadCmd, uint16_t RdAddr)
|
||||
ReadAddr=RdAddr;
|
||||
reserve=0;
|
||||
SendDataValid |=2;
|
||||
readAddress=RdAddr; // store here already because it's no longer returned from slave
|
||||
readSource=ReadCmd;
|
||||
}
|
||||
|
||||
void T_prot::setUserReadData( uint16_t ReadCmd)
|
||||
@@ -125,6 +178,8 @@ void T_prot::setUserReadData( uint16_t ReadCmd)
|
||||
ReadAddr=0;
|
||||
reserve=0;
|
||||
SendDataValid |=2;
|
||||
readAddress=0; // store here already because it's no longer returned from slave
|
||||
readSource=ReadCmd;
|
||||
}
|
||||
|
||||
void T_prot::setBLsendData( uint8_t len, uint8_t *buf)
|
||||
@@ -137,8 +192,13 @@ void T_prot::setBLsendData( uint8_t len, uint8_t *buf)
|
||||
for (int nn=0; nn<BLsendDataLength; nn++)
|
||||
ui8BLsendData[nn]=buf[nn];
|
||||
WriteCommand=0xFFFF;
|
||||
this->setRecLen(100); // how many??
|
||||
//readAddress= // needed??
|
||||
|
||||
qDebug()<<"prot: got BL data " << len << "bytes, ";
|
||||
for (int i=0; i<len; ++i) {
|
||||
printf("%02x ", (unsigned char)buf[i]);
|
||||
} printf("\n");
|
||||
/*
|
||||
qDebug()<<buf[0] <<buf[1] <<buf[2] <<buf[3] <<buf[4] <<buf[5] <<buf[6] <<buf[7];
|
||||
qDebug() <<buf[8] <<buf[9] <<buf[10] <<buf[11] <<buf[12] <<buf[13]<<buf[14]<<buf[15];
|
||||
@@ -153,6 +213,11 @@ qDebug() <<buf[72] <<buf[73] <<buf[74] <<buf[75] <<buf[76] <<buf[77] <<buf[78]<<
|
||||
*/
|
||||
}
|
||||
|
||||
void T_prot::receiveFixLen(int64_t nrOfbytesToReceive)
|
||||
{
|
||||
mySerialPort->receiveFixLen(nrOfbytesToReceive);
|
||||
}
|
||||
|
||||
void T_prot::sendUserData(uint16_t slaveAdr)
|
||||
{
|
||||
// man könnte hier noch "SendDataValid" abfragen,
|
||||
@@ -167,14 +232,90 @@ void T_prot::sendUserData(uint16_t slaveAdr)
|
||||
// Bypass for bootloader, no protocol frame but send as is...
|
||||
packBuf_2.clear();
|
||||
for (int nn=0; nn<BLsendDataLength; nn++)
|
||||
packBuf_2[nn]=char(ui8BLsendData[nn]);
|
||||
packBuf_2[nn]=char(ui8BLsendData[nn]);
|
||||
mySerialPort->writeToSerial(packBuf_2, BLsendDataLength);
|
||||
|
||||
|
||||
} else
|
||||
startPacking();
|
||||
startFastPacking(); // quicker since 15.12.21TS
|
||||
//startPacking();
|
||||
}
|
||||
|
||||
void T_prot::startFastPacking(void)
|
||||
{
|
||||
uint16_t mycrc;
|
||||
uint16_t sendLen;
|
||||
uint8_t uctmp, nn, pp, CrcLp;
|
||||
char sendBuffer[FRAME_MAXLEN], ctmp;
|
||||
|
||||
//qDebug() << "prot start fast packing "<<slaveAddr;
|
||||
|
||||
for (int nn=0; nn<FRAME_MAXLEN; nn++)
|
||||
sendBuffer[nn]=0;
|
||||
|
||||
if (WriteCommand>9 && WriteCommand<100)
|
||||
{
|
||||
// long command 10...99
|
||||
// WriteCommand==0 if only read request, then use short sending
|
||||
sendBuffer[0]=STARTSIGN_SEND_LONG;
|
||||
WrDataLength=DATALEN_SEND_LONG; // immer
|
||||
//qDebug() << "send long cmd, len: " << WrDataLength;
|
||||
} else
|
||||
{
|
||||
// fast command
|
||||
sendBuffer[0]=STARTSIGN_SEND_FAST;
|
||||
WrDataLength=DATALEN_SEND_FAST; // immer
|
||||
//qDebug() << "send fast cmd, len: " << WrDataLength;
|
||||
}
|
||||
sendBuffer[1]= uint8_t(WriteCommand);
|
||||
sendBuffer[2]= uint8_t(ReadCommand);
|
||||
if (WriteAddr>0)
|
||||
sendBuffer[3]= char(WriteAddr); // bei fast nur EINE adresse, wr hat Vorrang
|
||||
else
|
||||
sendBuffer[3]= char(ReadAddr);
|
||||
// beim Fast prot. ist das reserve dann ists egal was drin steht
|
||||
|
||||
if (kindOfData) // 0: binaries, 1:text
|
||||
{
|
||||
for (nn=0; nn<WrDataLength; nn++)
|
||||
{
|
||||
pp=HEADERLEN_SEND+nn;
|
||||
ctmp=(chOut_Data[nn]); // text
|
||||
sendBuffer[pp]= char(ctmp);
|
||||
}
|
||||
|
||||
} else
|
||||
{
|
||||
for (nn=0; nn<WrDataLength; nn++)
|
||||
{
|
||||
pp=HEADERLEN_SEND+nn;
|
||||
uctmp=(ui8OutputData[nn]); // bin
|
||||
sendBuffer[pp]= char(uctmp);
|
||||
}
|
||||
}
|
||||
|
||||
CrcLp= HEADERLEN_SEND + WrDataLength;
|
||||
mycrc=0;
|
||||
for (nn=0; nn<CrcLp; nn++)
|
||||
{
|
||||
uctmp=sendBuffer[nn];
|
||||
mycrc+=uint16_t(uctmp);
|
||||
//qDebug() << mycrc;
|
||||
}
|
||||
sendBuffer[CrcLp]=char(mycrc);
|
||||
mycrc>>=8;
|
||||
sendBuffer[CrcLp+1]=char(mycrc);
|
||||
sendLen=CrcLp+2;
|
||||
|
||||
// send to VCP:
|
||||
QByteArray packBuff;
|
||||
packBuff.clear();
|
||||
packBuff.append(sendBuffer, sendLen); // ohne sendLen wird beim ersten \0 abgeschnitten!!!
|
||||
mySerialPort->writeToSerial(packBuff, sendLen);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
void T_prot::startPacking(void)
|
||||
{
|
||||
uint16_t mycrc;
|
||||
@@ -262,10 +403,8 @@ void T_prot::startPacking(void)
|
||||
packBuff.append(sendBuffer, sendLen); // ohne sendLen wird beim ersten \0 abgeschnitten!!!
|
||||
mySerialPort->writeToSerial(packBuff, sendLen);
|
||||
// void T_com::writeToSerial(const QByteArray &data, uint16_t sendLength)
|
||||
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
// receiving.....
|
||||
@@ -293,7 +432,7 @@ uint8_t recBuffer[FRAME_MAXLEN];
|
||||
|
||||
// read from "VCP":
|
||||
mySerialPort->readFromSerial(Indata, recLength);
|
||||
|
||||
qDebug()<<"prot: got data " << recLength;
|
||||
if (recLength>FRAME_MAXLEN)
|
||||
recLength=FRAME_MAXLEN;
|
||||
for (int nn=0; nn<recLength; nn++)
|
||||
@@ -301,10 +440,11 @@ uint8_t recBuffer[FRAME_MAXLEN];
|
||||
|
||||
myString.clear();
|
||||
tempStr.clear();
|
||||
uint8_t result=FramecheckInData(recBuffer, recLength); // check input data (response from slave)
|
||||
//uint8_t result=FramecheckInData(recBuffer, recLength); // check input data (response from slave)
|
||||
uint8_t result=FastCheckInData(recBuffer, recLength); // check input data (response from slave)
|
||||
if (result>0)
|
||||
{
|
||||
// dann anzeigen und ende
|
||||
// dann anzeige
|
||||
switch (result)
|
||||
{
|
||||
case 1: gpi_setTxt4masterStateLine("wrong length received"); break;
|
||||
@@ -316,6 +456,7 @@ uint8_t recBuffer[FRAME_MAXLEN];
|
||||
}
|
||||
myString.setNum(result);
|
||||
|
||||
// Daten abspeichern, könnten vom BL sein:
|
||||
gpi_storeRawReceivedData(uint8_t(recLength), recBuffer);
|
||||
emit rawDataRecieved();
|
||||
|
||||
@@ -328,16 +469,59 @@ uint8_t recBuffer[FRAME_MAXLEN];
|
||||
// konnte der Slave die geforderten Daten ausgeben (DOs, AOs)?
|
||||
// konnte der Slave die geforderten Daten einlesen (AIs, DIs)?
|
||||
|
||||
CheckInResult(recBuffer); // Ergebnisse des Slaves anzeigen
|
||||
//CheckInResult(recBuffer); // Ergebnisse des Slaves anzeigen
|
||||
// stimmt nicht mehr bei FastProt
|
||||
|
||||
CheckInData(recBuffer); // Eingangs-Daten des Slaves anzeigen
|
||||
ShowFastInData(recBuffer); // Eingangs-Daten des Slaves anzeigen
|
||||
}
|
||||
emit framerecieved();
|
||||
//qDebug() << "framereceived emitted";
|
||||
}
|
||||
|
||||
|
||||
uint8_t T_prot::FastCheckInData(uint8_t *Inbuf, uint16_t LL)
|
||||
{
|
||||
|
||||
uint16_t rawInLen=LL, crcL_Addr, recCrc, myCrc, nn, datalen, nxt;
|
||||
|
||||
if (Inbuf[0]!=STARTSIGN_RECEIVE_FAST && Inbuf[0]!=STARTSIGN_RECEIVE_LONG)
|
||||
{
|
||||
//qDebug() << "prot: got wrong start sign: " << Inbuf[0];
|
||||
return 2; // wrong start sign
|
||||
}
|
||||
if ( (rawInLen<TELEGRAMLEN_RECEIVE_FAST && Inbuf[0]==STARTSIGN_RECEIVE_FAST) ||
|
||||
(rawInLen<TELEGRAMLEN_RECEIVE_LONG && Inbuf[0]==STARTSIGN_RECEIVE_LONG) )
|
||||
{
|
||||
//qDebug("prot: got %d bytes only", rawInLen);
|
||||
return 1; // wrong length
|
||||
}
|
||||
if (Inbuf[0]==0x5F)
|
||||
datalen=DATALEN_RECEIVE_FAST;
|
||||
else
|
||||
datalen=DATALEN_RECEIVE_LONG;
|
||||
|
||||
crcL_Addr=datalen+HEADERLEN_RECEIVE; // weil im definierten protocol 2 bytes vor den Daten stehen
|
||||
recCrc=0;
|
||||
recCrc=uchar2uint(uint8_t(Inbuf[crcL_Addr+1]), uint8_t(Inbuf[crcL_Addr]));
|
||||
myCrc=0;
|
||||
for (nn=0; nn<crcL_Addr; nn++)
|
||||
{
|
||||
nxt=uint16_t (Inbuf[nn]);
|
||||
nxt &=0x00FF; // the casting makes 0xFFFF out of 0xFF !!!!!!!!!
|
||||
myCrc+=nxt;
|
||||
//qDebug("CRC: nxt: %d sum: %d", nxt, myCrc);
|
||||
}
|
||||
|
||||
if (myCrc != recCrc)
|
||||
{
|
||||
//qDebug() << "crc does not match: mycrc=" << myCrc<< " receivedCRC=" << recCrc;
|
||||
//qDebug("calculated over %d bytes", crcL_Addr);
|
||||
return 5; // crc wrong
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
uint8_t T_prot::FramecheckInData(uint8_t *Inbuf, uint16_t LL)
|
||||
{
|
||||
|
||||
@@ -385,7 +569,7 @@ uint8_t T_prot::FramecheckInData(uint8_t *Inbuf, uint16_t LL)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
uint8_t T_prot::CheckInResult(uint8_t *Inbuf)
|
||||
{
|
||||
@@ -438,24 +622,27 @@ uint8_t T_prot::CheckInResult(uint8_t *Inbuf)
|
||||
|
||||
|
||||
|
||||
uint8_t T_prot::CheckInData(uint8_t *recBuffer)
|
||||
uint8_t T_prot::ShowFastInData(uint8_t *recBuffer)
|
||||
{
|
||||
QString myString=nullptr, tempStr=nullptr;
|
||||
uint8_t result;
|
||||
|
||||
RecSlaveAddr=recBuffer[1];
|
||||
if (recBuffer[2]==0 && recBuffer[4]==0) // comand result=OK und read result =OK,
|
||||
// dann sind die Eingangsdaten gültig
|
||||
RecSlaveAddr=0;
|
||||
result=recBuffer[1]; // total result
|
||||
result &=0x60; // only read result (bit 5,6)
|
||||
|
||||
if (result==0) // read result =OK,
|
||||
// dann sind die Eingangsdaten gültig
|
||||
{
|
||||
myString.append("valid INdata: ");
|
||||
INdataValid=true;
|
||||
readSource=uchar2uint(recBuffer[6],recBuffer[5]);
|
||||
readAddress=uchar2uint(recBuffer[8],recBuffer[7]);
|
||||
//lastWakeSrc=uint8_t(recBuffer[4]);
|
||||
RdDataLength=uint8_t(recBuffer[9]);
|
||||
//readSource already set with sending
|
||||
readAddress=0;
|
||||
// RdDataLength already set with sending
|
||||
if (RdDataLength>FRAME_DATALEN)
|
||||
RdDataLength=FRAME_DATALEN;
|
||||
for (int ii=0; ii<RdDataLength; ii++)
|
||||
InputData[ii]=uint8_t(recBuffer[ii+10]);
|
||||
InputData[ii]=uint8_t(recBuffer[ii+HEADERLEN_RECEIVE]);
|
||||
|
||||
tempStr.setNum(readSource,16);
|
||||
myString.append(tempStr);
|
||||
@@ -486,6 +673,57 @@ uint8_t T_prot::CheckInData(uint8_t *recBuffer)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
uint8_t T_prot::ShowInData(uint8_t *recBuffer)
|
||||
{
|
||||
QString myString=nullptr, tempStr=nullptr;
|
||||
|
||||
RecSlaveAddr=recBuffer[1];
|
||||
if (recBuffer[2]==0 && recBuffer[4]==0) // comand result=OK und read result =OK,
|
||||
// dann sind die Eingangsdaten gültig
|
||||
{
|
||||
myString.append("valid INdata: ");
|
||||
INdataValid=true;
|
||||
readSource=uchar2uint(recBuffer[6],recBuffer[5]);
|
||||
readAddress=uchar2uint(recBuffer[8],recBuffer[7]);
|
||||
//lastWakeSrc=uint8_t(recBuffer[4]);
|
||||
RdDataLength=uint8_t(recBuffer[9]);
|
||||
if (RdDataLength>FRAME_DATALEN)
|
||||
RdDataLength=FRAME_DATALEN;
|
||||
for (int ii=0; ii<RdDataLength; ii++)
|
||||
InputData[ii]=uint8_t(recBuffer[ii+10]);
|
||||
|
||||
tempStr.setNum(readSource,16);
|
||||
myString.append(tempStr);
|
||||
myString.append(" add:");
|
||||
|
||||
tempStr.setNum(readAddress);
|
||||
myString.append(tempStr);
|
||||
//myString.append(" wakeSrc:");
|
||||
//tempStr.setNum(lastWakeSrc);
|
||||
//myString.append(tempStr);
|
||||
myString.append(" Dlen:");
|
||||
tempStr.setNum(RdDataLength);
|
||||
myString.append(tempStr);
|
||||
|
||||
} else
|
||||
{
|
||||
myString=" "; // Eingangsdaten nicht gültig, sieht man aber weiter oben schon
|
||||
|
||||
}
|
||||
|
||||
gpi_setTxt4dataStateLine(myString);
|
||||
//qDebug() << myString;
|
||||
//qDebug("prot_checkInData_bindata: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d ",
|
||||
// InputData[0], InputData[1], InputData[2], InputData[3],
|
||||
// InputData[4], InputData[5], InputData[6], InputData[7],
|
||||
// InputData[8], InputData[9], InputData[10], InputData[11],
|
||||
// InputData[12], InputData[13], InputData[14], InputData[15]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
bool T_prot::ifDataReceived()
|
||||
{
|
||||
|
Reference in New Issue
Block a user