Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: ArduinoModbus porting question

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    7

    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 by Claritydnb; 06-09-2021 at 02:02 PM. Reason: modifying code

  2. #2
    Senior Member
    Join Date
    Oct 2016
    Posts
    1,222
    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().

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    28,474
    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).

    Click image for larger version. 

Name:	file1.png 
Views:	84 
Size:	30.1 KB 
ID:	25010

    (zoom in slightly so you can see each byte)

    Click image for larger version. 

Name:	file2.png 
Views:	57 
Size:	28.7 KB 
ID:	25011

  4. #4
    Junior Member
    Join Date
    Nov 2019
    Posts
    7
    Excellent, Thank you for your help. I will try those items.

  5. #5
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    1,089
    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.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    28,474
    Quote Originally Posted by jonr View Post
    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...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/co...arduino.h#L183

  7. #7
    Junior Member
    Join Date
    May 2022
    Posts
    1
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •