树莓派与Arduino的USB串口通讯

import serial
import time


ser = serial.Serial('/dev/ttyACM0', 9600,timeout=1)
abc= bytes("s","utf-8")
try:
  while 1:
    ser.write(abc);#写s字符
    response = ser.readall();#用response读取端口的返回值
    print (response);#进行打印
except:
  ser.close();#抛出异常后关闭端口




](https://cdn.chandianzi.cn/wp-content/uploads/csstm/c1b56c1b84834a0ca870f85dea213c4b.bmp?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAejg0Mzc3NzYyOA==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)

![
int led = 13;
void setup(){
Serial.begin(9600); // 9600波特率

pinMode(led, OUTPUT);
}

void loop() {
if ( Serial.available())//判断串口缓存区有没有数据
{
if(‘s’ == Serial.read())//有数据就用read来读取并判断是不是s

// the setup routine runs once when you press reset:

// the loo
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100);

    Serial.println("Hello Raspberry,I am jiayu's Arduino.");//是的话就向串口打印这串字符
 }

}
](https://cdn.chandianzi.cn/wp-content/uploads/csstm/92682460b4a94e228cb0b3c386c1a529.bmp?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAejg0Mzc3NzYyOA==,size_19,color_FFFFFF,t_70,g_se,x_16#pic_center)

版权声明:本文为CSDN博主「z843777628」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/z843777628/article/details/118718634

import serial
import time


ser = serial.Serial('/dev/ttyACM0', 9600,timeout=1)
abc= bytes("s","utf-8")
try:
  while 1:
    ser.write(abc);#写s字符
    response = ser.readall();#用response读取端口的返回值
    print (response);#进行打印
except:
  ser.close();#抛出异常后关闭端口




](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/c1b56c1b84834a0ca870f85dea213c4b.bmp?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAejg0Mzc3NzYyOA==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)

![
int led = 13;
void setup(){
Serial.begin(9600); // 9600波特率

pinMode(led, OUTPUT);
}

void loop() {
if ( Serial.available())//判断串口缓存区有没有数据
{
if(‘s’ == Serial.read())//有数据就用read来读取并判断是不是s

// the setup routine runs once when you press reset:

// the loo
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100);

    Serial.println("Hello Raspberry,I am jiayu's Arduino.");//是的话就向串口打印这串字符
 }

}
](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/92682460b4a94e228cb0b3c386c1a529.bmp?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAejg0Mzc3NzYyOA==,size_19,color_FFFFFF,t_70,g_se,x_16#pic_center)

版权声明:本文为CSDN博主「z843777628」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/z843777628/article/details/118718634

生成海报
点赞 0

z843777628

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

arduino与激光雷达?

先上图片: 一般来说,这样子的激光雷达都是与电脑或者树莓派等等配合使用的,但是暂时没有时间去捣鼓slam算法相关的东西,那有没有方法把它应用在其他简单的项目中呢? 。。。。我

电控部近期学习总结

目录 Arduino基础入门 基于arduino板控制小车 让小车动起来 学习PWM频率和占空比 初步调速 利用麦克纳姆轮原理控制全向移动 加入遥控小车功能 稳定小车速度 Arduino基础入门 最开始那几天,由于没有找到好用的教