Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. RS485 & SoftwareSerial

RS485 & SoftwareSerial

Scheduled Pinned Locked Moved Development
8 Posts 4 Posters 1.4k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PawelD
    wrote on last edited by
    #1

    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.

    mfalkviddM msmacsM 2 Replies Last reply
    0
    • P PawelD

      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.

      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by mfalkvidd
      #2

      @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?

      P 1 Reply Last reply
      0
      • K Offline
        K Offline
        kimot
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • mfalkviddM mfalkvidd

          @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?

          P Offline
          P Offline
          PawelD
          wrote on last edited by PawelD
          #4

          @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?

          K 1 Reply Last reply
          1
          • P PawelD

            @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?

            K Offline
            K Offline
            kimot
            wrote on last edited by kimot
            #5

            @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.

            P 1 Reply Last reply
            0
            • K kimot

              @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.

              P Offline
              P Offline
              PawelD
              wrote on last edited by PawelD
              #6

              @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

              1 Reply Last reply
              0
              • P PawelD

                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.

                msmacsM Offline
                msmacsM Offline
                msmacs
                wrote on last edited by
                #7

                @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

                P 1 Reply Last reply
                2
                • msmacsM msmacs

                  @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

                  P Offline
                  P Offline
                  PawelD
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  1
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  32

                  Online

                  11.7k

                  Users

                  11.2k

                  Topics

                  113.1k

                  Posts


                  Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • MySensors
                  • OpenHardware.io
                  • Categories
                  • Recent
                  • Tags
                  • Popular