#include "prot.h" #include #include "controlBus.h" #include "dcBL.h" #include "interfaces.h" T_prot::T_prot() { mySerialPort = new T_com(); connect(mySerialPort, SIGNAL(receivingFinished()), this, SLOT( analyseRecData() )); //connect(mySerialPort, SIGNAL(sendingFinished()), this, SLOT(sendeFin())); for (int nn=0; nnisPortOpen(); } bool T_prot::isSerialFree(void) { return true; // ohne HS's kann er nicht blockiert sein } // 14.9.23 Fehler!!!!! // hier wird vorausgesetzt dasss die Empfangslaenge von Sendetelegramm abh. ist. // also cmd>=100 ist kurze Antwort. das stimmt aber nicht, ist unabh. Wahr ist: cmd>=100 = kurzes Sendetelegramm // Laenge des request-telegramms (PTU-->DC) ist nur abh. vom WRcmd, die Antwort auf WR ist immer kurz (nur result byte) // WRcmd kurz oder lang: Antwort immer kurz // RDcmd kurz oder lang: Antwort genauso // Also Korrektur dieser Funktion: nur der Variablenname "WriteCmd" -> "ReadCmd" void T_prot::setRecLen(uint16_t ReadCmd) { if (ReadCmd<100) { RdDataLength=DATALEN_RECEIVE_LONG; // 64, store here already because it's no longer returned from slave mySerialPort->receiveFixLen(TELEGRAMLEN_RECEIVE_LONG); // 68 } else { RdDataLength=DATALEN_RECEIVE_FAST; // 8 mySerialPort->receiveFixLen(TELEGRAMLEN_RECEIVE_FAST); // 12 } } /* * 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); // 68 } else { RdDataLength=DATALEN_RECEIVE_FAST; mySerialPort->receiveFixLen(TELEGRAMLEN_RECEIVE_FAST); // 12 } } */ 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; nnsetRecLen(100); // default: short response } void T_prot::setUserWriteData(uint16_t WriteCmd, uint16_t WrAddr) { WriteCommand=WriteCmd; WriteAddr=WrAddr; WrDataLength=0; for (int nn=0; nnsetRecLen(100); // default: short response } void T_prot::setUserWriteData(uint16_t WriteCmd) { WriteCommand=WriteCmd; WriteAddr=0; WrDataLength=0; for (int nn=0; nnsetRecLen(100); // default: short response } 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 this->setRecLen(100); // default: short response } 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 this->setRecLen(100); // default: short response } 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; nnsetRecLen(100); // default: short response } void T_prot::setUserReadData( uint16_t ReadCmd) { ReadCommand=ReadCmd; //ReadAddr=WriteAddr; reserve=0; SendDataValid |=2; readAddress=WriteAddr; // store here already because it's no longer returned from slave //qDebug()<<"prot_setUserReadData, read address : " << readAddress; this->setRecLen(ReadCmd); // long response (68byte) only for ReadCmd < 100 readSource=ReadCmd; } void T_prot::setBLsendData( uint8_t len, uint8_t *buf) { for (int nn=0; nnBL_DATA_LEN) BLsendDataLength=BL_DATA_LEN; for (int nn=0; nnsetRecLen(100); //qDebug()<<"prot: got BL data " << len << "bytes, "; //for (int i=0; ireceiveFixLen(nrOfbytesToReceive); } 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 "<writeToSerial(packBuf_2, BLsendDataLength); } else 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 "<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>=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); } // --------------------------------------------------------------------------------------------------------- // 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); //qDebug()<<"prot: got data " << recLength; if (recLength>FRAME_MAXLEN) recLength=FRAME_MAXLEN; for (int nn=0; nn0) { // dann anzeige 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; case 6: gpi_setTxt4masterStateLine("slave: master cmd was wrong"); break; case 7: gpi_setTxt4masterStateLine("slave: could not write/read data"); break; } myString.setNum(result); // qDebug()<<"T_prot_analyseRecData: got BL data"<< recLength << // recBuffer[0]<< recBuffer[1]<< recBuffer[2]<< recBuffer[3] << // recBuffer[4]<< recBuffer[5]<< recBuffer[6]<< recBuffer[7]; // Daten abspeichern, könnten vom BL sein: gpi_storeRawReceivedData(uint8_t(recLength), recBuffer); INdataValid=false; emit rawDataRecieved(); } else { gpi_setTxt4masterStateLine("slave response OK"); INdataValid=true; ShowFastInData(recBuffer); // Eingangs-Daten des Slaves anzeigen } prot_storeResult=result; emit framerecieved(); // always emit, no matter if data valid or not } 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 ( (rawInLen0) schmarrn, kann nie kommen // qDebug() << "prot, got DC-result: "<< result; 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::ShowFastInData(uint8_t *recBuffer) { QString myString=nullptr, tempStr=nullptr; uint8_t result; 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; // 17.7. hier raus! //readSource already set with sending //readAddress=0; großer Fehler!!!! raus am 17.7 // RdDataLength already set with sending if (RdDataLength>FRAME_DATALEN) RdDataLength=FRAME_DATALEN; for (int ii=0; ii