Serial Communication Problem!!

Hi everyone, I really need your help ! Okay before I start here's my code below :

#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....
     inputstring = "C\r"; // send command C to device and carriage return
   
  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
      }
}

The only way I can get it working is by writing at the serial monitor of arduino. I know it's not Serial Write since it only print in the serial monitor but not send in the command into the device.

I'm trying to send some character into the inputstring but no matter what I've done it doesn't apply why's that so? I also include a statement of sending a command letter C with a carriage return to the device and I've already set the same baud rate. If I get this going I want to implement into a push button or switch. So whenever I need it I would switch it on and whenever I don't need it I will switch it off.

I cannot understand your problem from your description, but looking at your code I see that you use serialEvent(). This function will run each time through the loop() function if serial data is available. In serialEvent() you are reading a single chracter which you add to inputstring

  char inchar = (char)Serial.read();                               //get the char we just received
  inputstring += inchar;                                           //add it to the inputString

but at the start of loop() you have
  inputstring = "C\r";which will replace the data received.

Thanks UKHeliBob for replying. YA! the last sentence you said

but at the start of loop() you have
Code:
inputstring = "C\r";
which will replace the data received.

This is what I want to do that is to automatic write a string of message ( Commands ) whenever I push a button or toggle a switch. But I tried to replace the string like what you said nothing ran or happen....

I don't like serialEvent() because I can't control it and there is no need to deal with each character immediately it arrives - that makes a nonsense of having a serial input buffer.

It is perfectly easy to use Serial.available() to check if there is data available in the input buffer and then read it.

...R

  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
         }

You shouldn't use while here, you could read beyond the carriage return. JUst an if will be fine, loop() does
all the looping needed. sensor_stringcomplete is superfluous as you can handle the sensorstring being
complete immediately.

Thanks MarkT and Robin2 for replying

@Robin2 So what you meant is if I remove the function Serial.event and bring out the text inside void loop() and put a statement such

if(switch ==1)
{
char inchar = " C \r";
inputstring += inchar;
if (inchar =='\r')
{input_stringcomplete = true;}
}

I'm able to program a statement out and automatic write a string of message into the device by itself?

@MarkT I know what you meant...... It was the my first problem when I try program it but I've already solved the problem. Thanks for giving me the advice I would take note in future.

Thanks everyone I already able to press a button and send the command to the device already.

I'm guessing that your code works now.

I'm afraid I don't understand this (but perhaps it doesn't matter).

@Robin2 So what you meant is if I remove the function Serial.event and bring out the text inside void loop() and put a statement such
......
I'm able to program a statement out and automatic write a string of message into the device by itself?

...R