Hi All
I am attempting implement ArduinoModbus
on my STM32 Poternta H7 using the breakout board.
Its well documented that in ArduinoRS485
that the RS485 object is hardwired. If you are using the PH7/Breakout board you need to reconfigure to make sure that the RS485 is set to Serial1/Uart1.
After i have reconfigured, when i run my code i intially can write debug messages to the Serial monitor via Serial
(Onboard USB C). But once i start using RS485 on Serial1
i can no longer print to the serial monitor via Serial
and the code freezes.
What i did to re configure the RS485 obj. I implemented the following based on this work around from Arduino ArduinoModbus for other shields and with different Serial port - MKR SHIELDS - Arduino Forum
Then saved my updated ArduinoRS485 in /lib.
In ArduinoRS485.cpp
void RS485Class::setSerial(HardwareSerial* serial) {
_serial = serial;
}
In ArduinoRS485.h
void setSerial(HardwareSerial* serial);
In my setup code i defined;
#define UART1_TX_PIN PA_9 // J1 header 33 (LPUART) = SERIAL1_TX = R0
#define UART1_RX_PIN PA_10 // J1 header 35 (LPUART) = SERIAL1_RX = DI
#define UART1_RTS_PIN PI_14 // J1 header 37 (LPUART) = SERIAL1_RTS = REn pin
#define UART1_CTS_PIN PI_15 // J1 header 39 (LPUART) = SERIAL1_CTS = DE
#define SERIAL_PORT_HARDWARE Serial1
My ModbusClient set up
void modbusClientSetup() {
while (!Serial);
Serial.println("");
Serial.println("Modbus RTU Client Test");
RS485.setPins(UART1_TX_PIN, UART1_RX_PIN, UART1_RTS_PIN);
RS485.setDelays(50,50);
RS485.setSerial(&SERIAL_PORT_HARDWARE);
if(!ModbusRTUClient.begin(9600,SERIAL_8N1 )){
//Serial.println("Failed to start Modbus RTU Client!");
while (1);
}
}
The line Serial.println("Modbus RTU Client Test");
works fine.
Then i run this code to test my Modbus
void writeCoilValues() {
// set the coils to 1 when counter is odd
byte coilValue = ((counter % 2) == 0) ? 0x00 : 0x01;
Serial.print("Writing Coil values ... ");
// write 10 Coil values to (slave) id 1, address 0x00
ModbusRTUClient.beginTransmission(slaveID, COILS, 0x15, 10);> for (int i = 0;
i < 10; i++) {
counter = i;
coilValue = ((counter % 2) == 0) ? 0x00 : 0x01;
ModbusRTUClient.write(coilValue);
delay(100);
if (!ModbusRTUClient.endTransmission()) {
Serial.print("failed! ");
errClient = ModbusRTUClient.lastError();
Serial.println(errClient);
} else {
errClient = "All Good";
Serial.println("success");
}
}
The line Serial.print("Writing Coil values ... ");
does not print but the code does not freeze.
But once i get to Serial.print("failed! ");
the code just freezes at write_op.wait(NULL);
within USBCDC.cpp
below.
bool USBCDC::send(uint8_t *buffer, uint32_t size)
{
lock();
AsyncWrite write_op(this, buffer, size);
_tx_list.add(&write_op);
unlock();
write_op.wait(NULL);
return write_op.result;
}
I have been stepping thru this for the last days, but i still cant find where if fails. if anyone has an idea or a better method please let me know.
Cheers