如何将扁平字符串转换回 DOUBLE 数组?
How to convert back a flattened string to DBL array?
我正在尝试通过 tcp 从 labview 向 c# 发送扁平化的 DBl 数组。
我在 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 incomingData = new byte[length];
Array.Copy(bytes, 0, incomingData, 0, length);
// Convert byte array to string message.
string serverMessage = Encoding.ASCII.GetString(incomingData);
Debug.Log("server message received as: " + serverMessage);
Debug.Log(serverMessage.Length);
}
}
}
}
catch (SocketException socketException)
{
Debug.Log("Socket exception: " + socketException);
}
}
}
这是我的 labview 代码:
这是我的 labview 输出:(浮点数组和扁平字符串)
这是 C# 中的输出:
由于您将 FALSE 连线到 "prepend array or string size",缓冲区中的唯一数据就是展平的双打本身。但是您需要显式设置 "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”。我会将其更改为已声明的常量,并在其旁边添加一条注释 "Make sure this is a multiple of 8 (number of bytes in double data type)."
我正在尝试通过 tcp 从 labview 向 c# 发送扁平化的 DBl 数组。 我在 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 incomingData = new byte[length];
Array.Copy(bytes, 0, incomingData, 0, length);
// Convert byte array to string message.
string serverMessage = Encoding.ASCII.GetString(incomingData);
Debug.Log("server message received as: " + serverMessage);
Debug.Log(serverMessage.Length);
}
}
}
}
catch (SocketException socketException)
{
Debug.Log("Socket exception: " + socketException);
}
}
}
这是我的 labview 代码:
这是我的 labview 输出:(浮点数组和扁平字符串)
这是 C# 中的输出:
由于您将 FALSE 连线到 "prepend array or string size",缓冲区中的唯一数据就是展平的双打本身。但是您需要显式设置 "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”。我会将其更改为已声明的常量,并在其旁边添加一条注释 "Make sure this is a multiple of 8 (number of bytes in double data type)."