0
\$\begingroup\$

I have a gyroscope storing data on a Raspberry Pi 4 (The Client) and I need to send this data to Unity C# (The Server) so I can use a gyroscope to control position data.

I have two separate devices I am connecting with an Ethernet Cable. I would rather internet not be involved in the process (although would live time speeds differ that much?)

The Pi will Run the Python script to send data over the cable that will update a box's position in the Unity game.

The Code I am using is running into some Errors that I've seen before but it is still very unclear to me. The Python code actual works fine when the two scripts are run on one single device but not so much when I'm trying to connect two devices. I've read before the IP's get mixed up but I and still confused on how to edit this code for the fix.

I'm running into Errno 10061 : No connection could be made because the target machine actively refused it I have even encountered Errno 111 before as well, but not so much recently.

//C#
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
using System.Threading;

public class CSharpForGIT : MonoBehaviour
{
    Thread mThread;
    public string connectionIP = "127.0.0.1";
    public int connectionPort = 25001;
    IPAddress localAdd;
    TcpListener listener;
    TcpClient client;
    Vector3 receivedPos = Vector3.zero;

    bool running;

    private void Update()
    {
        transform.position = receivedPos; //assigning receivedPos in SendAndReceiveData()
    }

    private void Start()
    {
        ThreadStart ts = new ThreadStart(GetInfo);
        mThread = new Thread(ts);
        mThread.Start();
    }

    void GetInfo()
    {
        localAdd = IPAddress.Parse(connectionIP);
        listener = new TcpListener(localAdd, connectionPort);
        listener.Start();

        client = listener.AcceptTcpClient();

        running = true;
        while (running)
        {
            SendAndReceiveData();
        }
        listener.Stop();
    }

    void SendAndReceiveData()
    {
        NetworkStream nwStream = client.GetStream();
        byte[] buffer = new byte[client.ReceiveBufferSize];

        //---receiving Data from the Host----
        int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize); //Getting data in Bytes from Python
        string dataReceived = Encoding.UTF8.GetString(buffer, 0, bytesRead); //Converting byte data to string

        if (dataReceived != null)
        {
            //---Using received data---
            receivedPos = StringToVector3(dataReceived); //<-- assigning receivedPos value from Python
            print("received pos data, and moved the Cube!");

            //---Sending Data to Host----
            byte[] myWriteBuffer = Encoding.ASCII.GetBytes("Hey I got your message Python! Do You see this massage?"); //Converting string to byte data
            nwStream.Write(myWriteBuffer, 0, myWriteBuffer.Length); //Sending the data in Bytes to Python
        }
    }

    public static Vector3 StringToVector3(string sVector)
    {
        // Remove the parentheses
        if (sVector.StartsWith("(") && sVector.EndsWith(")"))
        {
            sVector = sVector.Substring(1, sVector.Length - 2);
        }

        // split the items
        string[] sArray = sVector.Split(',');

        // store as a Vector3
        Vector3 result = new Vector3(
            float.Parse(sArray[0]),
            float.Parse(sArray[1]),
            float.Parse(sArray[2]));

        return result;
    }
    
}

Py

import socket
import time

host, port = "127.0.0.1", 25001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))

startPos = [0, 0, 0] #Vector3   x = 0, y = 0, z = 0
while True:
    time.sleep(0.5) #sleep 0.5 sec
    startPos[0] +=1 #increase x by one
    posString = ','.join(map(str, startPos)) #Converting Vector3 to a string, example "0,0,0"
    print(posString)

    sock.sendall(posString.encode("UTF-8")) #Converting string to Byte, and sending it to C#
    receivedData = sock.recv(1024).decode("UTF-8") #receiveing data in Byte fron C#, and converting it to String
    print(receivedData)

Thanks!

\$\endgroup\$
3
  • \$\begingroup\$ If Unity is the server, why has the unity code comments about sending data to the host? \$\endgroup\$ Commented Sep 7, 2024 at 11:01
  • \$\begingroup\$ Assuming the 127.0.0.1 is a placeholder for the actual remote address: Actively refused means the port was not open. Most likely the firewall software on the computer running Unity is actively blocking the connection. \$\endgroup\$ Commented Sep 14, 2024 at 6:31
  • \$\begingroup\$ Asking 127.0.0.1 to listen will not hear anything outside of the local computer. \$\endgroup\$ Commented Sep 14, 2024 at 6:35

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.