Go Down

Topic: Atlas scientific ph/orp stamp RS232 (Read 6 times) previous topic - next topic

PaulS

Code: [Select]
delay(1000);
  mySerial.print("l0");           //the command "c" will tell the stamp to take continues readings
  mySerial.print(13,BYTE);       //ALWAYS end a command with <CR> (which is simply the number 13) or//(print("/r")

The delay is unnecessary. The probe has no way of knowing that you want to talk to it until you talk to it. So start talking right away.

It's nice that your comments match the code... Makes it real easy to understand what "l0" (or "r") does.

Code: [Select]
if(mySerial.available() > 0) {        //if we see the more than three bytes have been received by the Arduino
The code and comments again do not agree. You are doing something if 1 or more bytes of data are available to read, not 3.

Quote
Serial.print(stamp_data);             //printing each byte we received  through the hardware UART

The Serial.print() method that takes a character array as an argument expects that the array will be NULL terminated. Yours is not, which explains the garbage that you are seeing.

Add a NULL after each valid character is added.
Quote
for(i=1; i <= holding;i++)
    {
        stamp_data= mySerial.read();
        stamp_data[i+1] = '\0'; // <== add this line
    }


Code: [Select]
    mySerial.flush(); 
You asked the device for data, and you are randomly throwing away some of the data that it returns. OK. Works for me.

ossama

Thank you soo much Paul

I am still getting junk, the thing is that is it always in the same spot i.e

on continuous mode (i.e 

Code: [Select]

mySerial.print("18");           //the command "c" will tell the stamp to take continues readings
mySerial.print(13,BYTE);       //ALWAYS end a command with <CR> (which is simply the number 13) or//(print("/r")


serial output:

7.y4
7.y4
7.y4
7.y4
7.y4
7.y4
7.y4
7.y4
7.y4
7.94   =====> hehehe strange! I got a nice reading here, it happends every 50 reeading or so, randomly
7.y4
7.y4
7.y4
7.y4
7.y4
7.y4
7.y4
7.y4

if i take probe out:

chÅck àroÂe
chÅck@proÂe
chÅck@prÏbe
chÅck àroÂe
chÅck àroÂe
chÅck àroÂe
chÅck@proÂe
chÅck@proÂe
chÅck@prßbe

My code is as per you suggestions

Code: [Select]

#include <NewSoftSerial.h>               //this will let the software take advantage of the "newsoftserial" library.                   

NewSoftSerial mySerial =  NewSoftSerial(6, 7);

char stamp_data[15];         
//this is where the data from the stamp is stored. The array is 15 char long because
//if no pH probe is connected, the message "check probe" is transmitted.
//Having an array that is too small will cause data corruption and could cause the Arduino to crash.
byte holding;                       
//used to tell us the number of bytes that have been received by the Arduino and are
//holding in the buffer holding
byte i;                 


void setup()

{
  mySerial.begin(38400);   
  Serial.begin(57600); 
  delay(1000);
  mySerial.print("l0");           //the command "c" will tell the stamp to take continues readings
  mySerial.print(13,BYTE);       //ALWAYS end a command with <CR> (which is simply the number 13) or//(print("/r")
delay(1000);
  mySerial.print("c");           //the command "c" will tell the stamp to take continues readings
  mySerial.print(13,BYTE);       //ALWAYS end a command with <CR> (which is simply the number 13) or//(print("/r")
 
}
         
void loop() {                                          //main loop

if(mySerial.available() > 0) {                   //if we see the more than three bytes have been received by the Arduino

    holding=mySerial.available();              //lets read how many bytes have been received
    for(i=1; i <= holding;i++){                    //we make a loop that will read each byte we received
        stamp_data[i]= mySerial.read();     //and load that byte into the stamp_data array
        stamp_data[i+1] = '\0';                   // <== add this line
    }

    for(i=1; i <= holding;i++){                  //we now loop through the array       
    Serial.print(stamp_data[i]);             //printing each byte we received  through the hardware UART
       }

       Serial.println("");                         //once we finished, we print a null char with the <CR><LF> command.
    mySerial.flush(); 
    }
}



Any ideas Mate

ossama

I meant

mySerial.print("c");

instead of

mySerial.print("18");

PaulS

Code: [Select]
    for(i=1; i <= holding;i++){                    //we make a loop that will read each byte we received
Array indexes start at 0. Why don't you?

Code: [Select]
    for(i=1; i <= holding;i++){                  //we now loop through the array       
    Serial.print(stamp_data[i]);             //printing each byte we received  through the hardware UART
       }

       Serial.println("")

If you started in position 0, you could replace all this with
Code: [Select]
Serial.println(stamp_data);

Code: [Select]
    mySerial.flush(); 
This was NOT per my suggestion. In fact I strongly encouraged you to get rid of it.

ossama

Code: [Select]
#include <NewSoftSerial.h>               //this will let the software take advantage of the "newsoftserial" library.                   

NewSoftSerial mySerial =  NewSoftSerial(6, 7);

char stamp_data[15];         
//this is where the data from the stamp is stored. The array is 15 char long because
//if no pH probe is connected, the message "check probe" is transmitted.
//Having an array that is too small will cause data corruption and could cause the Arduino to crash.
byte holding;                       
//used to tell us the number of bytes that have been received by the Arduino and are
//holding in the buffer holding
byte i;                 


void setup()

{
  mySerial.begin(38400);   
  Serial.begin(57600); 
delay(1000);
  mySerial.print("l0");           //the command "c" will tell the stamp to take continues readings
  mySerial.print(13,BYTE);       //ALWAYS end a command with <CR> (which is simply the number 13) or//(print("/r")
delay(1000);
  mySerial.print("c");           //the command "c" will tell the stamp to take continues readings
  mySerial.print(13,BYTE);       //ALWAYS end a command with <CR> (which is simply the number 13) or//(print("/r")
 
}

         
void loop() {                                       //main loop


if(mySerial.available() > 0) {        //if we see the more than three bytes have been received by the Arduino

    holding=mySerial.available();      //lets read how many bytes have been received
   
   
    for(i=0; i <= holding;i++){       //we make a loop that will read each byte we received
        stamp_data[i]= mySerial.read();     //and load that byte into the stamp_data array
        stamp_data[i+1] = '\0'; // <== add this line
    }

    Serial.println(stamp_data);   



    }
}



With this code I still get garbage, why.? why are the others in this post not getting this?
I am using an Arduino fio 328 3.3v

output is

5.e45.e45.e4
5.u45.e45.e0
5.e05.u45.e4
5.e45.u45.e4
5.eÿ
5.e0ÿ
5.u0ÿ
5.e4ÿ
5.e0ÿ
5.e4ÿ





Go Up