0

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
  • 187
  • 1
  • 3
  • 11
  • Brandon why don't you read comments and answers of [your previous question](https://stackoverflow.com/questions/46407955/why-is-writeline-not-stopping-at-r) (for example: `you are never checking if you have read enough data to start processing it`) – L.B Sep 28 '17 at 16:36
  • Brandon, if you want to read/write binary data, don't use StreamReader/StreamWriter which are for text operations. Use directly `client.GetStream` – L.B Sep 28 '17 at 16:39
  • I am getting the data in correctly, it is the writing back I am having problems with . – Brandon Sep 28 '17 at 16:42
  • Have you ever heard of Big Endian and Little Endian. You simple have byte swapping when you are send numbers. – jdweng Sep 28 '17 at 16:46

1 Answers1

1

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

L.B
  • 114,136
  • 19
  • 178
  • 224
  • ok so you are getting the same problem.. but how do I use Client.Getstream to write back the information ? can you show a code example ? – Brandon Sep 28 '17 at 18:13
  • @Brandon `client.GetStream().Write` or `client.GetStream().Read` – L.B Sep 28 '17 at 18:26
  • @Brandon other line ? do you mean `you are never checking if you have read enough data to start processing it` ? – L.B Sep 28 '17 at 18:46
  • I do, modbus coming in a formatted, if the message is not in that right formatted, I stop processing it – Brandon Sep 29 '17 at 12:03