Go Down

Topic: Serial port input[SOLVED] (Read 893 times) previous topic - next topic

vasr

Jul 11, 2013, 02:55 pm Last Edit: Jul 11, 2013, 04:33 pm by vasr Reason: 1
I want print to serial port a sequence of numbers.
I use this code:

Code: [Select]
char buf1[3];
int i;

void setup() {
 Serial.begin(9600);
}

void loop() {

 while (Serial.available() > 0) {
   int x = Serial.parseInt();
   Serial.println(x);
   for(i=2;i>=0;i--){
     buf1[i]= x % 10;
         x /= 10;
       }
   vw_send((uint8_t *)buf1, strlen(buf1));
   
 }
 
}

when i input de numbers one by one in to the serial and press enter its work fine,but when i copy a sequence and paste to the serial port i view the sequence i paste and a zero.
Example:
If a copy paste this sequence"111"
I view:111
         0




MarkT

You don't have a terminating null in your buffer (its too short anyway), so calling strlen() could
return any value from 3 upwards.

Lose the call to strlen, pass 3 to vw_send.  Though you might want to send a newline character too...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

wizarcl

I try this and work's fine. The problem is, when you copy a "space" or "enter" caracter, you send this for controller and like x is int, return 0.

PeterH

You call strlen(buf1) as though it contained a string, but it doesn't - it contains three one-byte integers, not ascii characters. If the array is terminated, it would only be by chance because one of the integers happened to be zero.

If you're trying to print the number as a decimal string I suggest you use snprintf to format the number to a string and then output the string however you want. Your three-byte buffer is not long enough to hold a decimal representation of the numbers you're using in your example so I suggest you make the buffer bigger, and also decide what you're going to do when the input number is too big to be formatted into the output buffer.

Given that you are converting a textual input to a number and then back to text, is there some reason why you don't simply output the same text that was input and avoid the whole problem?

vasr

I only convert to view if the number I send is correct.If i dont convert to integer solve my problem?

vasr

You are right wizarcl.If i dont input any space its works fine.Thank you for all the answers.

RDaneelOlivaw

I have a question reffered to serial.
I got this code but there are some things I don't understand and I can't find them antwere :(. Does someone know what this means?:

int numberFromSerial(void)
{
  char numberString[8];
  unsigned char index=0;
  delay(10);
  while(Serial.available())
  {
    delay(10);
    numberString[index++]=Serial.read();
    if(index>6)
    {
      break;
    }
  }
  numberString[index]=0;
  return atoi(numberString);
}


The "index>6", I dont know what it is :((.

AWOL

Code: [Select]
int numberFromSerial(void)
{
  char numberString[8];
  unsigned char index=0;
  delay(10);
  while(Serial.available())
  {
    delay(10);
    numberString[index++]=Serial.read();
    if(index > 6)
    {
      break;
    }
  }
  numberString[index]=0;
  return atoi(numberString);
}
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up