02-16-2023 06:42 AM
Hello i have a little Problem.
I want to upgrad an old program with an 3rd party TCP Client to LV
The Server accept the following protocoll
length of message [2 byte]
Message [n byte]
EOT [1byte]
the 3rd Party sends the data with the following code
private static void SendBytes(string data, Stream nwstream) //adjusts bytes to protocoll
{
byte[] ba = Encoding.UTF8.GetBytes(data);
byte[] bstart = BitConverter.GetBytes(Convert.ToInt16(ba.Length));
byte eot = 0x04;
//Adjust Bytes to Protocoll
for (int i = 0; i < bstart.Length; i++) //message length
{
nwstream.WriteByte(bstart[i]);
}
for (int i = 0; i < ba.Length; i++) //message length
{
nwstream.WriteByte(ba[i]);
}
nwstream.WriteByte(eot); //Message Endoftransmission
}
private static string sendtoServer(string xmldata)
{
TcpClient tcpclnt = new TcpClient();
try
{
tcpclnt.Connect(ipadress, port);
}
catch
{
System.IO.File.WriteAllText("/home/pi/files/init/error.txt","NoConnection");
System.Environment.Exit(1);
}
Stream nwstream = tcpclnt.GetStream();
SendBytes(xmldata, nwstream);
StreamReader nwreader = new StreamReader(nwstream, Encoding.UTF8);
String response;
try
{
response = nwreader.ReadToEnd();
}
finally
{
nwreader.Close();
}
tcpclnt.Close();
return response;
}
Maby someone can help me
best regards
Alex
Solved! Go to Solution.
02-16-2023 09:45 AM
Do you want to create equivalent LabVIEW code for your C# code or is it about creating a TCP/IP Server that can handle this C# client?
02-16-2023 10:01 AM
Hello and thanks for replay
i want to create an equivalent LV code
best regards
Alex
02-16-2023 12:08 PM - edited 02-16-2023 12:09 PM
This is pretty much it.
But there might be something with the TCP Read. LabVIEW doesn't really know the equivalent of Stream.ReadToEnd, since there is no clearly defined end. Standard mode might work, and if your response can get bigger than 1000 bytes you would have to increase the number of bytes to read.
02-16-2023 12:38 PM
Thanks for the very fast replay, its perfect
i tryed it with many other moduls but i didn't get the right way to send the message length and eot
thanks so many times