Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to send strings to Arduino? [closed]

I'm working on a project for my computer science class that basically uses an Arduino board with an LCD as a "message board". The large-scale goal of my project is to have a program on a computer where one can enter a message, which will then be displayed on the Arduino screen. My big sticking point at the moment is how to send a string to the device. I looked at a few different things involving sending individual bytes to Arduino, and also looked at this code which may be some way to send a string to it: http://www.progetto25zero1.com/b/tools/Arduino/

Does anyone have any experience sending strings to the Arduino board, and if so, would you be willing to share your advice for how to go about it? I may have an issue later sending these strings from an external program (not the Ardunio IDE), but the biggest issue for me at this point is just sending the strings to the device, itself.

like image 821
Devin Avatar asked Oct 21 '11 05:10

Devin


People also ask

How do you send a string serially in Arduino?

Example Code void setup() { Serial. begin(9600); } void loop() { Serial. write(45); // send a byte with the value 45 int bytesSent = Serial. write("hello"); //send the string "hello" and return the length of the string. }

Are Arduino strings null terminated?

Generally, strings are terminated with a null character (ASCII code 0). This allows functions (like Serial.


1 Answers

Mitch's links should point you in the right direction.

A common way of sending and receiving strings from the host computer to the Arduino and back is using the Arduino's Serial library. The Serial library reads and writes a byte at a time over the connection to the computer.

The below code forms a String by appending chars received over the Serial connection:

// If you know the size of the String you're expecting, you could use a char[]
// instead.
String incomingString;

void setup() {
  // Initialize serial communication. This is the baud rate the Arduino
  // discusses over.
  Serial.begin(9600);

  // The incoming String built up one byte at a time.
  incomingString = ""
}

void loop() {
  // Check if there's incoming serial data.
  if (Serial.available() > 0) {
    // Read a byte from the serial buffer.
    char incomingByte = (char)Serial.read();
    incomingString += incomingByte

    // Checks for null termination of the string.
    if (incomingByte == '\0') {
      // ...do something with String...
      incomingString = ""
    }
  }
}

To send the serial data --- and to print out the data the Arduino prints --- you can use the Serial Monitor in the Arduino IDE.

like image 176
baalexander Avatar answered Sep 22 '22 12:09

baalexander