7 Replies - 3021 Views - Last Post: 26 August 2019 - 03:43 AM Rate Topic: -----

#1 rillzp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-September 15

Split up serialport.ReadLine();

Posted 22 September 2015 - 03:50 AM

Hello.

First of all, Sorry if the code looks is wierd and hard to understand, im really new to this and still have alot to learn :dontgetit:

I am having alot of trouble splitting up the values to diffrent textboxes.

The code im using in c# is
 this.Invoke(new EventHandler(displaydate_event));
            in_data = myport.ReadLine();  
        }
        private void displaydate_event(object sender, EventArgs e)
        {
            datetime = DateTime.Now;
            string time = + datetime.Hour + ":" + datetime.Minute + ":" + datetime.Second;
            data_tb.AppendText(time + "\t\t\t" + in_data + "\n");


This however gives me all the values im currently reading on my arduino (in_data) in to one textbox like the image below

Posted Image

I would like to split them up and retrive the temperature in one box the moist value in another and the light value in the last. How would i do this? Im really new to c# aswell as arduino so i really havn't managed to figure it out.


Full Arduino Code

Spoiler


Full C# code
Spoiler


Is This A Good Question/Topic? 0
  • +

Replies To: Split up serialport.ReadLine();

#2 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Split up serialport.ReadLine();

Posted 22 September 2015 - 03:56 AM

you could use the String.Split() method
https://msdn.microso...y/b873y76a.aspx

what is format of the data received from the microcontroller? I would use either raw binary or if in text XML or JSON

This post has been edited by horace: 22 September 2015 - 03:59 AM

Was This Post Helpful? 1
  • +
  • -

#3 rillzp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-September 15

Re: Split up serialport.ReadLine();

Posted 22 September 2015 - 04:26 AM

View Posthorace, on 22 September 2015 - 03:56 AM, said:

you could use the String.Split() method
https://msdn.microso...y/b873y76a.aspx

what is format of the data received from the microcontroller? I would use either raw binary or if in text XML or JSON


Thanks for a quick reply.

I tried yesterday with the string.split() method by putting a "/" in the arduino code (c# code aint there anymore as i didn't get it to work)

However i most likley did something wrong.


The microcontroller gives me both numbers and text at the moment but all the values (degreeC, moistValue, LightValue that comes out is only numbers, so the text is only C for celius and some blankspaces atm. But i can remove all those lines and display them from c# insted i guess. i really dont care if i can see that in the arduino log or not as im not planning to use it.

Code that prints from microcontroller to port.
  Serial.print(degreesC);
  Serial.print(" C                                          ");
  Serial.print("/");
  Serial.print(moistValue); //prints the values coming from the sensor on the screen
  Serial.print("                                            ");
  Serial.print("/");
  Serial.println(lightValue); //prints the values coming from the sensor on the screen
 
   delay(3000);



This give me pretty mutch exactly the same as it looks in the windows application.

Posted Image



Oh this dosn't look good, i have no clue what eighter raw binary, xml or json is :mellow:/>/>
Was This Post Helpful? 0
  • +
  • -

#4 rillzp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-September 15

Re: Split up serialport.ReadLine();

Posted 22 September 2015 - 04:43 AM

Not sure if it matter. But value of temperature is with 2 decimals.
Was This Post Helpful? 0
  • +
  • -

#5 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Split up serialport.ReadLine();

Posted 22 September 2015 - 04:45 AM

if the seperator is \ in C/C++ the character would be '\\', e.g.
            string words = "20 C                                  \\850";
            string[] split = words.Split(new Char[] { '\\'});
            foreach (string s in split)
                    Console.WriteLine(s);


when run gives
20 C
850


the data you receive from the serial port would only have a single \

raw binary data transmission is when you transmit the data as it is held in the memory of the machine - fast but you have to be careful of word lengths, little/big endian, number representation, etc

XML and JSON transmit the data in human readable form
http://www.json.org/

This post has been edited by horace: 22 September 2015 - 04:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 rillzp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-September 15

Re: Split up serialport.ReadLine();

Posted 22 September 2015 - 08:34 AM

View Posthorace, on 22 September 2015 - 04:45 AM, said:

if the seperator is \ in C/C++ the character would be '\\', e.g.
            string words = "20 C                                  \\850";
            string[] split = words.Split(new Char[] { '\\'});
            foreach (string s in split)
                    Console.WriteLine(s);


when run gives
20 C
850


the data you receive from the serial port would only have a single \

raw binary data transmission is when you transmit the data as it is held in the memory of the machine - fast but you have to be careful of word lengths, little/big endian, number representation, etc

XML and JSON transmit the data in human readable form
http://www.json.org/




Thanks alot for your help man, you cant imagine how many hours i have been trying to get this to work ! :bigsmile: ,

I cant do \ in arduino serialprint and // didn't work in c#, but i finally managed to get it to work with this code:

 private void displaydate_event(object sender, EventArgs e)
        {
            datetime = DateTime.Now;
            
            string time = + datetime.Hour + ":" + datetime.Minute + ":" + datetime.Second;
           
           
            string[] in_dataSplit = myport.ReadLine().Split('/');
           data_tb.AppendText(time + "\t\t\t" + in_dataSplit[0] + "\t\t\t" + in_dataSplit[1] + "\t\t\t" + in_dataSplit[2] + "\n"); // 
           textBox1.AppendText(in_dataSplit[0] + "\n");   //Display degreeC
           textBox2.AppendText(in_dataSplit[1] + "\n");   //Display moistValue
           textBox3.AppendText(in_dataSplit[2] + "\n");   //Display lightValue
 }



Once again, thanks alot
Was This Post Helpful? 0
  • +
  • -

#7 Yaswanth619   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-August 19

Re: Split up serialport.ReadLine();

Posted 25 August 2019 - 11:39 PM

sir can u send the code after output, i have same issue. The Data which is getting from hardware
has to split in different data grid and also display the real time data from the hardware.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: Split up serialport.ReadLine();

Posted 26 August 2019 - 03:43 AM

Given that this topic is almost four years old you should open your own topic for it.(BE wary of just asking for code though.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1