Go Down

Topic: Serial.read() help (Read 14698 times) previous topic - next topic

mikalhart

Yipes!  What am I thinking..?!  Sorry: fixed.  :D

Thanks, j.

M

brzi73

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)

PaulS

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.

brzi73

#18
Feb 28, 2010, 08:10 am Last Edit: Feb 28, 2010, 08:12 am by brzi73 Reason: 1
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: [Select]
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
}


PaulS

#19
Feb 28, 2010, 02:24 pm Last Edit: Feb 28, 2010, 02:26 pm by PaulS Reason: 1
You have this code:

Code: [Select]
     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: [Select]
     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


brzi73

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

Go Up