Go Down

Topic: Arduino Serial Monitor line length limit? (Read 1 time) previous topic - next topic

Sep 01, 2013, 10:47 pm Last Edit: Sep 02, 2013, 11:43 am by IanMcRV Reason: 1
Is there a problem with the Arduino Serial Monitor?

I have the IDE version 1.0.5 and when I send a line of text to the serial monitor from a bluetooth device the line of text ends at character 70 and then displays a random character.

Works fine on a terminal emulator program on my PC.

Unless it's my Arduino code?

Here is the code I'm using:

Code: [Select]
#include <SoftwareSerial.h>
// using the SoftwareSerial library allows the developer to choose
// which pins to put the bluetooth module on. Unfortunately some
// bluetooth 'shields' are hardwired to use Pins 0 and 1 which makes
// two-way communication with the Serial Monitor impossible :(
// use a cheap 'Linvor' 4 pin bluetooth module instead
// please see the Arduino section in the forum at www.BTInterface.com
// Set the Arduino Serial Monitor to 9600 Baud and no line ending.

// Receive pin (RX), Transmit pin (TX) of the Bluetooth module
SoftwareSerial softSerial(10, 11);  
String command = ""; // Stores response of bluetooth device
int baudrate = 9600;

void setup()  
{
 Serial.begin(baudrate);
 softSerial.begin(baudrate);
 Serial.println("SimpleTerm Ready!");
}

void loop()
{
 // SoftSerial is to and from the Bluetooth device  
 // Serial is to and from the PC (Arduino Serial Monitor).

  if (Serial.available()) softSerial.write(Serial.read()); // send from Arduino to Bluetooth
  while(softSerial.available() > 0) { // While there is more to be read, keep reading.
    command += (char)softSerial.read();
    delay(10);  
  }
 
// Your code can go here  
 
 // copy what's in command to the serial monitor.
  if(command != "") Serial.println(command); // send from Bluetooth to Serial Monitor
     
  command = ""; //empty the command string ready for the next use.
}// END loop()

PaulS

Code: [Select]
   while(softSerial.available() > 0) { // While there is more to be read, keep reading.
     command += (char)softSerial.read();
     delay(10); 
   }

What                         is                              the                       purpose                    of                   the                         delay?

Serial data should be read as fast as it becomes available.

If you know how much data you are going to receive, don't use the String crutch.

#2
Sep 02, 2013, 06:15 pm Last Edit: Sep 02, 2013, 06:20 pm by IanMcRV Reason: 1
Ah, I think I realise why that delay was in there, without the delay the line seems to break in the Serial Monitor such that a line that should read:

sfx10;say Welcome to my super device;1;1t My Super Device;b2h;b3h;b4h;b1 Press Me

will break and read:

sfx10;s
ay Welcome to my super device;1;1t My Super Device;b2h;b3h;b4h;b1 Press Me

Is there a better way to prevent that from happening in my Arduino code?

I don't want to use 'prefix' mode if I can get away without it.

delay(1);

seems to fix it so far :)

zoomkat

Quote
Is there a better way to prevent that from happening in my Arduino code?


You can you can use a "delimiter" to allow the arduino to collect the packets until the delimiter is encountered. The arduino checks the serial input buffer much faster than the serial input buffer is normally filled. Using the delay in the code causes the arduino to check the input buffer slower than the buffer is being filled so an empty buffer is not encountered until no more data is being sent. Below is simple code that uses a delay, and bottom is code that uses a comma as a delimiter.

Code: [Select]

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3); 
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  }
}





Code: [Select]

//zoomkat 3-5-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
//send on, or off, from the serial monitor to operate LED

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      Serial.println(readString); //prints string to serial port out
      //do stuff with the captured readString
      if(readString.indexOf("on") >=0)
      {
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON");
      }
      if(readString.indexOf("off") >=0)
      {
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF");
      }       
      readString=""; //clears variable for new input
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Hi, yes thanks for your input.

I have a Bluetooth module with RX/TX on pins 10 & 11 using the SoftwareSerial.h library.

This enables two-way comms with no problems, i found for example that when I connected the Bluetooth module to pins 0 and 1 although I got it working I'd have to take the module off each time I wanted to upload a sketch so for my application the SoftwareSerial library is the way to go so my routine:
Code: [Select]
   if (Serial.available()) softSerial.write(Serial.read()); // send from Arduino to Bluetooth
   while(softSerial.available() > 0) { // While there is more to be read, keep reading.
     command += (char)softSerial.read();
     delay(1);
   }

is the same as yours, I find at the moment that a delay of 1 does the job nicely.

I don't want to have to use delimiters but appreciate the advice.

Thanks!

Go Up