Serial Communication with the Arduino


hello,

i've been following this tutorial interface between c# program , arduino. c# program isn't problem @ moment.

i came following:
code: [select]
#include <arduino.h>

/* constants */
#define stx "\x02"    //ascii-code 02, text representation of stx code
#define etx "\x03"    //ascii-code 03, text representation of etx code
#define rs  "$"       //used rs code

/* warning, error , status codes */
//status
#define msg_method_success 0
//warnings
#define wrg_no_serial_data_available 250
//errors
#define err_serial_in_command_not_terminated -1

int readserialinputstring(string *command);
void writedummyweatherdata();

int main() {
  init();

  serial.begin(9600);

  while(true) {
    string command = ""; // storing latest command received
    int serialresult = 0; // answer code of command received
   
    serialresult = readserialinputcommand(&command);

    if(serialresult == msg_method_success) {
      if(command == "connection#") {
        returnping();
      }
    }
  }
}

void returnping() {
  serial.print(stx);
  serial.print("success");
  serial.print(etx);
}

int readserialinputcommand(string *command) {
  int operationstatus = msg_method_success;
  if (serial.available()) {
      char serialinbyte;//temporary variable hold last serial input buffer character

      // run following until terminating character comes or no data available.
      do{
        serialinbyte = serial.read();
        *command = *command + serialinbyte; //add last read serial input buffer byte received command
      } while (serialinbyte != '#' && serial.available());

      // if command not terminated '#', return error.
      if(serialinbyte != '#') {
        operationstatus = err_serial_in_command_not_terminated;
      }
    }
    else{
      // if no serial input buffer data available, operationstatus becomes wrg_no_serial_data_avaible
      operationstatus = wrg_no_serial_data_available;
    }
   
  return operationstatus;
}


to test code, want write "connection#" serial port (while '#' character indicate command's end) using arduino serial monitor. later on, c# part supposed take on task.

but now, nothing happens when write serial monitor. while "connection#" should return "succcess", nothing happens. placed test-write if(command == "connection#") see if triggered, not case. however, correct status codes generated typed in.

my guess command not read correctly don't know why.
any experts here help? :)

thanks,
daniel

this not arduino code. why not use setup() , loop() arduino gods intended?


Arduino Forum > Using Arduino > Programming Questions > Serial Communication with the Arduino


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts