首页 > 其他分享 >32、树莓派的简单测试串口通信和超声波模块测距

32、树莓派的简单测试串口通信和超声波模块测距

时间:2022-09-28 11:36:31浏览次数:55  
标签:树莓 32 printf tty fd 串口 GPIO include options


基本思想:随手记录一下众灵科技树莓派的测试串口通信和超声波模块,其镜像还是很nice,基本的库都给你安装了,比较大

链接:https://pan.baidu.com/s/11tMdoRh3bHmcYzPyolm96g 
提取码:fd58

第一步、测试树莓派的串口程序

32、树莓派的简单测试串口通信和超声波模块测距_#include

可以通过桌面的设置,在Raspberry Pi Configuration中将Serial Port设置为Enable, 我全部打开了

32、树莓派的简单测试串口通信和超声波模块测距_串口_02

引脚定义

32、树莓派的简单测试串口通信和超声波模块测距_嵌入式硬件_03

 第二步、根据pin引脚,引入usb转ttl引针,然后打开串口工具

32、树莓派的简单测试串口通信和超声波模块测距_单片机_04

 第三步、先使用权限命令打开权限

确定可用的串口

pi@raspberrypi:~/client/build $ ls /dev/ser* -al
lrwxrwxrwx 1 root root 7 6月 12 09:17 /dev/serial0 -> ttyS0
lrwxrwxrwx 1 root root 5 6月 12 09:17 /dev/serial1 -> ttyAMA0
pi@raspberrypi:~/client/build $

然后测试

pi@raspberrypi:~/sxj731533730 $ sudo chmod 777 /dev/ttyS0

测试代码

pi@raspberrypi:~/sxj731533730 $ cat a.py
import serial
ser = serial.Serial('/dev/ttyS0',9600)
ser.isOpen()
ser.write('Hello CSDN !!'.encode('utf-8'))

python测试结果

32、树莓派的简单测试串口通信和超声波模块测距_stm32_05

 c++测试代码


#include <chrono>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include <iostream>
#include <cstring>

using namespace std;

int sendSerialPort(const unsigned char W_BUF[], int length) {

int tty_fd = -1;

int rv = -1;

struct termios options;

tty_fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); //打开串口设备
fcntl(tty_fd, F_SETFL, 0);
if (tty_fd < 0) {
printf("open tty failed:%s\n", strerror(errno));
close(tty_fd);
return -1;
}

printf("open devices sucessful!\n");


memset(&options, 0, sizeof(options));

rv = tcgetattr(tty_fd, &options); //获取原有的串口属性的配置
if (rv != 0) {
printf("tcgetattr() failed:%s\n", strerror(errno));
close(tty_fd);
return -1;

}

options.c_cflag |= (CLOCAL | CREAD); // CREAD 开启串行数据接收,CLOCAL并打开本地连接模式
options.c_cflag &= ~CSIZE;// 先使用CSIZE做位屏蔽
options.c_cflag |= CS8; //设置8位数据位
options.c_cflag &= ~PARENB; //无校验位
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag &= ~CSTOPB;
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 0;
tcflush(tty_fd, TCIFLUSH);


if ((tcsetattr(tty_fd, TCSANOW, &options)) != 0) {
printf("tcsetattr failed:%s\n", strerror(errno));
close(tty_fd);
return -1;
}


std::cout << std::endl << "length= " << length << std::endl;
rv = write(tty_fd, W_BUF, length);
if(rv<=0) /* 出错了*/
{
if (errno == EINTR) /* 中断错误 我们继续写*/
{
close(tty_fd);
printf("[SeanSend]error errno==EINTR continue\n");
} else if (errno == EAGAIN) /* EAGAIN : Resource temporarily unavailable*/
{
sleep(1);//等待一秒,希望发送缓冲区能得到释放
close(tty_fd);
printf("[SeanSend]error errno==EAGAIN continue\n");
} else /* 其他错误 没有办法,只好退了*/
{
printf("[SeanSend]ERROR: errno = %d, strerror = %s \n", errno, strerror(errno));
return (-1);
}
}
if (rv < 0) {
printf("Write() error:%s\n", strerror(errno));
close(tty_fd);
return -1;
}
for (int i = 0; i < length; i++) {
std::cout << std::hex << (int) W_BUF[i] << " ";

}
close(tty_fd);
printf("\nWrite() successfully\n");
return 0;

}


int main(int argc, char **argv) {
//0xFD 0x00 0x07 0x01 0x01 0xD6 0xB1 0xD0 0xD0 0x9D //直行
//0xFD 0x00 0x07 0x01 0x01 0xD7 0xF3 0xD7 0xAA 0xA3 //左转
//0xFD 0x00 0x07 0x01 0x01 0xD3 0xD2 0xD7 0xAA 0x86 //右转


const unsigned char data[][10] = {{0xFD, 0x00, 0x07, 0x01, 0x01, 0xD6, 0xB1, 0xD0, 0xD0, 0x9D},
{0xFD, 0x00, 0x07, 0x01, 0x01, 0xD7, 0xF3, 0xD7, 0xAA, 0xA3},
{0xFD, 0x00, 0x07, 0x01, 0x01, 0xD3, 0xD2, 0xD7, 0xAA, 0x86}};
sendSerialPort(data[0], sizeof(data[0]));//测试



return 0;
}

测试方法

pi@raspberrypi:~/sxj731533730 $ g++ a.cpp
pi@raspberrypi:~/sxj731533730 $ ./a.out
open devices sucessful!

length= 10
fd 0 7 1 1 d6 b1 d0 d0 9d
Write() successfully

测试结果

32、树莓派的简单测试串口通信和超声波模块测距_嵌入式硬件_06

第四步、测试超声波模块

32、树莓派的简单测试串口通信和超声波模块测距_#include_07

引脚插线 我用的gpio14 和 15 引脚 ,超声波模块来自我2009年买的锐志开发板带的超声波 哈哈

32、树莓派的简单测试串口通信和超声波模块测距_stm32_08

32、树莓派的简单测试串口通信和超声波模块测距_stm32_09

 

32、树莓派的简单测试串口通信和超声波模块测距_串口_10

 测试代码

import RPi.GPIO as GPIO
import time

Trig_Pin = 14
Echo_Pin = 15
GPIO.setmode(GPIO.BCM)
GPIO.setup(Trig_Pin, GPIO.OUT, initial = GPIO.LOW)
GPIO.setup(Echo_Pin, GPIO.IN)
time.sleep(2)
def checkdist():
GPIO.output(Trig_Pin, GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(Trig_Pin, GPIO.LOW)
while not GPIO.input(Echo_Pin):
pass
t1 = time.time()
while GPIO.input(Echo_Pin):
pass
t2 = time.time()
return (t2-t1) *340/2
try:
while True:
print('Distance:%0.2f m' % checkdist())
time.sleep(3)
except KeyboardInterrupt:
GPIO.cleanup()

测试结果

32、树莓派的简单测试串口通信和超声波模块测距_#include_11

c++ 测距代码  代码需要修改,在while循环那里,判断异常退出

#include <wiringPi.h>
#include <stdio.h>
#include <sys/time.h>
#define Trig 14
#define Echo 15

void chaoShengBoInit(void)
{


pinMode(Echo, INPUT); //设置端口为输入
pinMode(Trig, OUTPUT); //设置端口为输出
}

float disMeasure(void)
{

struct timeval(Trig, LOW);
delayMicroseconds(2);

digitalWrite(Trig, HIGH);
delayMicroseconds(10); //发出超声波脉冲
digitalWrite(Trig, LOW);
gettimeofday(&tv0, NULL); //获取当前时间 开
while(!(digitalRead(Echo) == 1))
{
gettimeofday(&tv3, NULL); //获取当前时间 超时退出
if(tv3.tv_usec-tv0.tv_usec>10)
break;
}
gettimeofday(&tv1, NULL); //获取当前时间 开始接收到返回信号的时候

while(!(digitalRead(Echo) == 0))
{
gettimeofday(&tv3, NULL); //获取当前时间 超时退出
if(tv3.tv_usec-tv1.tv_usec>10)
break;
}

gettimeofday(&tv2, NULL); //获取当前时间 最后接收到返回信号的时候

start = tv1.tv_sec * 1000000 + tv1.tv_usec; //微秒级的时间
stop = tv2.tv_sec * 1000000 + tv2.tv_usec;

dis = (float)(stop - start) / 1000000 * 34000 / 2; //计算时间差求出距离

return dis;
}

int main(void)
{
float dis;
printf("wiringPiSetup\n");
if(wiringPiSetup() == -1){ //如果初始化失败,就输出错误信息 程序初始化时务必进行
printf("setup wiringPi failed !");
return 1;
}

chaoShengBoInit();

while(1){
dis = disMeasure();
printf("distance = %0.2f cm\n",dis);
delay(20);
}

return 0;
}

编译命令和测试

pi@raspberrypi:~ $ g++ -g test.cpp -lwiringPi
pi@raspberrypi:~ $ ./a.out
distance = 0.03 cm
distance = 0.02 cm
distance = 0.03 cm
distance = 0.03 cm
distance = 0.02 cm
distance = 0.03 cm

标签:树莓,32,printf,tty,fd,串口,GPIO,include,options
From: https://blog.51cto.com/u_12504263/5719070

相关文章

  • 9、FFmpeg使用clion+mingw32编译学习y420p,yuv,rgb编码
    基本思想:继续学习ffmpeg基础知识第一步:进行y420p解码,然后将数据转rgb24格式,显示,重点学习了sws_getContext函数,可以通过他进行各种转码cmakelists.txt文件内容cmake_minimum_......
  • 22、R329刷机受阻和测试仿真环境demo
    基本思想:收到了全志的R329开发板,主要是太忙了,没时间搞,今天调休,逐搞一下~(1)参考github说明:  ​​https://github.com/sipeed/R329-Tina-jishu​​系统是ubuntu18.04新建,然......
  • 20、NanoDet训练、测试 以及使用ncnn部署Jetson Nano 进行目标检测和串口数据转发
    基本思想:最近想尝试一下nano上部署nanodet,于是记录一下训练过程,手中有一份labelme标注的数据集,于是开始了一波操作~首先进行划分数据集分为训练集和验证集 31、TensorFlow......
  • USB-RS232转换器芯片GP232RL兼容FT232
    芯片介绍GP232RL是一款高度集成的USB到UART桥接控制器,提供了一种简单的解决方案,可以使用最少的元器件和PCB空间,将RS232接口转换为USB接口。GP232R包括一个USB2.0全速功能......
  • USB转高速串口芯片CH9102
    CH9102是一个USB总线的转接芯片,实现USB转高速异步串口。提供了常用的MODEM联络信号,用于为计算机扩展异步串口,或者将普通的串口设备或者MCU直接升级到USB总线。 特点......
  • P3238 [HNOI2014]道路堵塞
    P3238HNOI2014道路堵塞点击查看代码#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<utility>#include<array>#incl......
  • stm32单片机编程模块
    0keil5库函数配置1、建立工程文件夹,Keil中新建工程,选择型号2、工程文件夹里建立Start、Library、User等文件夹,复制固件库里面的文件到工程文件夹。​ Start文件夹中:......
  • 2022-2023-1 20211326《信息安全专业导论》第五周学习总结
    作业信息信息安全专业导论第四周作业:|无穷的技艺作业||我的黑客偶像|正文链接:https://www.cnblogs.com/TonySSS/教材学习内容总结|看漫画学Python第五章|学习了分支......
  • lib lib32 lib64 libx32
    lib和lib64一样,软件使用64位的指令和64位的指针lib32,软件使用32位的指令和32位的指针,在64位编译器下使用–m32编译输出libx32,软件使用64位的指令但是使用32位的指针长度,......
  • 32nd 2022/8/5 模拟赛总结21
    这次问题所在是卡常,优化以及DP,和组合数嗯,还有如T3样子的DP,一脸懵逼,因为根本看不出来是DP,只觉得与组合数有些联系还有阔别已久的矩阵乘法,再次回归,需要复习,可以将前面的矩......