简体   繁体   中英

C# TCP/IP message is being changed

I am writing my program in Microsoft Visual Studio 2013 professional C#. I used the debugger and I looked at my array called Message3 and it has the right information, but when it gets to the connected computer modbus program, the message is different. So I used Wireshark and wireshark agrees with the Modbus program. It is weird because it only does it for some values like the value 7600 in modbus it is sent out in two registers as 29 and 176 and my program has that in the array but wireshark is seeing 29 194 176. it somehow added a 194 that was not in my array.

        [0] 0 '\0'  char
    [1] 4 ''   char
    [2] 0 '\0'  char
    [3] 0 '\0'  char
    [4] 0 '\0'  char
    [5] 5 ''   char
    [6] 1 ''   char
    [7] 3 ''   char
    [8] 2 ''   char
    [9] 29 ''  char
    [10]    176 '°' char

that is what my debugger has and this is what wire shark see: 0 4 0 0 0 6 1 3 0 5 0 1 03 02 1d c2 b0

I have tried both: writer.Flush and writer.FlushAsync but nothing helps. again it is only some values it does this too. here is my code :

 void Listener_function()
    {
        IPHostEntry host;
        string localIP = "?";
        host = Dns.GetHostEntry(Dns.GetHostName());

        listener = null;
        try
        {
            listener = new TcpListener(IPAddress.Parse(Moxa_IPtxt.Text), Convert.ToInt16(modemPorttxt.Text));
            listener.Start();
            // ErrorBox.Text = " EchoServer started ... /n";
            connect_flag = true;
            MessageBox.Show(" waiting for incoming client connections.../n /r");
            client = listener.AcceptTcpClient();
            MessageBox.Show(" Accepted new client coneection ...");

            StreamReader reader = new StreamReader(client.GetStream());
            StreamWriter writer = new StreamWriter(client.GetStream());

            while (true)
            {

                string s = string.Empty;
                char[] temp = new char[12];
                int s1 = reader.Read(temp, 0, 12);
                int Registers_number = (((int)temp[10] << 8) + (int)temp[11]);
                int Message_Size = (Registers_number * 2) + 9;
                char[] Message3 = new char[Message_Size];
                TCPProcessing(temp, Message3);
                if (writeData == true)
                {
                    writer.Write(Message3);
                   // writer.Flush();
                    writer.FlushAsync();
                }

            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
            //button1.Text = "Connect";

            //ErrorBox.Text = e.ToString();
        }
        finally
        {
            if (listener != null)
            {
                listener.Stop();
            }

        }
    }

any ideas ?

again I had a breakpoint at the } after FlushAsync and I copy what message has and it does not have a 194 ( Please see above)

Brandon, you don't seem to read carefully what I wrote .

don't use StreamReader/StreamWriter which are for text operations. Use directly client.GetStream

Here is a code to show you

var m = new MemoryStream();
var wr = new StreamWriter(m);
wr.Write(new char[] { (char)29, (char)176 }, 0, 2);
wr.Flush();
var bytes2 = m.ToArray();

content of bytes2 is: 29,194,176 as in your case. So I repeat, use client.GetStream , not StreamReader/StreamWriter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM