How to detect "0X0D" car return in a TCP client program?

Asked

Viewed 44 times

0

Hey, fellas, what’s up? First of all I’m sorry if by chance I make a mistake in the forum, because, it’s my first time here and I’m not a professional programmer.

Well, I downloaded this program on the Internet and made some adaptations and it worked incredibly perfectly. I use a programmed Arduino as a server, and it sends phrases with (0X0D) car return at the end of each sentence.
The problem is that when the Arduino sends more than one sentence at a time, it is printed on the same line as the txtTalk phrases, but the idea would be the program detect these car returns and jump to next line printing the phrases in separate lines.

Follow the code below and thank you very much for your help.

public partial class Form1 : Form
{
    /*TCPClient variable created here to be thrown around in different threads & funcs*/
    private TcpClient myClient;
    private NetworkStream stream;
    WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer();

    public Form1()
    {
        InitializeComponent();
    }
           

    private void btnConnect_Click(object sender, EventArgs e)
    {
        /*Parse IP & port info and connect to client, the stream has global scope so that other funcs can use
          Note the usage of BGW is just for realtime display of received messages*/
        myClient = new TcpClient(textBox1.Text,int.Parse(textBox2.Text));

        stream = myClient.GetStream();
        txtTalk.AppendText("CONECTADO: " + textBox1.Text + ":" + textBox2.Text + "\r\n");
        backgroundWorker1.RunWorkerAsync();
        btnConnect.BackColor = Color.Green;
        btnStop.BackColor = Color.White;            
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
        /*Disconnect from client, close stream first then TCP connection
         NOTE: this is the Disconnect button, not to be confused with server stop(below)*/
        txtTalk.AppendText("DESCONECTADO\r\n");
        stream.Close();
        myClient.Close();
        btnConnect.BackColor = Color.White;
        btnStop.BackColor = Color.Red;
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        /*Listen for incoming messages indefinitely until connection is broke*/
        while (myClient.Connected)
        {
            if (!stream.DataAvailable)
            {
                System.Threading.Thread.Sleep(250);
                continue;
            }

            Byte[] data = new Byte[256];

            String responseData = String.Empty;

            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
            backgroundWorker1.ReportProgress(0, responseData);
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        txtTalk.AppendText("" + e.UserState.ToString() + "\r\n");
        btnSilenciar.BackColor = Color.Red;
        player.URL = @"C:\\mp3 file\\myMP3.mp3";
        player.controls.play();

    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        txtTalk.AppendText("Background worker stopped\r\n");
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        txtTalk.Clear();
    }            
          
    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btnSilenciar_Click(object sender, EventArgs e)
    {
        btnSilenciar.BackColor = Color.White;
        player.controls.stop();
       
    }           
  }
}

1 answer

1


You can use the Split method to split the string with multiple lines and print one at a time.

It would be something like:

    foreach (var line in e.UserState.ToString().Split('\n'))
    {
        txtTalk.AppendText("" + line + "\r\n");
    }
  • Thanks a lot Woody Costa, I’ll try your tip...

  • Sorry for the delay in performing the test with your Woody Costa example, and this delay was because of the holiday.. I forgot to thank Natan Fernandes who helped me in formatting my question next to the forum. Thanks Natan... I return to give the Feed-back of the result.. See you later...

  • Woody Costa ran his code, just switched from n to r to work perfectly... But once thank you very much... foreach (var line in e.UserState.Tostring(). Split(' r'))

  • Ok, so you can mark the answer as solution.

  • Hello Woody, clicked on the option you accepted this Answer... To recognize as solution your answer.. I believe I have done correct... Thank you...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.