I want to read out a device through Modbus communication with a mkr 485 shield and the M4 of the Portenta H7.
When i insert the line #include <ArduinoRS485.h> i have
...Documents\Arduino\progs\libraries\ArduinoRS485\src\RS485.cpp:189:18: error: 'SERIAL_PORT_HARDWARE' was not declared in this scope
RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);
How can i fix this?
b707
November 1, 2022, 10:09am
2
It seems a known issue and don't fix yet. You can find a possible workaround in this thread:
opened 06:41PM - 29 Mar 21 UTC
type: enhancement
topic: code
Currently (1.0.0), the library automatically creates a RS485 object running on t… he `SERIAL_PORT_HARDWARE` serial port (https://github.com/arduino-libraries/ArduinoRS485/blob/master/src/RS485.cpp#L181).
Although it might be idiomatic for Arduino libraries to automatically create an object to work with, for this library it causes trouble on some platforms and use cases:
- If you want to use a different Serial port than `Serial`, you still have to live with the automatically created RS485 object on `Serial`, which is a waste of memory.
- On some platforms (e.g. some STM32), `Serial` is of type `USBSerial`. As the RS485 constructor only accepts `HardwareSerial`, and you cannot set another Serial port, this causes that the library cannot be compiled for these platforms. Workaround is to remove L181 of RS485.cpp.
So actually, I propose two changes:
- Use `Stream` as interface type for the RS485 constructor, so it can be created with `HardwareSerial` as well as with `USBSerial` interfaces.
- Do not create a RS485 object on `SERIAL_PORT_HARDWARE`, but rather require the user to actively chose the desired port as a parameter in the `begin` method (I guess it is ok to have `SERIAL_PORT_HARDWARE` as the default value for the port parameter.) Drawback: This change is not backwards-compatible. Current users of the library will have to add a line to create the RS485 object manually.