gpt4 book ai didi

c# - 无法弄清楚如何让Socket接收数据C#

转载 作者:太空宇宙 更新时间:2023-11-04 04:04:20 27 4
gpt4 key购买 nike

所以我特别想做的是获得一个串行代理,将 arduino 通过管道传输到 Unity3d 中,以便在 Linux 上运行。 System.IO.Ports 无法在统一的 Linux 上运行。

所以我得到了一个基于 python 的串行代理脚本,我已经启动并运行得很好。我可以通过 netcat 进入该本地主机并实际从 arduino 获取输出。

nc localhost 8082
g'day from bitty 1.0 -- type 'logout' to disconnect
Connecting to /dev/tty.usbmodem5d11... connected.
HELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLO

我的arduino只在重复时打印出HELLO。

但是串行代理已启动并正在运行,并且有数据通过它。

现在我尝试在 Unity 中编写代码来接收这些数据,但我无法让它工作。

这是我的代码:

public Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
public byte[] buffer = new byte[256];

int pinRead = 0;

void Start ()
{
socket.Connect(IPAddress.Parse("127.0.0.1"),8082);

}

void Update ()
{
if (socket.IsBound)
{
try
{
int bytesRead;
bytesRead = socket.Receive(buffer);
string incommingdata = System.Text.Encoding.ASCII.GetString(buffer);
Debug.Log(bytesRead+" || "+incommingdata);

}
catch (System.Exception e)
{


}
}

bytesRead 始终为 0 并且传入数据没有任何内容。连接成功,isBound 返回 true。我只是无法获得正确的代码来接收数据并将其设置为可用的格式。

请帮忙。我需要让它发挥作用,但这远远超出了我的专业领域。我怎样才能做到这一点?

最佳答案

所以我使用 tcpclient 对象和一些发布在随机统一博客上的代码片段来实现它......

如果有人想看的话,就在这里。不过,我仍然很好奇如何让 Socket 实现正常运行。

using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System;
using System.Text;
using System.IO;
using System.Threading;

public class ArduinoTest : MonoBehaviour
{
private bool socketReady = false;
private TcpClient mySocket;
private NetworkStream theStream;
private StreamWriter theWriter;
private StreamReader theReader;
private string Host = "localhost";
private Int32 Port = 9900;
private int pinRead = 0;


void Start ()
{
setupSocket();
}

void Update ()
{
string read = readSocket();
if (read != "")
{
int value = int.Parse(read);
if ( value > 53)
{
Debug.Log((value-54)+" DOWN");
}
else
{
Debug.Log(value+" UP");
}
}
}

void OnApplicationQuit()
{
writeSocket("logout");
closeSocket();
}

public void setupSocket()
{
try
{
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
socketReady = true;
}
catch (Exception e)
{
Debug.Log("Socket error:" + e);
}
}

public void writeSocket(string theLine)
{
if (!socketReady)
return;
String tmpString = theLine + "\r\n";
theWriter.Write(tmpString);
theWriter.Flush();
}

public string readSocket()
{
if (!socketReady)
return "";
if (theStream.DataAvailable)
return theReader.Read().ToString();
return "";
}

public void closeSocket()
{
if (!socketReady)
return;
theWriter.Close();
theReader.Close();
mySocket.Close();
socketReady = false;
}

public void maintainConnection()
{
if(!theStream.CanRead)
{
setupSocket();
}
}

}

关于c# - 无法弄清楚如何让Socket接收数据C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21870714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com