我正在尝试通过tcp从Labview将平坦的DBl数组发送到C#。 我在C#中收到扁平化字符串。 我不知道如何将它们转换回DBL数组?
这是我在C#中的代码:
{
private TcpClient socketConnection;
private Thread clientReceiveThread;
void Start()
{
ConnectToTcpServer();
}
void Update()
{
}
private void ConnectToTcpServer()
{
try
{
clientReceiveThread = new Thread(new ThreadStart(ListenForData));
clientReceiveThread.IsBackground = true;
clientReceiveThread.Start();
}
catch (Exception e)
{
Debug.Log("On client connect exception " + e);
}
}
private void ListenForData()
{
try
{
socketConnection = new TcpClient("localhost", 5333);
Byte[] bytes = new Byte[4000];
while (true)
{
using (NetworkStream stream = socketConnection.GetStream())
{
int length;
while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
{
Debug.Log(bytes.Length);
var incommingData = new byte[length];
Array.Copy(bytes, 0, incommingData, 0, length);
// Convert byte array to string message.
string serverMessage = Encoding.ASCII.GetString(incommingData);
Debug.Log("server message received as: " + serverMessage);
Debug.Log(serverMessage.Length);
}
}
}
}
catch (SocketException socketException)
{
Debug.Log("Socket exception: " + socketException);
}
}
}
答案 0 :(得分:1)
由于将FALSE连线到“前缀数组或字符串大小”,因此缓冲区中唯一的数据就是平坦的double本身。但是您需要显式设置“ little endian”才能与C#对话。
如果您在G代码中进行了更改,那么这应该起作用:
double[] dblArray = (double[])Array.CreateInstance(typeof(System.Double), length / 8);
// I'm not sure if you need the cast or not.
您的代码中的常数为“ 4000”。我将其更改为一个声明的常量,并在其旁边添加一个注释,以确保“这是8的倍数(双倍数据类型的字节数)”。