redbats 发表于 2014-6-13 16:15:02

arduino串口通信问题

新手学习使用arduino leonardo
准备连接一个传感器到arduino的串口, 传感器输出接RX(digital 0)
波特率2400, 数据为0xaa ,循环发送,可是我怎么也读不到这个值
int LEDpin = 13;
byte val=0;

void setup()
{
Serial.begin(2400);
pinMode(LEDpin,OUTPUT);
}

void loop()
{
   val=Serial.read();
   Serial.println(val);
   delay(1000);
}

打开serial monitor,只输出255
求助,谢谢

i7456 发表于 2014-6-13 23:35:37

Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32U4 hardware serial capability. Note that on the Leonardo, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.

hsr18299 发表于 2014-6-14 08:31:54

讀取串口數據時,建議加一行
if(Serial.available()>0)
:)

ken0137 发表于 2014-6-15 14:11:02

或者
void setup()
{
Serial.begin(2400);
pinMode(LEDpin,OUTPUT);
while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
   }
}

redbats 发表于 2014-6-16 09:27:17

i7456 发表于 2014-6-13 23:35 static/image/common/back.gif
Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32 ...

谢谢, 这个“use the Serial1 class” 怎么理解?

i7456 发表于 2014-6-16 09:40:12

redbats 发表于 2014-6-16 09:27 static/image/common/back.gif
谢谢, 这个“use the Serial1 class” 怎么理解?

leonardo有两个串口
与电脑USB通讯用Serial
用D0,D1串口通讯用Serial1

redbats 发表于 2014-6-18 14:32:47

i7456 发表于 2014-6-16 09:40 static/image/common/back.gif
leonardo有两个串口
与电脑USB通讯用Serial
用D0,D1串口通讯用Serial1

非常感谢,已经可以正常读到了,leonardo特有的
对D0,D1串口操作,所有的命令改为Serial1 ,如Serial1.read()等就可以通过了
页: [1]
查看完整版本: arduino串口通信问题