78706

why is writeline not stopping at /r?

Question:

I am using C# Microsoft visual studio professional 2013. I am writing :

[0] 0 '\0' char [1] 1 '' char [2] 0 '\0' char [3] 0 '\0' char [4] 0 '\0' char [5] 8 '\b' char [6] 1 '' char [7] 3 '' char [8] 0 '\0' char [9] 0 '\0' char [10] 65535 '.' char [11] 65534 '' char [12] 0 '\0' char [13] 0 '\0' char [14] 13 '\r' char

but my end program is only seeing 0 1 0 0 0 8 1 3 0 it is not seeing the whole message. I was writing to a program called Modbus Master. That send my code a request for data and my code replies back. I am getting the full message from Modbus Master, but it is not getting the full massage back from my code.

here is my code :

void Listener_function() { IPHostEntry host; string localIP = "?"; host = Dns.GetHostEntry(Dns.GetHostName()); TcpListener listener = null; try { listener = new TcpListener(IPAddress.Parse(Moxa_IPtxt.Text), Convert.ToInt16(modemPorttxt.Text)); listener.Start(); // ErrorBox.Text = " EchoServer started ... /n"; MessageBox.Show(" waiting for incoming client connections.../n /r"); TcpClient 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) + 8; char[] Message3 = new char[Message_Size]; TCPProcessing(temp, Message3); if (writeData == true) { writer.Write(Message3); writer.Flush(); } /* reader.Close(); writer.Close(); client.Close(); */ } } catch (Exception e) { MessageBox.Show(e.ToString()); //ErrorBox.Text = e.ToString(); } finally { if (listener != null) { listener.Stop(); } } }

message 3 is a global array of size 30.

private void Form1_Load(object sender, EventArgs e) { tag = new List<Tag>(); Tagcall = new List<tagcall>(); Message3 = new char[30]; }

why is writeline not stopping at /r?

Answer1:

I see multiple problems with your code:

<ul><li>

first you tell us you are writing 15 byte (first snippet), but you are only trying to read 12 at a time(char[] temp = new char[12];)

</li> <li>

int s1 = reader.Read(temp, 0, 12); while you are saving the number of read bytes you are never checking if you have read enough data to start processing it

</li> </ul>

And lastly <a href="https://msdn.microsoft.com/en-us/library/ebb1kw70(v=vs.110).aspx" rel="nofollow">Streamwriter.WriteLine</a> writes until the <a href="https://msdn.microsoft.com/en-us/library/system.io.textwriter.corenewline(v=vs.110).aspx" rel="nofollow">TextWriter.CoreNewLine</a> character is encountered. if that's \r for your system it will correctly stop. If you want to write all or your char[] you should use <a href="https://msdn.microsoft.com/en-us/library/275y93ck(v=vs.110).aspx" rel="nofollow">StreamWriter.Write(char[])</a>

Recommend

  • How to execute command in a C# Windows console app?
  • Pyuno indexing issue that I would like an explanation for
  • Sending array as parameters using POST method in senchatouch2
  • How can I unpack a returned generic list of a custom type on the Compact Framework client?
  • Angular - Make multiple HTTP calls sequentially
  • Remove leading zeros from IP Address with C#
  • Getting the IP address of the android device when connected to 3G mobile network
  • Postman only posts to mongoose nested level 1
  • Remove leading zeros from IP Address with C#
  • Cannot access nodejs app on browser at localhost:4200 (docker run -p 4200:4200 …)
  • Test if two variable are empty at the same time
  • How can I stop iperf server when it finishes?
  • phpseclib $X509->setDomain on CSR?
  • Testing Broadcasting and receiving messages
  • Jackson JsonNode Serialization
  • Send an UDP multicast packet on all my IPs
  • Why no “what's my ip” well-known port/service?
  • joining two conditions in amazon s3 bucket policy
  • Simple subtraction and cast question
  • secure way of passing form variable
  • Delphi XE5 Rest Datasnap Server. Getting Client IP Address
  • Update web.config file in asp.net
  • adding item to window Extjs 4
  • Basler USB Camera byte buffer to image conversion
  • Checking whether the server is on/off
  • How can I replace the server in Web Component Tester
  • Clear activity stack before launching another activity
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • Record samples being played with OpenAL
  • Debugging ASP.NET on a built-in web server suddenly stops
  • Why is the size of this struct 32?
  • Controls, properties, events and timers running in design time
  • Sending data from AppleScript to FileMaker records
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • python regex in pyparsing
  • Android Google Maps API OnLocationChanged only called once
  • How can I use threading to 'tick' a timer to be accessed by other threads?