Adding src files (sax)
This commit is contained in:
525
src/prot.cpp
Normal file
525
src/prot.cpp
Normal file
@@ -0,0 +1,525 @@
|
||||
#include "prot.h"
|
||||
#include <QDebug>
|
||||
#include "controlBus.h"
|
||||
#include "dcBL.h"
|
||||
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
// sending.....
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
bool T_prot::isPortOpen(void)
|
||||
{
|
||||
return mySerialPort->isPortOpen();
|
||||
}
|
||||
|
||||
bool T_prot::isSerialFree(void)
|
||||
{
|
||||
return true; // ohne HS's kann er nicht blockiert sein
|
||||
}
|
||||
|
||||
|
||||
void T_prot::setUserWriteData(uint16_t WriteCmd, uint16_t WrAddr, uint8_t WrDatLen, uint8_t *data)
|
||||
{
|
||||
WriteCommand=WriteCmd;
|
||||
WriteAddr=WrAddr;
|
||||
WrDataLength=WrDatLen;
|
||||
if (WrDataLength>FRAME_DATALEN)
|
||||
WrDataLength=FRAME_DATALEN;
|
||||
|
||||
for (int nn=0; nn<WrDataLength; nn++)
|
||||
ui8OutputData[nn]=data[nn];
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
}
|
||||
|
||||
void T_prot::setUserWriteData(uint16_t WriteCmd, uint16_t WrAddr)
|
||||
{
|
||||
WriteCommand=WriteCmd;
|
||||
WriteAddr=WrAddr;
|
||||
WrDataLength=0;
|
||||
for (int nn=0; nn<FRAME_DATALEN; nn++)
|
||||
ui8OutputData[nn]=0;
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
}
|
||||
|
||||
void T_prot::setUserWriteData(uint16_t WriteCmd)
|
||||
{
|
||||
WriteCommand=WriteCmd;
|
||||
WriteAddr=0;
|
||||
WrDataLength=0;
|
||||
for (int nn=0; nn<FRAME_DATALEN; nn++)
|
||||
ui8OutputData[nn]=0;
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
}
|
||||
|
||||
void T_prot::setUserWrite1DB(uint16_t WriteCmd, uint16_t WrAddr, uint8_t val)
|
||||
{
|
||||
// wie oben, jedoch einfachere Datenübergabe
|
||||
WriteCommand=WriteCmd;
|
||||
WriteAddr=WrAddr;
|
||||
WrDataLength=1;
|
||||
ui8OutputData[0]=val;
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
}
|
||||
|
||||
void T_prot::setUserWrite2DB(uint16_t WriteCmd, uint16_t WrAddr, uint8_t val0, uint8_t val1)
|
||||
{
|
||||
WriteCommand=WriteCmd;
|
||||
WriteAddr=WrAddr;
|
||||
WrDataLength=2;
|
||||
ui8OutputData[0]=val0;
|
||||
ui8OutputData[1]=val1;
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=0; // 0: binaries, 1:text
|
||||
}
|
||||
|
||||
void T_prot::setUserWriteText(uint16_t WriteCmd, uint16_t WrAddr, uint8_t WrDatLen, char *data)
|
||||
{
|
||||
WriteCommand=WriteCmd;
|
||||
WriteAddr=WrAddr;
|
||||
WrDataLength=WrDatLen;
|
||||
if (WrDataLength>FRAME_DATALEN)
|
||||
WrDataLength=FRAME_DATALEN;
|
||||
|
||||
for (int nn=0; nn<WrDataLength; nn++)
|
||||
chOut_Data[nn]=data[nn];
|
||||
SendDataValid=1; // always set WR first
|
||||
kindOfData=1; // 0: binaries, 1:text
|
||||
}
|
||||
|
||||
void T_prot::setUserReadData( uint16_t ReadCmd, uint16_t RdAddr, uint16_t reserv)
|
||||
{
|
||||
ReadCommand=ReadCmd;
|
||||
ReadAddr=RdAddr;
|
||||
reserve=reserv;
|
||||
SendDataValid |=2;
|
||||
}
|
||||
|
||||
void T_prot::setUserReadData( uint16_t ReadCmd, uint16_t RdAddr)
|
||||
{
|
||||
ReadCommand=ReadCmd;
|
||||
ReadAddr=RdAddr;
|
||||
reserve=0;
|
||||
SendDataValid |=2;
|
||||
}
|
||||
|
||||
void T_prot::setUserReadData( uint16_t ReadCmd)
|
||||
{
|
||||
ReadCommand=ReadCmd;
|
||||
ReadAddr=0;
|
||||
reserve=0;
|
||||
SendDataValid |=2;
|
||||
}
|
||||
|
||||
void T_prot::setBLsendData( uint8_t len, uint8_t *buf)
|
||||
{
|
||||
for (int nn=0; nn<BL_DATA_LEN; nn++)
|
||||
ui8BLsendData[nn]=0;
|
||||
|
||||
BLsendDataLength=len;
|
||||
if ( BLsendDataLength>BL_DATA_LEN) BLsendDataLength=BL_DATA_LEN;
|
||||
for (int nn=0; nn<BLsendDataLength; nn++)
|
||||
ui8BLsendData[nn]=buf[nn];
|
||||
WriteCommand=0xFFFF;
|
||||
|
||||
qDebug()<<"prot: got BL data " << len << "bytes, ";
|
||||
/*
|
||||
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];
|
||||
qDebug() <<buf[16] <<buf[17] <<buf[18] <<buf[19] <<buf[20] <<buf[21]<<buf[22]<<buf[23];
|
||||
qDebug() <<buf[24] <<buf[25] <<buf[26] <<buf[27] <<buf[28] <<buf[29]<<buf[30]<<buf[31];
|
||||
qDebug() <<buf[32] <<buf[33] <<buf[34] <<buf[35] <<buf[36] <<buf[37]<<buf[38]<<buf[39];
|
||||
qDebug() <<buf[40] <<buf[41] <<buf[42] <<buf[43] <<buf[44] <<buf[45]<<buf[46]<<buf[47];
|
||||
qDebug() <<buf[48] <<buf[49] <<buf[50] <<buf[51] <<buf[52] <<buf[53] <<buf[54]<<buf[55];
|
||||
qDebug() <<buf[56] <<buf[57] <<buf[58] <<buf[59] <<buf[60] <<buf[61] <<buf[62]<<buf[63];
|
||||
qDebug() <<buf[64] <<buf[65] <<buf[66] <<buf[67] <<buf[68] <<buf[69] <<buf[70]<<buf[71];
|
||||
qDebug() <<buf[72] <<buf[73] <<buf[74] <<buf[75] <<buf[76] <<buf[77] <<buf[78]<<buf[79];
|
||||
*/
|
||||
}
|
||||
|
||||
void T_prot::sendUserData(uint16_t slaveAdr)
|
||||
{
|
||||
// man könnte hier noch "SendDataValid" abfragen,
|
||||
// muss immer 3 sein, muss man aber nicht
|
||||
|
||||
//qDebug() << "prot send user data "<<slaveAdr;
|
||||
QByteArray packBuf_2;
|
||||
slaveAddr=slaveAdr;
|
||||
|
||||
if (WriteCommand==0xFFFF)
|
||||
{
|
||||
// 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]);
|
||||
mySerialPort->writeToSerial(packBuf_2, BLsendDataLength);
|
||||
|
||||
|
||||
} else
|
||||
startPacking();
|
||||
}
|
||||
|
||||
void T_prot::startPacking(void)
|
||||
{
|
||||
uint16_t mycrc;
|
||||
uint16_t uitmp, sendLen;
|
||||
uint8_t uctmp, nn, pp, CrcLp;
|
||||
char sendBuffer[FRAME_MAXLEN], ctmp;
|
||||
|
||||
//qDebug() << "prot start packing "<<slaveAddr;
|
||||
|
||||
for (int nn=0; nn<FRAME_MAXLEN; nn++)
|
||||
sendBuffer[nn]=0;
|
||||
|
||||
sendBuffer[0]='>';
|
||||
uitmp=slaveAddr;
|
||||
sendBuffer[1]= char(uitmp);
|
||||
uitmp>>=8;
|
||||
sendBuffer[2]= char(uitmp);
|
||||
|
||||
uitmp=WriteCommand;
|
||||
sendBuffer[3]= char(uitmp);
|
||||
uitmp>>=8;
|
||||
sendBuffer[4]= char(uitmp);
|
||||
|
||||
uitmp=WriteAddr;
|
||||
sendBuffer[5]= char(uitmp);
|
||||
uitmp>>=8;
|
||||
sendBuffer[6]= char(uitmp);
|
||||
|
||||
uitmp=ReadCommand;
|
||||
sendBuffer[7]= char(uitmp);
|
||||
uitmp>>=8;
|
||||
sendBuffer[8]= char(uitmp);
|
||||
|
||||
uitmp=ReadAddr;
|
||||
sendBuffer[9]= char(uitmp);
|
||||
uitmp>>=8;
|
||||
sendBuffer[10]= char(uitmp);
|
||||
|
||||
uitmp=reserve;
|
||||
sendBuffer[11]= '-'; //char(uitmp);
|
||||
uitmp>>=8;
|
||||
sendBuffer[12]= '-'; //char(uitmp);
|
||||
|
||||
sendBuffer[13]= char(WrDataLength);
|
||||
CrcLp= 14 + WrDataLength;
|
||||
|
||||
if (kindOfData) // 0: binaries, 1:text
|
||||
{
|
||||
for (nn=0; nn<WrDataLength; nn++)
|
||||
{
|
||||
pp=14+nn;
|
||||
ctmp=(chOut_Data[nn]);
|
||||
sendBuffer[pp]= ctmp;
|
||||
}
|
||||
|
||||
} else
|
||||
{
|
||||
for (nn=0; nn<WrDataLength; nn++)
|
||||
{
|
||||
pp=14+nn;
|
||||
uctmp=(ui8OutputData[nn]);
|
||||
sendBuffer[pp]= char(uctmp);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
sendBuffer[CrcLp+2]=13;
|
||||
sendBuffer[CrcLp+3]=10;
|
||||
sendLen+=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_com::writeToSerial(const QByteArray &data, uint16_t sendLength)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
// receiving.....
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
void T_prot::analyseRecData(void)
|
||||
{
|
||||
// Aufruf per connect aus serialcontrol wenn Daten empfangen wurden
|
||||
// getRecData(QByteArray &data, uint16_t &sendLength);
|
||||
|
||||
QByteArray Indata;
|
||||
QString myString, tempStr;
|
||||
//char recBuffer[FRAME_MAXLEN];
|
||||
uint8_t recBuffer[FRAME_MAXLEN];
|
||||
|
||||
uint16_t recLength;
|
||||
|
||||
INdataValid=false;
|
||||
gpi_setTxt4HsStateLine("");
|
||||
gpi_setTxt4masterStateLine("");
|
||||
gpi_setTxt4resultStateLine("");
|
||||
gpi_setTxt4dataStateLine("");
|
||||
gpi_setTxt4datifLine("");
|
||||
|
||||
// read from "VCP":
|
||||
mySerialPort->readFromSerial(Indata, recLength);
|
||||
|
||||
if (recLength>FRAME_MAXLEN)
|
||||
recLength=FRAME_MAXLEN;
|
||||
for (int nn=0; nn<recLength; nn++)
|
||||
recBuffer[nn]=uint8_t(Indata[nn]);
|
||||
|
||||
myString.clear();
|
||||
tempStr.clear();
|
||||
uint8_t result=FramecheckInData(recBuffer, recLength); // check input data (response from slave)
|
||||
if (result>0)
|
||||
{
|
||||
// dann anzeigen und ende
|
||||
switch (result)
|
||||
{
|
||||
case 1: gpi_setTxt4masterStateLine("wrong length received"); break;
|
||||
case 2: gpi_setTxt4masterStateLine("wrong start sign received"); break;
|
||||
case 3: gpi_setTxt4masterStateLine("received datalen too big"); break;
|
||||
case 4: gpi_setTxt4masterStateLine("wrong data len received"); break;
|
||||
case 5: gpi_setTxt4masterStateLine("wrong crc received"); break;
|
||||
|
||||
}
|
||||
myString.setNum(result);
|
||||
|
||||
gpi_storeRawReceivedData(uint8_t(recLength), recBuffer);
|
||||
emit rawDataRecieved();
|
||||
|
||||
} else
|
||||
{
|
||||
//& result ==0
|
||||
gpi_setTxt4masterStateLine("slave response OK");
|
||||
// Daten OK, also prüfe Inhalt.
|
||||
// Konnte der Slave das Master-Command verwenden oder hatte es Fehler?
|
||||
// konnte der Slave die geforderten Daten ausgeben (DOs, AOs)?
|
||||
// konnte der Slave die geforderten Daten einlesen (AIs, DIs)?
|
||||
|
||||
CheckInResult(recBuffer); // Ergebnisse des Slaves anzeigen
|
||||
|
||||
CheckInData(recBuffer); // Eingangs-Daten des Slaves anzeigen
|
||||
}
|
||||
emit framerecieved();
|
||||
//qDebug() << "framereceived emitted";
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t T_prot::FramecheckInData(uint8_t *Inbuf, uint16_t LL)
|
||||
{
|
||||
|
||||
uint16_t rawInLen=LL, crcL_Addr, recCrc, myCrc, nn, datalen, nxt;
|
||||
|
||||
if (rawInLen<12)
|
||||
{
|
||||
qDebug("prot: got %d bytes only", rawInLen);
|
||||
return 1; // wrong length
|
||||
}
|
||||
|
||||
if ( Inbuf[0] != '<')
|
||||
return 2; // wrong start sign
|
||||
|
||||
datalen=uint16_t(Inbuf[9]);
|
||||
if ( datalen > FRAME_DATALEN) //[9]=reported data lenght
|
||||
return 3; // reported datalen too big
|
||||
|
||||
if ((datalen !=(rawInLen-12)) && (datalen !=(rawInLen-13)) && (datalen !=(rawInLen-14)) )
|
||||
{
|
||||
// angehängtes CR und/oder LF tolerieren
|
||||
qDebug() << "wrong data length, " << datalen << " " << rawInLen;
|
||||
return 4; // data len does not match to complete length
|
||||
|
||||
}
|
||||
crcL_Addr=datalen+10; // weil im definierten protocol 10 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::CheckInResult(uint8_t *Inbuf)
|
||||
{
|
||||
char slaveresult;
|
||||
QString myString=nullptr, tempStr=nullptr;
|
||||
|
||||
// slave results anzeigen
|
||||
|
||||
slaveresult=Inbuf[2]; // hier steht das "Command Result" des slaves,
|
||||
// d.h das Ergebnis der Protokol-Prüfung (Master->Slave)
|
||||
switch (slaveresult)
|
||||
{
|
||||
// received message (from master) analysis:
|
||||
// 0: got valid request
|
||||
// this errors can only come back from a single device (not bus)
|
||||
// or from a bus slave in local mode
|
||||
// 1: wrong start 2: wrong length
|
||||
// 3: wrong crc 4: wrong addr
|
||||
|
||||
case 1: gpi_setTxt4resultStateLine("slave got wrong start sign"); break;
|
||||
case 2: gpi_setTxt4resultStateLine("slave got wrong length"); break;
|
||||
case 3: gpi_setTxt4resultStateLine("slave got wrong crc"); break;
|
||||
case 4: gpi_setTxt4resultStateLine("slave got wrong addr"); break;
|
||||
|
||||
case 10: gpi_setTxt4resultStateLine("slave is in local mode"); break;
|
||||
case 13: gpi_setTxt4resultStateLine("local mode with wrong crc"); break;
|
||||
case 14: gpi_setTxt4resultStateLine("local mode with wrong addr"); break;
|
||||
// wenn 1..4 dann konnte der Slave das Mastertelegramm gar nicht verwenden, also hier Stoppen
|
||||
}
|
||||
if (slaveresult>0 && slaveresult<10)
|
||||
return 1;
|
||||
|
||||
// Slave hat gültiges Kommando empfangen:
|
||||
// 2.result auswerten:
|
||||
// recBuffer[3]; // Write result, d.h. Ergebnis des Schreibvorganges (z.B. DOs) des Slaves
|
||||
// recBuffer[4]; // Read result, d.h. Ergebnis des Lesevorganges (z.B. DIs) des Slaves
|
||||
// bisher nicht bekannt welche Fehlercodes es gibt, also den code direkt ausgeben.
|
||||
// bisher bekannt: 0=OK
|
||||
|
||||
myString.clear();
|
||||
myString = "Slave OUT and IN Result: ";
|
||||
tempStr.setNum(Inbuf[3],16);
|
||||
myString.append(tempStr);
|
||||
myString.append(" ");
|
||||
tempStr.setNum(Inbuf[4],16);
|
||||
myString.append(tempStr);
|
||||
gpi_setTxt4resultStateLine(myString);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t T_prot::CheckInData(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()
|
||||
{
|
||||
return INdataValid;
|
||||
}
|
||||
|
||||
bool T_prot::getReceivedInData(uint8_t *SlavAddr, uint16_t *readSrc, uint16_t *readAddr,
|
||||
uint8_t *RdDlen, uint8_t *receivedData)
|
||||
{
|
||||
uint8_t nn;
|
||||
*SlavAddr=RecSlaveAddr;
|
||||
*readSrc=readSource; // diese (Eingangs-)Daten stehen im Puffer
|
||||
*readAddr=readAddress; // von dieser Adr wurden die Daten gelesen
|
||||
//*lastWakSourc=lastWakeSrc; // falls der Slave den Master geweckt hat
|
||||
*RdDlen=RdDataLength;
|
||||
for (nn=0; nn<FRAME_DATALEN; nn++)
|
||||
receivedData[nn]=0;
|
||||
for (nn=0; nn<RdDataLength; nn++)
|
||||
receivedData[nn]=InputData[nn];
|
||||
return INdataValid; // nur true wenn CommandState OK und readState OK
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user