极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15075|回复: 6

arduino串口通信问题

[复制链接]
发表于 2014-6-13 16:15:02 | 显示全部楼层 |阅读模式
新手学习使用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
求助,谢谢
回复

使用道具 举报

发表于 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.
回复 支持 反对

使用道具 举报

发表于 2014-6-14 08:31:54 | 显示全部楼层
讀取串口數據時,建議加一行
if(Serial.available()>0)
回复 支持 反对

使用道具 举报

发表于 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

   }
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-6-16 09:27:17 | 显示全部楼层
i7456 发表于 2014-6-13 23:35
Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32 ...

谢谢, 这个“use the Serial1 class” 怎么理解?
回复 支持 反对

使用道具 举报

发表于 2014-6-16 09:40:12 | 显示全部楼层
redbats 发表于 2014-6-16 09:27
谢谢, 这个“use the Serial1 class” 怎么理解?

leonardo有两个串口
与电脑USB通讯用Serial
用D0,D1串口通讯用Serial1
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-6-18 14:32:47 | 显示全部楼层
i7456 发表于 2014-6-16 09:40
leonardo有两个串口
与电脑USB通讯用Serial
用D0,D1串口通讯用Serial1

非常感谢,已经可以正常读到了,leonardo特有的
对D0,D1串口操作,所有的命令改为Serial1 ,如Serial1.read()等就可以通过了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-9 12:50 , Processed in 0.067428 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表