split serial communication

Hi. I have a project with a arduino duemilanove. Here I got 2 sensors (Thermistors) and one led. Also I have a Visual Basic program I made for this, and I wish to be able to have a 2-way communication between PC and Arduino. I get the sensor values on the PC, but when I'm gonna take values from PC to arduino, I need to split the values from PC. Example: I have a TrackBar giving a value between 0 and 255. The name is led1. PC writes [u]"SerialPort1.Write = ("led1=" & TrackBar1.Value & ",")"[/u] and much more to arduino. In arduino I need to "pick" the value from led1 and treat the code. Therefor I need to split [u]Serial.read();[/u]. So I split the whole code I get from serial read which can be: "led1=243, led2=23, led3=124," after ",".

Here is my problem. I cant get anny split functions to work!

Annyone please help ME!!!

Best, Andy

You could use scanf(), but why are you sending all that human-readable stuff when all the Arduino needs is a few numbers?

For example if you send 243,23,124 as binary numbers there's no need to parse anything. (caveat, needs a little more to be reliable).


Rob

Once you have captured what you have sent the arduino into a string, you can parce the string for the data using the string functions from below.

http://www.arduino.cc/en/Tutorial/TextString

Hi Graynomad. The values I send, I need to threat. So if I just send values like "243,23,124 ", the program don't know what thees values are for... Therefor I need to split the value "led1=243, led2=23, led3=124,..." from ",". Then I store values in arrays[] so I could get values like:

"led1=243" "led2=23" "led3=124"

Then I have my values...

So if I just send values like "243,23,124 ", the program don't know what thees values are for...

Yes it does, I'm not suggesting sending a string, send 3 bytes, the first # is for LED1, the second is for LED2, and the third for LED3.

If you want to send a string send "243,023,124", replace all the "," with \0, then do three atoi()s. Or use one of the string parsing funcs we've referenced above.

As far as I can see adding all the "ledx=," stuff just makes it harder.

The only reason I can see to do something like that is if there are a lot of LEDs, say 100 but only a couple change at a time, then it might make sense to qualify the values. But even then you can just blat 100 bytes every time (plus a frame delimiter to allow detection of the start of the bytes and maybe a checksum if it's important data)


Rob

Hi Graynomad. Currently now, I'm trying to build a home automation system. I've been working with PLC systems for many year, and now I'm trying to make my own system.

The deal is I want a main "arduino" that works as a base station. Connected to the arduino, there is (input) Temperature sensors, movement sensors and door sensors. (output) lights, Panel Heater... And these Nodes will be talking to arduino wierlessly. So I'll get a hole bunch of data in serialport. And when I get the data I'll prosess them.

Best, Andy

From the initial post

I get the sensor values on the PC, but when I'm gonna take values from PC to arduino, I need to split the values from PC.

I read this as you are sending the values we're talking about to an Arduino, hense my comments.

And these Nodes will be talking to arduino wierlessly.

That's a different ball of wax.

So I'll get a hole bunch of data in serialport. And when I get the data I'll prosess them.

How is all this data appearing on the serial port? Xbee or some such? How are you going to co-ordinate the data coming from the sensors? Why would this data come in from multiple devices in a nice orderly 1,2,3 format?


Rob

Simple test code that captures strings and splits the string at the ",".

// zoomkat 10-29-10 simple delimited ',' string parce 
// from serial port input (via serial monitor)
// and print result out serial port
// CR/LF could also be a delimiter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial-delimit-21"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
  while (Serial.available()) {
    delay(1);  //small delay to ensure buffer as a character

    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      break;
    }  //breaks out of capture loop to print readstring
    readString += c; 
  } //makes the string readString  

  if (readString.length() >0) {
    Serial.println(readString); //prints string to serial port out

    readString=""; //clears variable for new input
  }
}

Hi again Graynomad. :P For now I only have my arduino. Connected to Arduino, there is now 2 sensors(input), and one red diode(output). I made a visual basic winForm program, talking to Arduino with serialport. There I have 2 TextBox'es showing temperature value(sensor1: 22, sensor2: 4,), RichTextBox showing serial data, and one TrackBar that gives value 0-255 for the sharpness of red diode.(led1: 255,) Later in my project I'll have more nodes. Therefor it's best to split the serial string, like html Request.QueryString. So I could just grab what I want...

I hope this was more explanatory.

Best, Andy :D

Hi zoomkat. Thanks for your tip. I'll try to work this out. I think I need a array or something between this:

    if (c == ',') {
      break;
    }

Or?

Best, Andy

Thanks for your tip. I'll try to work this out. I think I need a array or something between this:

If you want to do something with the data, you would probably do it in the code area where the data is sent back to the serial monitor. You could convert the ascii string to a number, then use the number to set the pwm rate to a pin and such.

I hope this was more explanatory.

Maybe for this test, but for the final project

How is all this data appearing on the serial port? Xbee or some such? How are you going to co-ordinate the data coming from the sensors? Why would this data come in from multiple devices in a nice orderly 1,2,3 format?


Rob