I'm running a modbus client that is trying to set a bunch of coils on a server using function 15.
ModbusRTUClient.beginTransmission(i, COILS, 3200, 8);
ModbusRTUClient.write(MPOutputs[i - 1]);
ModbusRTUClient.endTransmission();
except it only sets the first coil instead of all 8 of them. MPOutputs being a byte array
whose bits correspond to the 8 coils since that's how function 15 works... so for 8 coils I need 1 whole byte to go out the RS-485 line.
Think I've solved it myself... It's simply not documented or designed in a way that makes any sense.
How I thought it worked(the way that makes sense if you know anything about modbus behind the scenes)
BeginTransmission() asserts D pin to enable tx and writes out a header according to the stuff you've told it
write() works simply as it always does by spitting a char array out the serial port (but also calculates CRC on them)
then endTransmission() sends the CRC and drops the driver enable signal
This way makes sense ^^^^^^^^^
except that write() works completely differently than it does in literally every single use in Arduino... for coils it expects 1 entire int per individual coil to be written out, not 1 char per * coils like modbus uses. This is not mentioned in the refrence (it should be)
A far better way would be to have 2 different sets of functions for registers and coils...... ORRRR
create a second write() overload that accepts bool, which makes it instantly clear to the user that it
is expecting only 1 bit.