Skip to content
  • 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. Hardware
  3. Serial gateway using Arduino Mega, how to set what serial to use ?
  • Getting Started
  • Controller
  • Build
  • Hardware
  • Download/API
  • Forum
  • Store

Serial gateway using Arduino Mega, how to set what serial to use ?

Scheduled Pinned Locked Moved Hardware
5 Posts 2 Posters 3.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.
  • RedguyR Offline
    RedguyR Offline
    Redguy
    wrote on last edited by
    #1

    I am trying to build a serial <-> RS485 gateway using an Arduino Mega 2560 R3.

    I would like to use the Mega's 4 hardware serial interfaces as follows :

    Serial0 -> debug output (and programming) - This is the USB serial
    Serial1 -> Serial interface of the gateway to the Domoticz Controller
    Serial2 -> RS485 MySensors bus interface to the sensors nodes (using a MAX485 with direction connected to D2)

    Digging through the various library files and examples, I have found settings to configure the hardware serial to use for the RS485 interface : #define MY_RS485_HWSERIAL Serial2

    The setting does not turn green in the arduini IDE like the other defines, but it seems to be accepted if I try to compile the sketch.

    Until however, I have not bee able to find how I can setup the use of hardware serial 0 for debug output and hardware serial 1 as RS232 side of the serial gateway.

    Is this even possible ?

    Current settings so far :

    
    #define MY_GATEWAY_SERIAL                 // Enable serial gateway
    #define MY_BAUD_RATE 115200               // Set RS232 baud rate to Controller
    
    #define MY_DEBUG                                     // Enable debug prints to serial monitor
    
    #define MY_RS485                                            // Enable RS485 transport layer
    #define MY_RS485_HWSERIAL Serial2         // Use hardware serial port 2 for RS485
    #define MY_RS485_DE_PIN 2                         // Use D2 as DE-pin for RS485 
    #define MY_RS485_BAUD_RATE 9600         // Set RS485 baud rate to use
    
    
    #define MY_LEDS_BLINKING_FEATURE          // Flash leds on rx/tx/err
    #define MY_DEFAULT_LED_BLINK_PERIOD 300   // Set blinking period
    #define MY_DEFAULT_RX_LED_PIN  4  // Receive led pin
    #define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED
    #define MY_DEFAULT_ERR_LED_PIN 6  // Error led pin
    
    #include <MySensors.h>  
    
    
    1 Reply Last reply
    0
    • RedguyR Offline
      RedguyR Offline
      Redguy
      wrote on last edited by
      #2

      Realy ? nobody ?

      1 Reply Last reply
      0
      • vikmadV Offline
        vikmadV Offline
        vikmad
        wrote on last edited by
        #3

        On my Arduino Mega works for me this:

        // Enable debug prints to serial monitor
        //#define MY_DEBUG
        //#define MY_DEBUG_HWSERIAL Serial // = Serial0 / Rx0 & Tx0 to programming & debug 
        
        
        // Enable serial gateway
        #define MY_GATEWAY_SERIAL
        #define MY_SERIALDEVICE Serial3 // Rx3 & Tx3 to Gateway
        
        
        // Enable RS485 transport layer
        #define MY_RS485
        #define MY_RS485_HWSERIAL Serial1 // Rx1 & Tx1 to RS485 network 
        #define MY_RS485_BAUD_RATE 9600
        #define MY_RS485_DE_PIN 22
        

        I tried to edit a library MyHwAVR.cpp for MY_DEBUG_HWSERIAL, but without any positive result.:

        #ifdef MY_DEBUG
        #ifndef MY_DEBUG_HWSERIAL
        #define MY_DEBUG_HWSERIAL MY_SERIALDEVICE
        #endif
        void hwDebugPrint(const char *fmt, ... )
        {
        	char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
        #ifdef MY_GATEWAY_FEATURE
        	// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
        	snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
        	MY_DEBUG_HWSERIAL.print(fmtBuffer);
        #else
        	// prepend timestamp (AVR nodes)
        	MY_DEBUG_HWSERIAL.print(hwMillis());
        	MY_DEBUG_HWSERIAL.print(" ");
        #endif
        	va_list args;
        	va_start (args, fmt );
        #ifdef MY_GATEWAY_FEATURE
        	// Truncate message if this is gateway node
        	vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
        	fmtBuffer[sizeof(fmtBuffer) - 2] = '\n';
        	fmtBuffer[sizeof(fmtBuffer) - 1] = '\0';
        #else
        	vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
        #endif
        	va_end (args);
        	MY_DEBUG_HWSERIAL.print(fmtBuffer);
        	MY_DEBUG_HWSERIAL.flush();
        
        	//MY_DEBUG_HWSERIAL.write(freeRam());
        }
        #endif
        

        The code above does not work!

        1 Reply Last reply
        0
        • RedguyR Offline
          RedguyR Offline
          Redguy
          wrote on last edited by
          #4

          Ah that My_Serialdevice setting is something i completely missed..will test that this weekend..

          i am fine with the debug going to the usb port.. it's just that i can leave the usb to my pc for programming and debugging while another serial is connected to the raspberry pi running domoticz for the mysensors gateway part.

          1 Reply Last reply
          0
          • RedguyR Offline
            RedguyR Offline
            Redguy
            wrote on last edited by
            #5

            Tried ir, but no change.. Serial0 was still used while serial1was defined

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


            17

            Online

            11.7k

            Users

            11.2k

            Topics

            113.0k

            Posts


            Copyright 2019 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
            • OpenHardware.io
            • Categories
            • Recent
            • Tags
            • Popular