我从串口监视器发送 数字123,arduino得到的是1,2,3,独立的三个数字 。
如果需要发送一个字符串怎么发送?arduino又要怎样接收字符串?
截图:
代码:
/*
verify the function and serial port.
*/
int isodd(int a )
{
if (a % 2 == 0)
return 1;
else
return 0;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available())
{
int a = Serial.read() - '0';
if (isodd(a))
{
digitalWrite(13, HIGH);
Serial.print(a);
Serial.println(" 是偶数");
}
else
{
Serial.print(a);
Serial.println(" 是奇数");
digitalWrite(13, LOW);
}
}
}
|