Arduino - UART communication

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. Hi
    I am using a spark fun red board and I get a com of 51. In what file do I need to change the com port from 4 to 51
    many thanks

    ReplyDelete