Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4615

Troubleshooting • RPi4b UART0 missing bytes from packet

$
0
0
I'm pretty stumped with this issue, so hopefully someone can tell me if I'm missing something.

The basic premise of my project is that I have a Pi4b communicating with an MCU (ATSAMDA1E16B) on a custom board via UART0 (GPIO pins 14 & 15, with BT disabled) and RS485 transceiver chips (MAX14781EESA+). The Pi GPIO header is connected to a custom board which has the RS485 chip, which sends the UART across a semi-long (~30ft) CAT5 cable to the peripheral custom board which has another RS485 chip which feeds the UART to the SAM MCU. *I don't think this info will be super pertinent to the troubleshooting as I've already ruled out issues with RS485 and hardware*

The communication uses a packet protocol where the SAM MCU is taking sensor readings constantly and waiting to receive a command packet from the Pi. Once a packet is received, it returns a packet containing sensor data back to the Pi. Using a debugger I'm able to confirm that the command packet from the Pi is being received properly by the MCU, but when returning the data packet to the Pi the first 8 bytes are missing. By probing the Rx line of the Pi I can see that those bytes are present (meaning there is no issue with RS485 blocking part of the packet).

I'm using the PiGPIO library, and the relevant portion of my code is this:

Code:

PacketProcessor packetHandler;int PacketProcessor::run(){  //Processing Incoming if present, otherwise transmit  if(0 < serDataAvailable(h))  {    printf("Got serial data!\n");    //wait 1 ms for packet to be received    gpioDelay(1000);    int t_bytesToRead = serDataAvailable(h);    printf("There are %d bytes to read.\n", t_bytesToRead);    for(int i = 0; i < t_bytesToRead; i++)    {      inputBuffer[i] = serReadByte(h);      printf("Data: %X\n", inputBuffer[i]);    }          printf("Validity Check start\n");           //Check packet validity    int t_packetLength = inputBuffer[PACKET_LENGTH_INDEX];    bool t_packetLengthValid = (t_packetLength == t_bytesToRead);    int t_startOfPacket = 0;    int t_endOfPacket   = t_packetLength - 1;    bool t_recoveryCompleted = false;    //Possibly a bad packet    if(!t_packetLengthValid)    {        //attempt to search for start delimiter        for(int i = 1; i < t_endOfPacket && (!t_recoveryCompleted); i++)        {            if(inputBuffer[i] == PACKET_START_DELIMITER)            {                // Serial.print("  Start delimiter found at index: ");                // Serial.println(i);                // Log.info("  Start delimiter found at index: %d", i);                if(inputBuffer[ i + PACKET_LENGTH_INDEX ] + i == t_bytesToRead)                {                    // Serial.println("  Length Match! Shifting packet");                    // Log.info("  Length Match! Shifting packet");                    for(int j = 0; j < i; j++) {                        char *temp = logBuffer;                        temp += sprintf(temp, "%X - ", inputBuffer[j]);                    }                    // Log.info("  Bytes Removed: %s", logBuffer);                    //shift it all over                    int packetLength = inputBuffer[ i + PACKET_LENGTH_INDEX ];                    for(int j = 0; j < packetLength ; j++)                    {                        // Log.info("Copying: %X from index: %d to index: %d", inputBuffer[j + i], j+i, j);                        inputBuffer[j] = inputBuffer[j + i];                    }                    //Update parameters                    t_packetLength = inputBuffer[PACKET_LENGTH_INDEX];                    t_endOfPacket = t_packetLength - 1;                    t_packetLengthValid = true;                    // Serial.println("  Recovery complete");                    // Log.info("  Recovery complete");                    t_recoveryCompleted = true;                    // break;                }            }        }    }    //if bytes read matches length    if(t_packetLengthValid)    {        if( (PACKET_START_DELIMITER == inputBuffer[0]) &&            (PACKET_END_DELIMITER == inputBuffer[t_endOfPacket]))        {            int t_CRC = (inputBuffer[t_endOfPacket - 2] << 8) | inputBuffer[t_endOfPacket - 1];            int t_CRCCalced = calculateCRC(inputBuffer,  t_packetLength -3);            // Serial.print("CRC in packet: ");            // Serial.println(t_CRC,HEX);            // Serial.print("CRC calced: ");            // Serial.println(t_CRCCalced,HEX);            // Log.info("CRC in packet: %X", t_CRC);            // Log.info("CRC calced: %X", t_CRCCalced);            if(t_CRC == t_CRCCalced)            {                // Serial.println("CRC Match!");                // Log.info("CRC Match!");                inputPacketPayloadLength = t_packetLength - 3 - PACKET_PAYLOAD_START_INDEX; // Isolate Payload Length                // Serial.print("Payload Length: ");                // Serial.println(inputPacketPayloadLength);                // Serial.println("\r\n");                // Log.info("Payload Length: %d", inputPacketPayloadLength);                inputPendingPacket = 1;            }        }    } else {        for(int j = 0; j < t_bytesToRead; j++) {            char *temp = logBuffer;            temp += sprintf(temp, "%X - ", inputBuffer[j]);            // Log.info("  Byte Skipped: %X", )        }        printf("  Invalid Packet: %s\n", logBuffer);    }    unsigned long readTime = micros()-testTime;    // Log.info("PacketProcessor.run.read time: %lu", readTime);  }  else  {    if(0 != outputPendingPacket)    {      printf("Sending serial data!\n");      printf("Packet Contents: %X %X %X %X %X %X %X %X %X %X\n\n", outputBuffer[0], outputBuffer[1], outputBuffer[2], outputBuffer[3], outputBuffer[4], outputBuffer[5], outputBuffer[6], outputBuffer[7], outputBuffer[8], outputBuffer[9]);      // Log.info("Sending serial data!");                  //Serial1.write(outputBuffer, outputPacketLength);            //set TXE pin high      gpioWrite(UART_TEMP_TE_PIN, 1);      gpioSleep(PI_TIME_RELATIVE, 0, 100);            //send packet      serWrite(h, outputBuffer, outputPacketLength);      outputPendingPacket = 0;      gpioSleep(PI_TIME_RELATIVE, 0, 1040);            //set TXE pin low      gpioWrite(UART_TEMP_TE_PIN, 0);      gpioSleep(PI_TIME_RELATIVE, 0, 100);    }  }    return 0;}void processSerial(){    static unsigned long timeOfLastUpdate = 0;    unsigned long currentTime = millis();    if(125 <= ((currentTime - timeOfLastUpdate) & 0xFFFFFFFFFFFFFFFF))    {        //printf("processSerial timer triggered\n");        timeOfLastUpdate = currentTime;        packetHandler.run();    }}int main(void){if (gpioInitialise() < 0){printf("PiGPIO library initialization failed.\n");return 0;}//set Pi pinmodesassignGpioMode();packetHandler.begin();while(1){processSerial();}return 0;}
The prints from running this code look like this:

Code:

Serial port initialized!Sending serial data!Packet Contents: F5 7 10 20 5 49 FE 0 0 0Got serial data!There are 15 bytes to read.Data: C6Data: EAData: 60Data: 0Data: C6Data: EAData: 60Data: 0Data: C6Data: EAData: 60Data: 0Data: 55Data: 22Data: FEValidity Check start  Invalid Packet: FE - 
The correct packet is {F5, 17, 10, 20, C6, EA, 60, 00, C6, EA, 60, 00, C6, EA, 60, 00, C6, EA, 60, 00, 55, 22, FE}, so I know it's just missing the first 8 bytes. But the bytes are present on the Rx pin so I'm not sure why they aren't being read.

I know this is complicated and I probably missed including something, but any input would be much appreciated.

Statistics: Posted by joeyb2000 — Tue Apr 09, 2024 8:46 pm — Replies 0 — Views 21



Viewing all articles
Browse latest Browse all 4615

Trending Articles