Am 04.07.2012 14:04, schrieb Daniel Kirkham:
> But regarding the modbus code, my initial thoughts on any callback
> functions (before I saw your code) is that they should be designed so
they
> don't have to be aware of the modbus message coding. That is:
Hello Daniel,
this matches my intentions only partially.
> * libmodbus decodes and encodes the messages
In my proposal this is done except for the data to be written or read.
> * user callback functions are passed or return native uint16_t values (for
> registers) and uint8_t (for bits)
In my use case accessing native uint16_t values is not what I need. It
would even make it more difficult
Only a small fraction of my data is uint16_t. There are many 32-bit
integers, 32- and 64-bit floats and string values. All numeric values
are already stored as big-endian. That means I simply have to copy the
data between my data structure and request/response buffer.
(This is what prevents me from using the unmodified library with
manipulating the address in the request and the pointers in the mapping
structure.)
For a user like you who wants to access native 16-bit integer values I
would add conversion functions that can be used in the callback functions.
Something like this:
myWriteCallback(... uint8_t *inputData, int inputByteCount, ...)
{
uint16_t wordData[MAX_SIZE];
int wordDataCount = sizeof(wordData) / sizeof(wordData[0]);
modbus_raw_to_uint16(inputData, inputByteCount, wordData,
&wordDataCount);
writeDataFromWordDataToSomewhere();
}
myWriteCallback(... uint8_t *outputData, int *outputByteCount, ...)
{
uint16_t wordData[MAX_SIZE];
int wordDataCount;
readDataFromSomewhereToWordDataAndFillWordDataCount();
modbus_uint16_to_raw(wordData, wordDataCount,
outputData, outputByteCount);
}
The conversion functions would check the buffer size, convert the data
using a htons/ntohs loop and return the resulting size.
As optimization the functions could use memcpy if htons/ntohs does not
modify the data.
> * user callback functions do not have access to the modbus message buffers
> (both req and rsp)
Currently my callback functions will get a pointer to the location
inside the message buffer where the data has to be written or read.
That means the callback functions don't have to (and should not) care
about header, checksum etc.
Bodo