Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: jhen on Mar 14, 2013, 09:44 am

Title: how to interpret the serial data
Post by: jhen on Mar 14, 2013, 09:44 am
can u please give me a sample that are receiving a serial data,that f the mcu receive a charater 'a' the corresponding data will store in a buffer and when the character 'b' is detected d corresponding character will store to other buffer..can u please give me a related sample
Title: Re: how to interpret the serial data
Post by: AWOL on Mar 14, 2013, 11:35 am
Code: [Select]
if (Serial.available ()) {
  int inChar = Serial.read ();
  switch (inChar) {
    case 'a': // do something
    break;
    case 'b': // do something else
    break;
   default: // do something completely else
  }
}
Title: Re: how to interpret the serial data
Post by: AWOL on Mar 14, 2013, 01:56 pm
The code I gave you is just an incomplete example - you need to fill in the blanks, or you could rewrite as, say, a simple state machine.
You original question wasn't specific.
Title: Re: how to interpret the serial data
Post by: jhen on Mar 14, 2013, 02:02 pm
ah...i know that it is incomplete i just want to know if it is applicable to what i want to do?tnx
Title: Re: how to interpret the serial data
Post by: PaulS on Mar 14, 2013, 02:32 pm
Quote
the char 0999999999 will be save to a buffer

Which ONE key did you press to type the character 0999999999?
Title: Re: how to interpret the serial data
Post by: jhen on Mar 14, 2013, 02:46 pm
one key??i want to store it in a buffer...this character will be send by the computer to the arduino,if ahm..for example the original data is a099999bTYYYYU, IF character 'a' is detected the 099999 will be store to a buffer and if character 'b' is detected the TYYYYU will be store to another buffer....please help me to do this...
Title: Re: how to interpret the serial data
Post by: PaulS on Mar 14, 2013, 02:50 pm
Proper use of capital letters and punctuation would really be appreciated here. You sound like a little boy whining.

You keep saying you want to store A character, and then you say that the character is 0999999999.

Well, I have news for you. 0999999999 is not a character. It is a string.

If you want to ask about how to read characters, and store them in an array AS A string, then say that. Don't keep referring to 0999999999 as a character.
Title: Re: how to interpret the serial data
Post by: jhen on Mar 14, 2013, 02:59 pm
i'm so sorry for that....im unfamiliar with this,,,
ahm..is it possible to use the switch case function??plz help me
Title: Re: how to interpret the serial data
Post by: AWOL on Mar 14, 2013, 03:01 pm
I've shown you how to wait for and read one character into one particular buffer called "inChar", and make a decision based on the value of that character.
It isn't a great leap of imagination to read in further characters and read them into other buffers.
Title: Re: how to interpret the serial data
Post by: jhen on Mar 14, 2013, 03:09 pm
how can i do this?any advice?
Title: Re: how to interpret the serial data
Post by: AWOL on Mar 14, 2013, 03:32 pm
Some application, a little effort, and maybe work through some of the examples supplied in the IDE.
Title: Re: how to interpret the serial data
Post by: jhen on Mar 14, 2013, 03:36 pm
plz...give me a specfic examples
Title: Re: how to interpret the serial data
Post by: AWOL on Mar 14, 2013, 03:37 pm
It's been a while since I looked at the IDE supplied examples, but I'm pretty sure there's (at least) one on serial comms, and another on arrays.
I'd start with those.
Title: Re: how to interpret the serial data
Post by: jhen on Mar 14, 2013, 03:52 pm
please give me the very specific examples in IDE pls???
Title: Re: how to interpret the serial data
Post by: AWOL on Mar 14, 2013, 04:03 pm
I'm posting from my tablet, so I'm afraid I can't.
What did you do to help yourself in the fifteen minutes between my last post and yours?
Title: Re: how to interpret the serial data
Post by: AWOL on Mar 14, 2013, 04:41 pm
The problem looks to be that you're never printing the value of "inData" when it has some dta a in it.
Move the final "print" inside the "started && ended" condition, but before you reset the string.
Title: Re: how to interpret the serial data
Post by: AWOL on Mar 14, 2013, 05:35 pm
The problem?
You didn't open the serial monitor, you didn't select the correct line speed, you didn't compile the code, you didn't upload the code, you didn't hit "send"...
It works, perfectly.
Title: Re: how to interpret the serial data
Post by: jhen on Mar 14, 2013, 05:42 pm
im sory....it works it print but the problem is it auto scroll...
Title: Re: how to interpret the serial data
Post by: AWOL on Mar 14, 2013, 05:45 pm
You'll have to explain what that means, and why you perceive it to be a problem.
Title: Re: how to interpret the serial data
Post by: jhen on Mar 14, 2013, 05:49 pm
THANK YOU very much...is it functional of what i want to do as what i said in my first post?
Title: Re: how to interpret the serial data
Post by: jhen on Mar 14, 2013, 05:57 pm
is it functional if the computer will send two type of  data for example <jvvmv>{fff},is it posible to use two start and end maker...
Title: Re: how to interpret the serial data
Post by: PaulS on Mar 14, 2013, 06:02 pm
Quote
is it posible to use two start and end maker...

Yes, but...

<jvvmv,fff> is easier to deal with. Save all the data, then use strtok() to parse into jvvmv and fff.

With two start and end markers, you need two arrays, and you need to keep track of which array to store a character in. Does f go in the first buffer or the second buffer?
Title: Re: how to interpret the serial data
Post by: AWOL on Mar 14, 2013, 06:51 pm
Quote
what i need to do?
you need to start thinking for longer than 20 minutes at a time, and working through some of the examples in the IDE.
I believe I've said this before, but it's worth repeating.

(Hint: you haven't written anything called "sendCommand" yet.)
Title: Re: how to interpret the serial data
Post by: jhen on Mar 15, 2013, 12:51 am
i got it...thank you very much
Title: Re: how to interpret the serial data
Post by: mixania on Mar 18, 2013, 03:24 pm
This example may help you grasp the basics.

Code for Arduino:

Code: [Select]
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A0));
  delay(2);
}


Code for Processing:
Code: [Select]
import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph

void setup () {
// set the window size:
size(400, 300);       

// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my  Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);

// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);

// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}