Go Down

Topic: Beginner- Reading 5 bytes from rx. (Read 427 times) previous topic - next topic

luxeomni

I ve got a serial device connected to my arduino, and i know it send packets of 5 bytes, i want it to store the 5 bytes into  buffers when the first byte is corresponding to the start of the packet. and then do the computation that i want it to do ( forming 3 int) and output the results to the  serial monitor.
My questions are : how do i set up the arduino sketch to 19200 8n1 to communicate correctly from the serial device, and then tell the arduino sketch to buffer the bytes.
I know it's beginner questions, and i don't expect anyone to do all the work for me, just general directions, thanks.

regards

retrolefty

how do i set up the arduino sketch to 19200 8n1 to communicate correctly from the serial device,

A simple Serial.begin(19200); added to the setup function in your sketch will activate the serial port at the correct baud rate. The 8n1 is already the standard default settings.


and then tell the arduino sketch to buffer the bytes.

The serial library already buffers up to 32 characters in a library buffer on a interrupt basis. All you have to do is test that a charater is indeed ready to be read by your sketch using a Serial.avalible command and then using Serial.read() to fetch each character one at a time. You are then free to store them away in your own buffer in your sketch for furthern action.


I know it's beginner questions, and i don't expect anyone to do all the work for me, just general directions, thanks.

You should have little problem reading existing sketches on how serial communications is handled. Just keep in mind the serial library is a character at a time operation, it's up to your programming to turn multiple characters into variables or strings.

Lefty

zoomkat

Some simple code you can tinker with using the serial monitor.

Code: [Select]

// 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(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

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

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

    readString="";
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

PaulS

Quote
The serial library already buffers up to 32 characters in a library buffer on a interrupt basis.

Code: [Select]
// Define constants and variables for buffering incoming serial data.  We're
// using a ring buffer (I think), in which rx_buffer_head is the index of the
// location to which to write the next incoming character and rx_buffer_tail
// is the index of the location from which to read.
#if (RAMEND < 1000)
  #define RX_BUFFER_SIZE 32
#else
  #define RX_BUFFER_SIZE 128
#endif

Or 128 characters, depending on how much memory is available.

Go Up