Help on getting RealPort data to Node-Red

I have a Connectport X2D 900HP Ethernet that is set up as a coordinator and is sending data to the RealPort(Com1) from my Xbees that are configured as end devices using API 1.

I am using python code in and see the output I want in VS Code terminal:
Received data: PR01,055200,0013A200422BDF79 Sent data through RealPort: |PR01,055200,0013A200422BDF79|

My question is how can I get the data that comes through the RealPort into Node Red, and ultimately a SQL database? I’m somewhat familiar with Node-Red, but this is the first I’ve done one from the ground up. I’ve successfully connected RealPort(COM1) to a Serial COM port in Node-Red, but I have no idea what to do between the RealPort and Node-Red. Do I need to run a Python script on the ConnectPort X2D to send the data?

This is the code I used to get the data string above:

import socket
import time
from digi.xbee.devices import XBeeDevice
from digi.xbee.models.mode import APIOutputMode

# XBee module configuration
SERIAL_PORT = 'COM1'  # Update this with your ConnectPort serial port
BAUD_RATE = 115200

# RealPort configuration
REALPORT_IP = '192.168.0.14'  # Update this with your RealPort IP address
REALPORT_PORT = 771  # Default RealPort port

def connect_to_realport():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((REALPORT_IP, REALPORT_PORT))
    return sock

def main():
    device = None
    sock = None
    try:
        # Set up XBee device
        device = XBeeDevice(SERIAL_PORT, BAUD_RATE)
        device.open()
        device.set_api_output_mode_value(1)

        # Set up RealPort TCP connection
        sock = connect_to_realport()

        print("Listening for data and sending through RealPort...")

        while True:
            # Read data from the XBee module
            xbee_message = device.read_data()
            if xbee_message is not None:
                data = xbee_message.data.decode('ascii', errors='ignore')
                print("Received data: {}".format(data))
                try:
                    # Add delimiters around the data
                    delimited_data = f"|{data}|"
                    print("Sending delimited data: {}".format(delimited_data))
                    # Send the data through RealPort
                    sock.sendall(delimited_data.encode('ascii'))
                    print("Sent data through RealPort: {}".format(delimited_data))
                except Exception as e:
                    print("Failed to send data through RealPort: {}".format(e))
                    # Attempt to reconnect
                    try:
                        sock.close()
                    except Exception as close_err:
                        print(f"Failed to close socket: {close_err}")
                    print("Reconnecting to RealPort...")
                    time.sleep(5)  # Wait before attempting to reconnect
                    sock = connect_to_realport()
                    print("Reconnected to RealPort")

    except KeyboardInterrupt:
        print("Script interrupted by user")

    except Exception as e:
        print("An error occurred: {}".format(e))

    finally:
        # Close connections
        if device is not None and device.is_open():
            try:
                device.close()
            except Exception as e:
                print("Failed to close XBee device: {}".format(e))
        if sock is not None:
            try:
                sock.close()
            except Exception as e:
                print("Failed to close socket connection: {}".format(e))

if __name__ == "__main__":
    main()

Any help would be greatly appreciated. Thank you!

Yes, you would need to write and port over a python application to the X2D that reads the communicates with the XBee network and then does something with the data. Either that can be sent over Real port or via a TCP connection to an IP address and port you so desire.

Thank you for your response. This is where I am with the script, but I’m not receiving any data in Node Red. The TCP In node shows that it is listening on Port 771. Could you make any recommendations that may help?

import socket
import os
import time

# Serial port configuration
SERIAL_PORT = '/dev/ttyS0'  # Update this with the correct serial port on the ConnectPort X2D
BAUD_RATE = 115200

# Node-RED TCP server configuration
NODE_RED_IP = '192.168.0.11'  # Update this with the IP address of the machine running Node-RED
NODE_RED_PORT = 771           # Port number on which Node-RED is listening

def main():
    ser = None
    sock = None

    try:
        # Set up serial connection
        ser = open(SERIAL_PORT, 'rb')  # Open serial port without buffering

        # Set up TCP connection to Node-RED
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((NODE_RED_IP, NODE_RED_PORT))

        while True:
            try:
                # Read data from the serial port
                data = ser.read(1024).decode('ascii', 'ignore')
                if data:
                    delimited_data = "|{}|".format(data)
                    # Send the data to Node-RED
                    sock.sendall(delimited_data.encode('ascii'))
            except socket.error:
                try:
                    sock.close()
                except:
                    pass
                time.sleep(5)  # Wait before attempting to reconnect
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.connect((NODE_RED_IP, NODE_RED_PORT))
            except:
                pass

    except:
        pass

    # Cleanup outside of the main loop
    if ser is not None:
        try:
            ser.close()
        except:
            pass
    if sock is not None:
        try:
            sock.close()
        except:
            pass

if __name__ == "__main__":
    while True:
        try:
            main()
        except:
            time.sleep(5)  # Wait before retrying in case of a major error

You need to have a Python script set up on the X2D or on the X2D, go to its WebUI > XBee network> Gateway Access> Enable Serial access to the Gateway radio. Enabling this function will allow Realport to make a direct connection to the XBee module that is in the X2D that is in API mode.

Yes sir, I have done that. The code above is what I have loaded onto the X2D and I have set the script to Auto Start on reboot. I have also tried executing the script via SSH. It is running without any errors, but it doesn’t seem to be sending anything out to the TCP IN node in Node-Red.

There is also readable data coming through the Realport into Putty with a few weird characters. Are there any Node-Red nodes that you’re aware of that have been proven to work with RealPort? I’ve done a lot of searching and haven’t been able to find anything.

I’m also open to any other avenue you might suggest in order to get the data written to a database. I only chose Node Red because I’m somewhat familiar with it and have used it before.

Thanks!

None of the code you are showing is something that I would expect to be running on an X2D. This code appears to be more in line of what I might see on an IX15.

The suggestion of enabling serial access on the Gateway will allow Real port and the COM port is drivers are assigned to direct access to the XBee in the Gateway. Just understand that the XBee module in the Gateway will be using our HEX API frames which means all data being sent over the Realport COM port will be in Hex API characters. You can verify this by using XCTU’s Serial Console option.