ArduinoModbus porting question

Hello all. I am looking to use a teensy 4.1 as a Modbus RTU Client over RS485. It seems that it is attempting to use the Serial port associated with the USB. I need it to operate on Serial1. Is there a way to do this?
Code:
#include <Arduino.h>
#include <Metro.h>
#include <SPI.h>
#include "mcp23s17.h"
#include <Wire.h>
#include "MCP342x.h"
#include <FlexCAN_T4.h>
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

bool clientStarted = 0;

void setup()
{
clientStarted = ModbusRTUClient.begin(57600);
  while (!clientStarted){
    Serial.println("Client Error");
  }
}

void loop {

      ModbusRTUClient.beginTransmission(15, HOLDING_REGISTERS, 0x100, 6);
      ModbusRTUClient.write(ALowOutput);
      ModbusRTUClient.write(AHighOutput);
      ModbusRTUClient.write(BLowOutput);
      ModbusRTUClient.write(BHighOutput);
      ModbusRTUClient.write(CLowOutput);
      ModbusRTUClient.write(CHighOutput);
      
      if (!ModbusRTUClient.endTransmission()){
        Serial.println ("failed! ");
        Serial.println (ModbusRTUClient.lastError());
      }
      else{
        Serial.println("Success");
      }
}

The teensy just locks up at the begin(57600) function. After that it won't respond to teensyduino or run the program. It seems like it is trying to start on the same serial port. Thanks in advance for your help
 
Last edited:
The Modbus RTU client uses the default RS485 object from ArduinoRS485, and that library uses macros for default TX/RX pin assignments. The library documentation isn't very complete, but it looks like you need to do two things. The first is create an instance of RS485Class that uses Serial1 and the correct pin assignments. The second is to use the form of ModbusRTUClient.begin() that allows you to pass a reference to the RS485Class object. See ModbusRTUClient.h for the signature of that form of begin().
 
This library doesn't even compile. Neither does the code from msg #1.

Here's a fixed copy of the library.
https://github.com/PaulStoffregen/ArduinoModbus

To get the code above to compile, I had to replace undefined variables with numerical constants, and also fix a syntax error in the loop function definition. Here is a copy which compiles (when used with the fixed copy of ArduinoModbus).

Code:
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

bool clientStarted = 0;

void setup() {
  clientStarted = ModbusRTUClient.begin(57600);
  while (!clientStarted) {
    Serial.println("Client Error");
  }
}

void loop() {
  ModbusRTUClient.beginTransmission(15, HOLDING_REGISTERS, 0x100, 6);
  ModbusRTUClient.write(100);
  ModbusRTUClient.write(101);
  ModbusRTUClient.write(102);
  ModbusRTUClient.write(103);
  ModbusRTUClient.write(104);
  ModbusRTUClient.write(105);

  if (!ModbusRTUClient.endTransmission()) {
    Serial.println ("failed! ");
    Serial.println (ModbusRTUClient.lastError());
  }
  else{
    Serial.println("Success");
  }
}

I ran this on a Teensy 4.0. I see it's transmitting Modbus-looking stuff on pin 1 (Serial1 TX1).

file1.png

(zoom in slightly so you can see each byte)

file2.png
 
I'm curious - I don't see any references to Serial1 in the code or any indication that it knows about teensy hw registers. So how can it output to Serial1? Clearly I missed something.
 
I don't see any references to Serial1 in the code or any indication that it knows about teensy hw registers. So how can it output to Serial1?

Look at the last line of RS485.cpp

https://github.com/arduino-libraries/ArduinoRS485/blob/master/src/RS485.cpp#L181

It's depending on whatever the board's core library defines for SERIAL_PORT_HARDWARE

For Teensy 4.x, this is defined in pins_arduino.h

https://github.com/PaulStoffregen/c...a483c96dea7b47d3c/teensy4/pins_arduino.h#L183
 
Hello!

Sorry to dig this thread back up, but it appears to be the most recent discussion about implementing Modbus RTU on a Teensy 4.1 I'm interested in communicating with a device over Modbus RTU but am scratching my head at what transceiver chip/library combination to use. Ideally I'd like an existing breakout board but can't find one that will work. I was thinking potentially this board; or this with a logic level converter to work with Teensy 4.1 3.3V logic. The sensor I'm looking to communicate here is an Alicat Mass Flow Meter

Any insight/suggestions would be much appreciated!
 
Back
Top