아두이노 보드끼리 uart핀을 서로 연결하여 시리얼 데이타를 주고 받을 수 있습니다.

이때 보내는 쪽 보드(TX)의 TX와 받는쪽 보드(RX)의 RX와 연결해주고, 보내는보드의 RX핀과 받는보드의 TX와 연결해주어야 시리얼통신이 됩니다.

보내는 보드는 아두이노 나노, 받는 보드는 우노보드를 사용하였습니다. 

 

보내는 측(TX, 나노보드)의 소스코드는

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(115200);

}

 

// the loop routine runs over and over again forever:

void loop() {

// read the input on analog pin 0:

char msg[] = "Hello World";

// print out the value you read:

Serial.write(msg);

Serial.println();

delay(2); // delay in between reads for stability

}

나노보드의 하드웨어 시리얼핀을 통하여 "Hello World" String을

보내는데 delay(2)를 사용하여 2ms 쉬었다가 한번씩 보내도록 했습니다.

하드웨어 시리얼이므로 baudRate를 115200으로 할 수 있습니다.

배열 char msg[]을 사용하고, Serial.write()함수로 보냈습니다.

char msg[] = "Hello World";

Serial.write(msg);

 

이번에는 수신보드(RX)인 우노보드에 업로드한 코드입니다.

아두이노 ide에 있는 serialEvent()예제코드에서 loop()문에 

Serial.println("OK!");를 추가하였습니다.

이렇게 하면 "OK!"를 시리얼모니터로 출력하면서 동시에 serialEvent()함수로 TX로부터 받은 시리얼값(여기서는 "Hello World")을 출력하게 됩니다.

 

String inputString = ""; // a String to hold incoming data

bool stringComplete = false; // whether the string is complete

 

void setup() {

// initialize serial:

Serial.begin(115200);

// reserve 200 bytes for the inputString:

inputString.reserve(200);

}

 

void loop() {

// print the string when a newline arrives:

Serial.println("OK!");

if (stringComplete) {

Serial.println(inputString);

// clear the string:

inputString = "";

stringComplete = false;

}

}

 

/*

SerialEvent occurs whenever a new data comes in the hardware serial RX. This

routine is run between each time loop() runs, so using delay inside loop can

delay response. Multiple bytes of data may be available.

*/

void serialEvent() {

while (Serial.available()) {

// get the new byte:

char inChar = (char)Serial.read();

// add it to the inputString:

inputString += inChar;

// if the incoming character is a newline, set a flag so the main loop can

// do something about it:

if (inChar == '\n') {

stringComplete = true;

}

}

}

<출력결과>

TX측(나노보드)

RX측(우노보드)

 

RX보드의 loop()문에 있던 "OK!"데이타가 2개 출력될때, TX보드로 부터 받은 "Hello World"데이타가 1개 출력되므로

출력빈도를 1대 1로 맞추려면 TX보드의 출력빈도를 늘려주면 됩니다.

TX보드의 소스코드중 delay타임을 delay(1)로 한 경우 RX보드의 출력데이타가 다음과 같이 깨짐현상이 발생하고,

Hello World 데이타 갯수가 많아 1.5ms로 변경하여 봅니다.

delay(1.5);는 없으므로 delayMicroseconds(1500);으로 변경 후 확인 결과

1개씩 출력하며 1대1로 맞았습니다.

 

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(115200);

}

 

// the loop routine runs over and over again forever:

void loop() {

// read the input on analog pin 0:

char msg[] = "Hello World";

// print out the value you read:

Serial.write(msg);

Serial.println();

delayMicroseconds(1500); // delay in between reads for stability

}

 

마지막으로 TX측 소스코드를 바꾸어 테스트해보면

<소스코드>

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(115200);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0: 
  String msg = "Hello World";   
  // print out the value you read:
  //Serial.write(msg);
  Serial.println(msg);
  delayMicroseconds(1500);        // delay in between reads for stability   
}

배열함수대신 String 변수 msg를 정의하였고, Serial.write(msg)대신 Serial.println(msg) 함수를 사용하였습니다.

 

결과는 동일하나 OK! 데이타가 간혹 2개씩 출력되는 결과가 보입니다.

코드를 수정하며 TX쪽에서 보내지는 시리얼데이타 전송속도가 달라진다는 것을 짐작할 수 있습니다.

 

+ Recent posts