LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

TCP Socket c# Project to LV

Solved!
Go to solution

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

0 Kudos
Message 1 of 5
(1,297 Views)

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?

 

 

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 5
(1,264 Views)

Hello and thanks for replay

 

i want to create an equivalent LV code

 

best regards

Alex

 

0 Kudos
Message 3 of 5
(1,255 Views)
Solution
Accepted by topic author KTM_Lex

This is pretty much it.

TCP Write and Read.png

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.

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 5
(1,241 Views)

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

0 Kudos
Message 5 of 5
(1,229 Views)