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!