3

how i can read negative number with command Serial.read()? beacouse i with this

 char tmpChar = Serial.read(); //read incoming character from serial port
 if (isDigit(tmpChar)) { //check if the in character it's a number
 setpoint = (setpoint * 10 ) + tmpChar - '0'; // convert from ascii code to it's corresponding number; '0' == DEC 48
 }
if (tmpChar == '\n' || tmpChar == '\r') { //if CR or LF was received
 if (setpoint > 0) {
 tmpChar = "";
 Serial.print(setpoint);
 Serial.println(" NUMERO STEP IN AVANTI");
 }

im using an encoder motor, i want to move for N steps the motor forward (positive number) or backward (negative number) , i insert the number of step in serial input

i can read only positive number. Thanks for help

how suggest Majenko i made a partial solution :

char buf[8];
int valore = 0;
void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
}
void loop() {
 if (Serial.available() >= 8) {
 for (int i = 0; i < 8; i++) {
 buf[i] = Serial.read();
 }
 String str();
 if (buf[0] == "-") {
 valore = (atoi(buf)) * -1;
 Serial.println(valore);
 }
 else {
 valore = atoi(buf);
 Serial.println(valore);
 }
 }
}

but the problem now it's the static array , unpleasant to use

asked Jun 8, 2017 at 10:32

3 Answers 3

0

Personally I would read the data into a char array then use either atoi() or strtol() to convert that char array into a number.

Using those functions is more robust that parsing the data yourself since they identify where the number ends and discards any trailing garbage. It also skips any white space before the number. For instance it will interpret all these as the number -32: " -32", "-32 Degrees", " -32+4 " and "-32-2".

strtol() has the additional advantage that it can work in other bases than 10, so you could interpret hexadecimal data, binary data, sexagesimal data, etc.

atoi() and strtol() are standard C functions.

E.g.:

int val = atoi(mycharbuffer);

or

long val = strtol(mycharbuffer, NULL, 10);
answered Jun 8, 2017 at 10:55
6
  • can make an example please Commented Jun 8, 2017 at 10:57
  • Of what - the reading into the char buffer, or the converting to integer? Commented Jun 8, 2017 at 10:59
  • to convert readed char in a negative int , thanks for help Commented Jun 8, 2017 at 11:00
  • Done. See my edit. Commented Jun 8, 2017 at 11:00
  • im tryng with this without succes, where im wrong? void loop() { char tmpChar = Serial.read(); long val = strtol(tmpChar, NULL, 10); Serial.println(val); } Commented Jun 8, 2017 at 11:09
1

You could do something like this:

static int setpoint; // the partially read setpoint
static bool isNegative; // whether it should be negative
void applySetpoint(int); // apply the setpoint when fully read
char tmpChar = Serial.read();
if (isDigit(tmpChar)) {
 setpoint = (setpoint * 10 ) + tmpChar - '0';
} else if (tmpChar == '-') { // minus sign
 isNegative = true;
} else if (tmpChar == '\r') { // line terminator
 if (isNegative) {
 setpoint = -setpoint;
 isNegative = false; // reset for next read
 }
 applySetpoint(setpoint);
}

Note that this is not very robust: it will happily interpret "3-2" as meaning "-32". Note also that I assumed your terminator to be '\r', you may be using another one.

answered Jun 8, 2017 at 10:40
0

The parseInt can read negative numbers (I think it does, the reference is very cryptic). The parseInt has a timeout so it does not wait forever.

Can you tell what kind of data you are reading ? Is it part of more data ?

answered Jun 8, 2017 at 10:48
2
  • im using an encoder motor, i want to move for N steps the motor forward (positive number) or backward (negative number) , i want to insert the number of step in serial input Commented Jun 8, 2017 at 10:52
  • Just a number then ? When parseInt has a timeout, it returns 0, that means you can't send a 0. If that is okay, then check for Serial.available and do a Serial.parseInt. Commented Jun 8, 2017 at 12:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.