Problem with Software Serial

Hi,

I am using the following code with the arduino and i am getting some random characters. Please let me know whats worng with the code and how to modify this code to use the hardware serial instead of software serial if the problem is with software serial.

The ouput is as follows

ÿ-,--,--
æ0502,11072,12
æ6023,8653,9
Ë-,--,--
ÿ3834,7471,8
þ622,1956,0
æ0502,11072,12

Thank you

ec.ino (4.71 KB)

I am using the following code with the arduino and i am getting some random characters.

From? The Mars rover?

Check your serial monitor setting - do they match the ones the program is using.

Mark

Hi,

The serial monitor settings are correct and i used the same code for testing some others sensors they worked fine but got this problem with only one sensor.

Thank you

Typically these readings arise when in setup u write Serial.begin (9600) and your serial monitor is reading at 115200 or at any other setting.

Also try to lower the baud rate to 9600 if it has been set higher

When i set baud rate to 9600 then no readings at all.

Thank you

jahnavi: but got this problem with only one sensor.

So why do you think it's not the sensor then?

Hi,

Actually i mailed the people from whom i brought the sensor stating the problem but then they told that the problem is with softwawre serial and asked me to use the hardware serial instead of it. So i just want to give a try using hardware serial. Can anyone help me to convert the above linked code to use hardware serial instead of software serial.

Thank you

jahnavi: Actually i mailed the people from whom i brought the sensor stating the problem but then they told that the problem is with softwawre serial and asked me to use the hardware serial instead of it.

Well if they want you to use "proper real hardware" serial then you are going to need more hardware to get the signal up to 15 volts DC, but hopefully they just mean hardware handshaking (and 5 volts will work). That means you are going to have to use more pins to implement DTR (data terminal ready) and CTS (clear to send). You will also need to know which device is acting in which capacity. They may also require carrier Detect. You need more information (along with the required protocol parameters speed, parity, data bits, stop bits). Until you get that you are at the trial and error level. Ask them for specific information.

SoftwareSerial myserial(rxpin, txpin);       

myserial.begin(38400);

The online reference for SoftwareSerial says it supports up to 115200 bps bps, but I have read comments that it is not reliable at high speed. 38400 bps seems rather high to me and it's possible that the problem is the SoftwareSerial library just isn't handling that speed reliably. The symptoms of noise/character corruption at the start of transmission are consistent with that. This also explains why the vendor has advised you to use hardware serial.

I haven't used it, but there is an alternative software serial library (AltSoftSerial) which has several advantages over the standard SoftwareSerial and perhaps if the standard one is the problem it would be worth trying that.

Hi,

I am attaching the spec sheet for the sensor. Please let me know how to modify the below posted code which uses software serial to the code that uses hardware serial port.

/*
This software was made to demonstrate how to quickly get your Atlas Scientific product running on the Arduino platform.
An Arduino Duemilanove board was used to test this code.
This code was written in the Arudino 1.0 IDE
Modify the code to fit your system.
Code efficacy was NOT considered, this is a demo only.
The soft serial port TX line goes to the RX pin.
The soft serial port RX line goes to the TX pin.
Make sure you also connect to power and GND pins to power and a common ground.
Data is received and re-sent through the Arduinos hardware UART TX line.
Open TOOLS > serial monitor, set the serial monitor to the correct serial port and set the baud rate to 38400.
Remember, select carriage return from the drop down menu next to the baud rate selection; not "both NL & CR".
The data from the Atlas Scientific product will come out on the serial monitor.
Type in a command in the serial monitor and the Atlas Scientific product will respond.
*/




#include <SoftwareSerial.h>                                                    //add the soft serial libray
#define rxpin 2                                                                //set the RX pin to pin 2
#define txpin 3                                                                //set the TX pin to pin 3


SoftwareSerial myserial(rxpin, txpin);                                         //enable the soft serial port


String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product


  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port to 38400
     myserial.begin(38400);                                                    //set baud rate for software serial port to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     }
 
 
 
   void serialEvent() {                                                         //if the hardware serial port receives a char
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              }  
 
 
 
 void loop(){                                                                   //here we go....
     myserial.print("r\r");
  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      myserial.print(inputstring);                                             //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }
 

  while (myserial.available()) {                                               //while a char is holding in the serial buffer
         char inchar = (char)myserial.read();                                  //get the new char
         sensorstring += inchar;                                               //add it to the sensorString
         if (inchar == '\r') {sensor_stringcomplete = true;}                   //if the incoming character is a <CR>, set the flag
         }


   if (sensor_stringcomplete){                                                 //if a string from the Atlas Scientific product has been received in its entirety
       Serial.print(sensorstring);                                             //use the hardware serial port to send that data to the PC
       sensorstring = "";                                                      //clear the string:
       sensor_stringcomplete = false;                                          //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
      }
}

Thank you

EC_Circuit_3.0 spec sheet.pdf (680 KB)

Please let me know how to modify the below posted code which uses software serial to the code that uses hardware serial port.

Isn't that obvious? Use Serial in place of myserial!

You could try some simple code like below with the hardware serial pins. Connect the device tx to the arduino rx, and connect the device and arduino grounds together. Start the serial monitor and see if the device sends the ascii digits to the serial monitor.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(38400);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

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

    readString="";
  } 
}

Hi,

I tried with the above code but not getting any ASCII characters in the output. I think should send '"r\r" to get single reading or "c\r" to get continous readings.

Thank you.

You could add one more wire between the device tx and the arduino rx pins. Then in the serial monitor change the “no line ending” option to carriage return so a carriage return is added to what is sent from the serial monitor. Then send an R from the serial monitor and see if any data appears. The below is from the device data sheet.

R Instructs the E.C. Circuit to return a single E.C. reading.
*This instruction takes 1000 milliseconds to complete
When using the “R” command the temperature is defaulted to 23° C
if no temperature data has been entered. If temperature data has been
entered then the last temperature entered is used.
Full proper syntax: r or R
The E.C. Circuit will respond: EC,TDS,SAL
Where:
EC is the electrical conductivity in ?s
TDS is Total Dissolved Solids (referenced to Kcl)
SAL is Salinity (Practical Salinity scale 1978) expressed as a whole number only.
Taking a single reading is not considered scientifically accurate. The E.C. Circuits readings
will become accurate after ~ 15-25 continues readings. A single reading will only give you
an estimate of the conductivity.
If the Sensor responds with “–” the E.C, TDS or salinity is out of range.
Such as:
70000,43000,–
This is because the PSS (practical salinity scale) only goes to 42; which is reached
at a conductivity of 62,290 ?s
“–“can also be seen when using a Sensor that is not designed for the type of water
it is immersed in. Using a K.01 Sensor in saltwater would produce that type of reading.

Hi,

Can you please tell me the code that is used to send the command ‘c’ (without using the serial monitor).

Thank you