Hi,
I'm using ESP32 module and It communicates Arduino mega via UART using SerialTransfer.h. But when I tried to transmit struct, ESP32 is rebooting.
The function is below.
Yes It gives me PC: 0x400d71d7: SerialTransfer::sendData(unsigned short const&, unsigned char) at C:\Users\Documents\Arduino\libraries\SerialTransfer\src\SerialTransfer.cpp line 66 EXCVADDR: 0x00000000
This is very strange for me: you are passing a variable sendSize as parameter of function txObj() and assigning the result of function to the same variable.
As my opinion the second parameter of function should be the size of packet, not the local variable uint16_t sendSize = 0;
But SerialTransfer library says this. I did it according to that example.
void loop()
{
// use this variable to keep track of how many
// bytes we're stuffing in the transmit buffer
uint16_t sendSize = 0;
///////////////////////////////////////// Stuff buffer with struct
sendSize = myTransfer.txObj(testStruct, sendSize);
///////////////////////////////////////// Stuff buffer with array
sendSize = myTransfer.txObj(arr, sendSize);
///////////////////////////////////////// Send buffer
myTransfer.sendData(sendSize);
delay(500);
}
I didn't know this library, but I've seen that it use an internal buffer. Example should be a little better explained as my opinion, because it use the same variable for 2 different purposes and this is confusing: first to allocate different objects in the tx buffer using sendSize as index (with the two txObj() instructions) and then to store the amount of bytes sent.
Anyway, if you put the complete code, I can try to debug with ESP-PROG debugger if you want in order to get exactly the line causing exception.
/*
uint16_t SerialTransfer::txObj(const T &val, const uint16_t &index=0, const uint16_t &len=sizeof(T))
Description:
------------
* Stuffs "len" number of bytes of an arbitrary object (byte, int,
float, double, struct, etc...) into the transmit buffer (txBuff)
starting at the index as specified by the argument "index"
Inputs:
-------
* const T &val - Pointer to the object to be copied to the
transmit buffer (txBuff)
* const uint16_t &index - Starting index of the object within the
transmit buffer (txBuff)
* const uint16_t &len - Number of bytes of the object "val" to transmit
Return:
-------
* uint16_t maxIndex - uint16_t maxIndex - Index of the transmit buffer (txBuff) that directly follows the bytes processed
by the calling of this member function
*/
template <typename T>
uint16_t txObj(const T& val, const uint16_t& index = 0, const uint16_t& len = sizeof(T))
{
return packet.txObj(val, index, len);
}