Wednesday, November 6, 2024
spot_img

python – Raspberry Pi Reside knowledge to Unity by Ethernet connection


I’ve a gyroscope storing knowledge on a Raspberry Pi 4 (The Consumer) and I must ship this knowledge to Unity C# (The Server) so I can use a gyroscope to regulate place knowledge.

I’ve two separate units I’m connecting with an Ethernet Cable. I’d moderately web not be concerned within the course of (though would stay time speeds differ that a lot?)

The Pi will Run the Python script to ship knowledge over the cable that may replace a field’s place within the Unity recreation.

The Code I’m utilizing is operating into some Errors that I’ve seen earlier than however it’s nonetheless very unclear to me. The Python code precise works fantastic when the 2 scripts are run on one single gadget however not a lot once I’m attempting to attach two units. I’ve learn earlier than the IP’s get combined up however I and nonetheless confused on tips on how to edit this code for the repair.

I am operating into
Errno 10061 : No connection may very well be made as a result of the goal machine actively refused it
I’ve even encountered Errno 111 earlier than as properly, however not a lot just lately.

//C#
utilizing System.Web;
utilizing System.Web.Sockets;
utilizing System.Textual content;
utilizing UnityEngine;
utilizing System.Threading;

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

    bool operating;

    non-public void Replace()
    {
        rework.place = receivedPos; //assigning receivedPos in SendAndReceiveData()
    }

    non-public void Begin()
    {
        ThreadStart ts = new ThreadStart(GetInfo);
        mThread = new Thread(ts);
        mThread.Begin();
    }

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

        shopper = listener.AcceptTcpClient();

        operating = true;
        whereas (operating)
        {
            SendAndReceiveData();
        }
        listener.Cease();
    }

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

        //---receiving Information from the Host----
        int bytesRead = nwStream.Learn(buffer, 0, shopper.ReceiveBufferSize); //Getting knowledge in Bytes from Python
        string dataReceived = Encoding.UTF8.GetString(buffer, 0, bytesRead); //Changing byte knowledge to string

        if (dataReceived != null)
        {
            //---Utilizing acquired data---
            receivedPos = StringToVector3(dataReceived); //

Py

import socket
import time

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

startPos = [0, 0, 0] #Vector3   x = 0, y = 0, z = 0
whereas True:
    time.sleep(0.5) #sleep 0.5 sec
    startPos[0] +=1 #enhance x by one
    posString = ','.be a part of(map(str, startPos)) #Changing Vector3 to a string, instance "0,0,0"
    print(posString)

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

Thanks!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles