Arduino - UART communication

Saturday, December 17, 2016

This example will show how to send/receive String from Arduino to computer (PC) through COM (UART) Port




Make program as here (download at this link - Google share):
String mySt;
char myChar = 0;

void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  establishContact();  // send a byte to establish contact until receiver responds
}

void loop() {
  if (Serial.available() > 0) {
    myChar = Serial.read();
    mySt +=myChar;
  }
  if ((mySt.length() >0)&(!Serial.available())) {
    Serial.print(mySt);
    mySt="";
  }
}
void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println("Send me something");
    delay(3000);
  }
}


After download program, open Serial monitor from IDE: tool > Serial Monitor (or press Shift+CTL+M)
Configure COM port in the Monitor with exactly baudrate (if wrong, it will show something can't be read for human)
In this example, sentence "Send me something" will be shown in every 3 seconds. It will stop when a sentence is sent from text box.




2 comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks, You wrote awesome, I have learn lots of things from your article. It's really helpful for any readers.
    Battery Operated Flow Meter

    ReplyDelete

 

Search This Blog

Most Reading

Google+ Followers

...