Forum breadcrumbs - You are here:ForumDevelopment: TestersscriptTCP
You need to log in to create posts and topics.

scriptTCP

TCP communication is a very interesting feature for interprocess communication. It allows SimulIDE to communicate with another application. For example, to do data retrieval or visualization.
I work in a French school, and the computers are under Windows. And I currently use the serial port for communication to another application (usually python). This requires serial ports coupled together. For example, com11 <-> com12. Under Windows, for a few years, this has been rather difficult. There is only one free software capable of doing this: com0com. Because it requires signed drivers, approved by Microsoft.
Using TCP communication makes communication more universal. This would allow communicating from SimulIDE to any machine, whether Windows or Linux without installing special driver.

However, I tried to test the "scriptTCP" template provided in another post. But, it seems that for the moment, we can send data from SimulIDE, but not receive it.
I have a small Python server (made with the help of ChatGPT) and a simulation file with only a "TCP script". When I start the simulation, and if I don't try to send data, I correctly receive the messages sent by SimulIDE.
But, as soon as I try to send data from the Python server to SimulIDE, it crashes and closes.

I guess this is normal, still in development.
Or is there a special way to send data from Python to SimulIDE?

Uploaded files:

I don't know much about TCP or other web technologies (Simulide is using Qt libraries) but as far as I know this TCP module is working.
For example here Simulide is sending and receiving data to Webots (robot simulator).
https://www.youtube.com/watch?v=yNwQd49Mlvw
This is a simple Car with 1 Arduino, 2 motors and ultrasonic sensors.
And there is an script working as interface: it reads data from the motors and sends to Webots, which simulates the movement and returns distance data, then the script writes these distances to the sensors in the circuit.

What can be the problem in your case I have no idea, specially without watching the code.
But some basic questions:  what kind of python server did chatGPT? are you sure it is working?

I've uploaded the files. Here is the python server :

import socket

HOST = ''      # Écoute sur toutes les interfaces
PORT = 10020   # Le port sur lequel votre application envoie des données

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    print(f"Le serveur écoute sur le port {PORT}...")
    conn, addr = s.accept()
    with conn:
        print(f"Connecté par {addr}")
        while True:
            data = conn.recv(1024)
            if not data:
                print("Le client a fermé la connexion.")
                break
            message_recu = data.decode()
            print(f"Données reçues : {message_recu}")

            # Préparer la réponse
            reponse = f"Message reçu : {message_recu}\r\n"
            # Envoyer la réponse au client
            conn.sendall(reponse.encode())

The last line send data to SimulIDE thru the port it was connected.
If you comment the last line, the python programme display "message" sent by SimulIDE continuously.

The SimulIDE script is "scriptTCP.as" that come from the templates from another post :



bool m_connected;

string m_received;

void setup() // Executed when Component is created
{
    print("tcp  setup() OK"); 
}

void reset() // Executed at Simulation start
{
    print("tcp reset()");

    m_received = "";
    
    m_connected = false;

    Wb_link.connectToHost( 0, "127.0.0.1", 10020 ); // Connect to host at Socket nº 0
}

void updateStep()
{
    if( !m_connected ) return;

    Wb_link.sendMsgToHost("Message", 0 ); // Send message to host at Socket nº 0
    
    // Wb_link.closeSocket( 0 ); // Closing Socket nº 0
}

void received( string msg, int link ) // Received message from host at Socket nº link
{
    if( msg == "\r\n") return;
    
    m_received = msg;
}

void tcpConnected( int link ) // We got connected to Socket nº link
{
    print("tcp Connected to " + link );

    m_connected = true;
}

void tcpDisconnected( int link ) // We got disconnected from Socket nº link
{
    print("tcp Disconnected from " + link );
    
    m_connected = false;
}

The test is under Windows. SimulIDE (1.2.0) crash and close if data sent from python to SimulIDE.

I re-upload my test files below.

Uploaded files:

I've tried the simulation and python server under Linux (Lubuntu virtualbox). The problem is the same, SimulIDE crashes and closes when data is sent to it.
The question will be : In which way data must be sent to SimulIDE ?

In which way data must be sent to SimulIDE ?

Like in any socket, for example:  socket.send( data );
So your python code is doing ok.

Did some testing with your code and it works ok, the python side is receiving and sending response.
And the C++ side is receiving from python and sending to the script.
The script is crashing but I don't know why yet.

I have to do some debugging...

Found and solved the problem.
Note that repo for 1.2.0 is here: https://github.com/eeTools/SimulIDE-dev

 

Good ! Wait for the next truck version to test.