RS485 & SoftwareSerial



  • Re: How to use SoftwareSerial Library?

    Hi.

    Anyone managed to run SoftwareSerial on RS485 gateway?
    I tried to do everything according to what is said in the link above, but with no success.

    This works (using hardware serial1 of my Mega):

    #define MY_RS485
    #define MY_RS485_DE_PIN 3
    #define MY_RS485_BAUD_RATE 9600
    #define MY_RS485_HWSERIAL Serial1
    #define MY_GATEWAY_SERIAL
    #include <MySensors.h>
    
    void setup(){}
    
    void presentation(){}
    
    void loop(){}
    

    and this one doesn't work (trying to use SoftwareSerial pins 4 and 2)

    #define MY_RS485
    #define MY_RS485_DE_PIN 3
    #define MY_RS485_BAUD_RATE 9600
    
    #include <SoftwareSerial.h>
    SoftwareSerial mySerial(4,2); //rx, tx
    #define MY_RS485_HWSERIAL 
    #define MY_RS485_SWSERIAL mySerial
    
    #define MY_GATEWAY_SERIAL
    #include <MySensors.h>
    
    void setup(){}
    
    void presentation(){}
    
    void loop(){}
    

    I also made a change in MyTransportRS485.cpp

    before:

    #if defined(__linux__)
    SerialPort _dev = SerialPort(MY_RS485_HWSERIAL);
    #elif defined(MY_RS485_HWSERIAL)
    HardwareSerial& _dev = MY_RS485_HWSERIAL;
    #else
    AltSoftSerial _dev;
    #endif
    

    and after:

    #if defined(MY_RS485_SWSERIAL)
    SoftwareSerial& _dev = MY_RS485_SWSERIAL;
    #elif defined(__linux__)
    SerialPort _dev = SerialPort(MY_RS485_HWSERIAL);
    #elif defined(MY_RS485_HWSERIAL)
    HardwareSerial& _dev = MY_RS485_HWSERIAL;
    #else
    AltSoftSerial _dev;
    #endif
    

    What am I doing wrong?
    I'm using MySensors 2.3.1 library.


  • Mod

    @paweld MY_RS485_SWSERIAL does not exist in the MySensors code. You'll need to modify the library in the way mentioned in https://forum.mysensors.org/post/71505

    Edit: Sorry, I see that you already did.
    What happens if you don't define MY_RS485_HWSERIAL at all?

    With "doesn't work" do you mean that the sketch compiles and can be uploaded, but communicating on the software serial pins does not work? Or does the code not compile?



  • A long time before I try serial gateway from MySensors example and it works without any modification.
    My configuration:
    Domoticz
    Pro mini like serial gateway - hw serial to Domoticz, sw serial to another Pro mini with serial motion node example with serial sw from MySensors too.
    But no RS485 drivers, only sw serial tx and rx pins cross wired.
    And do not forgot assign node_id in your sketch.



  • @mfalkvidd
    I tried removing "define MY_RS485_HWSERIAL" but it didn't help.
    My code compiles correctly, but there is no communication.
    Hardware setup is identical for both examples, except for the rx/tx pins used.
    This is very simple setup: a button connected to Nano+Max485 module, and Mega+Max485 gateway, connected via RS485.

    @kimot
    I have node_id assigned in my Nano node's code.

    @kduino @hek maybe you can help here?



  • @paweld
    And must you really use SoftwareSerial and not AltSoftSerial, which is default for MySensors RS485 link layer?
    You do not need include SoftwareSerial in your sketch then.



  • @kimot
    I rejected AltSoftSerial library because:

    1. My shields operate on pins 0 to 5. AltSoftSerial has fixed pins 8,9 for Uno and 46,48 for Mega.
    2. I want to keep pins 0 and 1 for debug/controller communication
    3. I want to use two RS485 shields on my Mega: one using pins 2,3, and second pins 4,5.

    Here you can see the setup I'm talking about:
    Zihatec RS485 Multiple busses



  • @paweld Hi, I was just reading the "Limitations" section on the official page: https://www.arduino.cc/en/Reference/softwareSerial
    You are using a "Mega" board so it seems that with the SoftwareSerial library you can only use a subset of the available pins (actually the limitation is for the RX pin).

    Just to confirm, could you try to use different pins as described in the "Limitations"?

    Max



  • @msmacs
    Thanks a lot. This is it.
    I tried pins 10, 11, 12 (RX / TX / DE-RE) and it worked.
    Problem solved.


 

265
Online

8.7k
Users

9.5k
Topics

99.9k
Posts