Python Socket library TCP client to Node-Red TCP Server Node not working

Hi,

I am trying to send Python TCP client message to Node-Red TCP Server Node.

I am using Python Socket library.

But however i am not able to receive the message in my Node-Red TCP in Server Node.
I am able to receive Python TCP client message to Python TCP Server via the Python Socket Library.

Please help.

Please provide details of the error you see and the code you are using.

See About the Python Help category for how to post code here.

Hi @barry-scott,

Below is my Python TCP Client Source code

import serial
import socket
import datetime
import time

def connect_to_serial_port(port="COM3", baud_rate=115200, timeout=1):
    ser = serial.Serial(port, baud_rate, timeout=timeout)
    return ser

def send_scpi_command(ser, command):
    ser.write(command.encode() + b'\n')

def read_scpi_response(ser):
    result = ser.readline().decode().strip('OK/nOK/nOK/n')
    final = result.split('ADC')
    num = float(final[0])
    num = int(num * 1000)
    return num

def measure_current(ser):
    scpi_cmd = "conf:curr"
    send_scpi_command(ser, scpi_cmd)
    print("Current configuration set.")
    time.sleep(1)  # 2-second interval
    scpi_cmd = "meas:show?"
    send_scpi_command(ser, scpi_cmd)

    response = read_scpi_response(ser)
    timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(f"[{timestamp}] Instrument response for current measurement: {response}")

    with open("instrument_log.txt", "a") as file:
        file.write(f"[{timestamp}] Current Measurement:  {response}\n")

def send_data_to_receiver(ip, port, response):
    sender_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    receiver_address = (ip, port)
    sender_socket.sendto(str(response).encode(), receiver_address)
    sender_socket.close()
    

def main():
    receiver_ip = "127.0.0.1"
    receiver_port = 139
    ser1 = connect_to_serial_port()
    print("Connected to the instruments.")
    
    try:
        while True:
            response1 = measure_current(ser1)
            #response2 = measure_current(ser2)
            
            #response2s = str(response2)
            send_data_to_receiver(receiver_ip, receiver_port, response1)
            #send_data_to_receiver(receiver_ip, receiver_port, response2s)
             # Add a delay between measurements
    except KeyboardInterrupt:
        pass  # Allow the user to exit the loop with Ctrl+C
    finally:
        ser1.close()
        
        

if __name__ == "__main__":
    main()

Node red Server node flow
image

Is this a coincidence, or are you attempting to talk NETBIOS protocol here?

The ip address and the port is supposed to be the IPV4 address and port number. As I forget to change

Yeah, just wondering at the use of port 139. If that was a completely arbitrary choice on your part, then no probs, not an issue.

Except this is running on a Windows system so I wonder if that port is in use by Windows?

What is the error you see?
What is a “Node red Server”?

Yes this Python program is running in a windows system.

Node-RED is an open-source flow-based development tool that is commonly used for IoT (Internet of Things) applications. Which we want to read sensors measurement then using Python Socket library send messages to Node-Red server via TCP Server.

There is no error, but in the Node-Red Server (TCP Server) we are not able to receive messages from Python Socket library (TCP Client) from our windows system.

Where exactly is the server, and where exactly is the Python client running? Have you checked the firewalls, routers, Network gateways, software/ OS network tables, and port settings between the two?

If it’s not that, would find a different simple TCP server to test the Python code, and some other TCP client to test the config on the Node-Red server.

It certainly can be - I used to know it as the thing Windows did for networking, although it’s now more historical than factual.

This COULD be nothing more than a collision on port numbers and completely insignificant, but I’m worried that this IoT device might actually be talking NETBIOS (or equivalent) and the Python script will have to do the same.

You are not using TCP, you are sending using UDP.

I recommednd that you are explicit with the conversion of response.
In production code I would not rely on str() to convert the response object as it may not do what the protocol requires.

Suggest you change the Node-Red from port 139 so that it cannot conflict with Windows.

In a windows terminal run netstat -ano and check that there is a program LISTENING on your chosen port. Is it TCP or UDP? Check that the PID is for
the Node-Red server.

Add print statements to your python code to prove that you are sending the message.

Hello Everyone!

Thank you for all of your input!

It has been solved, we are sending messages while using Python to listen, whereby we should only listen on only 1 device, and also port number we changed to > 1024.

Ports below 1024 are privileged so you either need to run as root (not advised) or enable access to that port as a user, or choose a port > 1024. Source

Warmest regards,