Pages: 1 [2]   Go Down
Author Topic: Serial.read() help  (Read 13858 times)
0 Members and 1 Guest are viewing this topic.
Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 998
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yipes!  What am I thinking..?!  Sorry: fixed.  smiley-grin

Thanks, j.

M
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am also trying to connect two pc Arduino because I do not have outputs for connecting to the GLCD.

I need to send value from my nunchuck and temp sensor to another Arduino.
I've used the code here on the forum but what I can not handle is negative numbers.

I have negative numbers that I have to use in my other Arduino.
What I need is something like:
x = 50 or x = -50 can be positive or negative numbers
y = 50 or y = -50 can be positive or negative numbers and
temp = 25 or temp = -2

Desire is to send all this in a string such as for example
*- 50 79 25, all numbers can be a digit or more and could be negative.
or maybe something like this
*x=value y=value temp=value

Best regards (sorry my english)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've used the code here on the forum but what I can not handle is negative numbers.

There have been more that 170, 000 posts. We could not begin to guess which of those posts contain the code you are using.

Quote
I have negative numbers that I have to use in my other Arduino.

If you have negative numbers, you must not be using an unsigned type. That is not, then, the source of your problem.

Quote
Desire is to send all this in a string such as for example
*- 50 79 25, all numbers can be a digit or more and could be negative.
or maybe something like this
*x=value y=value temp=value

Either format is almost trivially simple. Using Serial.print(), the value specified is output as a string.

May I respectfully suggest that you use a closing delimiter, too. Send something like:

<-50 79 25>

This makes it much easier to determine when all the data for a packet has been received, and allows some level of checking for completeness. For example, there are two spaces in the string, so if you read '<', a number, a space, a number, a space, a number, and '>', you could be reasonably certain that there was no data loss.

If you have trouble sending or receiving and parsing the data, post your code. We'll be happy to help you get it working.

Writing it all for you, though, is not something we really want to do.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi again,

Thanks for your reply

The code I'm using is from this thread that is from Rob
What I'm doing right now is that I send my data from the Serial Monitor. I am writing myself in the serial monitor window and see what happens.
I have not tried to send data from my other Arduino. First I want to be sure that it works to receive and then I'll try to send from the correct source (Arduino)

Here is the code I have used where I send 7 characters * 252525 and it works but if I send *- 5-5-5 that is three negative numbers  it does not work. I will use more characters later, but must first solve the problem.
Code:
void  loop(){   // run over and over again
    
    Serial.flush(); // clear the serial buffer before reading new data
  char timeString[7]; // create a string to hold the time value when it's read
  memset(timeString,'\0',7); // initialize that string to all NULL characters
  boolean timeStringValid = false; // declare and initialize a variable to track whether the string has all valid characters
  //Serial.print("GET");
  byte inByte = '\0'; // declare and initialize a byte to read in serial data
  long startTime = millis();//makes the start time = to now
  int timeout = 1000; // timeout after one second
  while(millis() - startTime < timeout && inByte != '*') {
    inByte = Serial.read(); // read data and wait for an asterisk character
  }

  if (inByte == '*') { // if we got the correct start character (instead of a timeout)
    timeStringValid = true; // declare and initialize a variable to track whether the string has all valid characters
    long startTime = millis();//makes the start time = to now
    int timeout = 1000; // timeout after one second
    while(millis() - startTime < timeout && Serial.available() < 6) {
      ; //wait for enough data to be available (14 characters of time string), while doing nothing else
    }
    for (int i=0; i < 6; i++) {
      timeString[i] = Serial.read(); // reach each time string character into a character array
      if(timeString[i] < '0' || timeString[i] > '9') {
        timeStringValid = false;  // if any character is bad then the whole string is bad
      }
    }
Serial.print("Results: ");
    Serial.println(timeString);
  }

  if (timeStringValid == true) {
    char yearString[3];  // create a string to hold the year part of the string
    memset(yearString,'\0',3); // initialize that string to all NULL characters
    strncpy( yearString, timeString, 2); // copy the first four characters of timeString into the year string
    Serial.print("X: ");
    Serial.println(yearString);
    
    char monthString[3]; // create a string to hold the month part of the string
    memset(monthString,'\0',3); // initialize that string to all NULL characters
    strncpy( monthString, timeString+2, 2); // skip four characters, then copy the next two of timeString into the month string
    Serial.print("Y: ");
    Serial.println(monthString);

    char dayString[3];
    memset(dayString,'\0',3);
    strncpy( dayString, timeString+4, 2);
    Serial.print("Temp: ");
   Serial.println(dayString);

 
  }

  Serial.println(""); // print a blank line

  delay(100); // wait for a moment so that output is readable
}
« Last Edit: February 28, 2010, 02:12:05 am by brzi73 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have this code:

Code:
     if(timeString[i] < '0' || timeString[i] > '9') {
        timeStringValid = false;  // if any character is bad then the whole string is bad

When you send a '-' as part of the string, you are declaring that the whole string is invalid.

Try this:

Code:
     if([glow]([/glow]timeString[i] < '0' || timeString[i] > '9'[glow]) &&
                  timeString[i] != '-')[/glow] {
        timeStringValid = false;  // if any character is bad then the whole string is bad
« Last Edit: February 28, 2010, 08:26:02 am by PaulS » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply

I've tried but it does not work.
I've removed all that and now I can send any characters I want.

Thank you for helping me find the error
Logged

Pages: 1 [2]   Go Up
Jump to: