project to read input values from serial port

Hi,
I have a project that reads setup values from the serial port (from a laptop) which are then used to control the operation of a camera. Everything works fine if i manually set the values in advance but is not going so well when attempting to get the values from the serial port. The values come as a delimited serial stream. A start character is sent to a python program that sends teh serial stream then waits for a stop character. A version of this program works in PIC microcontroller perfectly but i am trying to migrate to AVR and the arduino platform.

A code excerpt showing serial input is below.

At teh moment the issue seems to be that even when the serial port is not connected to the laptop there still seems to be characters in teh input buffer when there should be none - it should be cycling around doing nothing untill it gets input. I would have also posted output from the serial monitor which shows there there are characters appearing in the input buffer that shouldnt be there but it wouldnt allow me to cut and paste.

Any help is much appreciated.

#include <SoftwareSerial.h>
#include <EEPROM.h>

char inByte;
String inString;
int ledOnPeriod, ledDelay, imageInt, numOfImages, startDelay, pumpSpeed;
SoftwareSerial serIO = SoftwareSerial(10, 11, true);
int imageCnt = 0;
int newValues = 0;

void getSetupValues() {
  Serial.println("Getting setup values");
  int bufferStatus = 0;
  int cnt = 0;
  inString = "";
  serIO.write('

);  //send asci start code to pyprog
 
  while (true) {
    if (serIO.available() > 0) {    //something is in teh serial buffer
      bufferStatus = 1;
      inByte = serIO.read();   
 
      if (inByte == '\n') {        //if program recieves a carriage return then break and process teh string
        Serial.print("String: ");
        Serial.println(inString);
        break;
      }

Serial.print(inByte);    //testing
      inString += inByte;   
    }
    delay(40);
    cnt++;
    if (cnt > 50) {break;}    //wait for a total of two seconds for input if none occurs exit
  }
   
  Serial.print("bufferStatus> ");
  Serial.println(bufferStatus);
   
  if (bufferStatus == 0) {      //nothing in the serial buffer
    newValues = 0;   
  }
  else {newValues = 1;}
   
  serIO.write('#');
}

void setup(){
  Serial.begin(9600);
  serIO.begin(9600);
 
  //pinMode(3, OUTPUT);
  //pinMode(4, OUTPUT);
  //pinMode(5, OUTPUT);
  pinMode(10, INPUT);
  pinMode(11, OUTPUT);
  pinMode(9, OUTPUT);
 
  digitalWrite(9, HIGH);  // set the LED on
  Serial.println("Camera LED System");
  Serial.println("Program: CameraLEDDrive");
  Serial.println("Pyprog: CameraLedSystem.py");
  delay(2000);              // wait for 2 second
  digitalWrite(9, LOW);    // set the LED off
 
}

void loop(){
  getSetupValues();
 
  if (newValues == 1) {
    processString();
    delayStart();
    startPump();
    startCamera();
    setupValues();
    getImages();
  }
 
  digitalWrite(9,HIGH); 
  delay(50);
  digitalWrite(9,LOW); 
  delay(200);
  digitalWrite(9,HIGH); 
  delay(50);
  digitalWrite(9,LOW); 
  delay(700);
}

you need to clear both instring and InByte after they do what ever they need to do, especially instring, otherwise it will put out unwanted values.

Also if copy and paste dont work, try a screen shot.

I would have also posted output from the serial monitor which shows there there are characters appearing in the input buffer that shouldnt be there but it wouldnt allow me to cut and paste.

How are you trying to cut and paste? Drag the mouse over the text of interest, press Ctrl-C, come here, and press Ctrl-V.

SoftwareSerial serIO = SoftwareSerial(10, 11, true);

What is connected to these pins? Why is invert set to true?

katesfb:

String inString;

I can't say for certain that your use of String is causing any problems, but it might be. I recommend that you use plain old 'C' strings (null-terminated char arrays) instead. Also, since your problem seems to be associated with reading from a non-standard serial port, it would be helpful to have details of what is connected to your software serial port (the remote device) and how it is connected (the circuit between them).

Hi,
And thanks for all the replies. Firstly i am running the program on a boarduino (cut down version of the arduino), the software serial pins are connected to a PC/laptop and i am using the 'true' command part of SoftwareSerial cos if a dont i just get garbage coming thru (inverted??). Anyway, i think i may have found the issue - i made the recommended changes to the code and ran it on the normal arduino board (Uno R3) at home and it worked fine but on the boarduino it still had the same problems but i discovered that the problem only occurs if the Bauduino software serial pins are not connected to the PC. If it is plugged in but not receiving any data then it works as it is supposed to but as soon as i unplug it - it goes back to seemingly receiving characters that cannot be their - i think they are probably cariage return characters (string of squares in the serial monitor). This behavior does not occur on the Uno R3 which does exactly what it should whether is is connected or not and leads me to believe that it may be an boarduino issue as all connections seem fine and ground connection is solid.

Does anybody have any idea why this is occurring.

The circuitry from the PC to the software serial input lines is simply a 22k ohm series resistor on the software serial input (RX) line nad direct connection for ground and output (TX) line - this works fine on the UNO R3 so cant see why it wouldn't work on the Boarduino. The only thing i dont have is a resistor across ground and the boarduino RX line but i don't require this on the Uno R3 so should be the same for the boarduino.

Below is output from the serial monitor of the correct output when operating correctly (plugged in) and when not plugged in.
The gaps before "bufferStatus = 1" in the "not plugged in" example is where the string of squares (cariage return??) are, again i could not use ctrl-c adn ctrl-v to paste teh output when the software serial lines were disconnected from teh PC - would suggest ground issue but ground seems fine.

Any help is much appreciated.

Camera LED System
Program: CameraLEDDrive
Pyprog: CameraLedSystem.py

getting SetupValues
bufferStatus> 0
newValues = 0
getting SetupValues
10:150:2:3:0:40:
string> 10:150:2:3:0:40:
bufferStatus> 1
newValues = 1
getting SetupValues
bufferStatus> 0
newValues = 0

Camera LED System
Program: CameraLEDDrive.c
Pyprog: CameraLedSystem.py

getting SetupValues
bufferStatus> 1
newValues = 1
getting SetupValues
bufferStatus> 1
newValues = 1
getting SetupValues
bufferStatus> 1
newValues = 1

Please clarify the following:
Are you connecting boarduino to laptop RS232 (9-pin) port with some resistor stuff?
Can you provide pictures?
Can you describe how you connect boarduino and pc with gnd? Possibly show a picture?

Might you not just be suffering from a 'floating input pin' condition when the cable is disconnected and noise is causing false start bit detection? I would try a pull-down resistor just because it's easy to do and see if that has a positive effect.

Lefty

Hi lefty and thanks others for all your replies and help and yes you were right, i just needed a pull down resistor across input and ground and its now working fine - i probably should have done this right from the start but because the Uno R3 didint need one i thought the boarduino would be the same - important lesson learned here - never discount the obvious. Its even more anoying that when i work with serial coms on PIC micros i always have to use a pull down!

Anyway, thanks for all your help.

Cheers.