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

  • 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>  

  • Realy ? nobody ?

  • 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_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
    void hwDebugPrint(const char *fmt, ... )
    	char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
    	// 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);
    	// prepend timestamp (AVR nodes)
    	MY_DEBUG_HWSERIAL.print(" ");
    	va_list args;
    	va_start (args, fmt );
    	// Truncate message if this is gateway node
    	vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
    	fmtBuffer[sizeof(fmtBuffer) - 2] = '\n';
    	fmtBuffer[sizeof(fmtBuffer) - 1] = '\0';
    	vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
    	va_end (args);

    The code above does not work!

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

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

Log in to reply

Suggested Topics

  • 1
  • 9
  • 10
  • 4
  • 1
  • 2
  • 5
  • 50