Beaglebone Black 串口的操作(二)
来源:互联网 发布:知乎 斯坦纳树 编辑:程序博客网 时间:2024/06/07 09:13
本博客已逐步移至我的个人网站:www.zj-fighting.cn,欢迎访问。
之前我写了一篇文章《Beaglebone Black 串口的操作(一)(更新)》,一直没有接着写。现在把之前没有做的事情做完。
上一次是直接通过终端用shell完成对串口的操作,这一次,我尝试用C语言来完成。主要参考了Advanced Programming in the Unix Environment和 Bad to Bone
。
首先在Ubuntu中编辑好源文件uart.c:
#include <stdio.h>#include <string.h>#include <termios.h>#include <unistd.h>#include <fcntl.h>intmain(void){ //define file handle for uart2 FILE *ofp_uart2_tx, *ofp_uart2_rx; //uart2 configuration struct struct termios uart2; int fd; char message[1024]; //open uart2 for tx/rx, not controlling device if( (fd=open("/dev/ttyO2", O_RDWR | O_NOCTTY)) < 0 ) printf("Unable to access uart2.\n"); //get attributes of uart2 if( tcgetattr(fd, &uart2) < 0 ) printf("Failed to get attributes of uart2.\n"); //set Baud rate if( cfsetospeed(&uart2, B9600) < 0) printf("Failed to set baud rate.\n"); else printf("Baud rate: 9600\n"); //set attributes of uart2 uart2.c_iflag = 0; uart2.c_oflag = 0; uart2.c_lflag = 0; tcsetattr(fd, TCSANOW, &uart2); //write messages to ttyO2 scanf("%s", message); while( strcmp(message, "end")!= 0) { write(fd, message, strlen(message)+1); scanf("%s", message); } close(fd); return 0; }
之后将源文件复制到tftp的根目录下:
cp uart.c /srv/tftp/
然后登陆到BBB中,键入命令
tftp 192.168.7.1 -g -r uart.c
上面的命令用于把Ubuntu的源文件下载到BBB上。
之后编译源文件,得到a.out
在执行之前,别忘了要重载uart2
echo BB-UART2 > $SLOTS
然后就可以执行a.out了,如下:
0 0
- Beaglebone Black 串口的操作(二)
- Beaglebone Black 串口的操作(一)(更新)
- 使用Beaglebone Black的串口UART
- 使用Beaglebone Black的串口UART
- 使用Beaglebone Black的串口UART
- Beaglebone Black基本操作(Debian)
- BeagleBone Black (ubuntu)(u-boot)使用串口
- 使用Beaglebone Black的I2C(一)
- 使用Beaglebone Black的PRU(一)
- Beaglebone Black的启动
- 使用SSH操作Beaglebone Black
- 使用Beaglebone Black的PRU(二)——Hello World!
- 使用Beaglebone Black的I2C (二)——使用C语言和i2c-dev驱动
- Beaglebone Black(6.1)Beaglebone Black的c++开发环境Eclipse构建
- Beaglebone Black上使用Codesys开发EtherCAT(二)
- beaglebone black 释放ttyO1作为通信串口(关闭调试串口)
- BeagleBone Black教程之BeagleBone Black设备的连接
- Beaglebone Black教程Beaglebone Black的引脚分配
- JVM内存状况及分析工具
- 恭喜您成为CSDN博客专家
- UTF-8 和 UTF-8 without BOM
- Struts2文件和图片上传
- sdfasdegnmgh
- Beaglebone Black 串口的操作(二)
- IOS应用开发-App提交审核
- Android 定时器的3种方法
- Google Maps SDK for iOS使用初探(一)
- Hibernate3.3笔记
- 数控机床中的半闭环控制系统与闭环控制系统在结构上主要区别
- 在codecademy学习的第一个index.html
- Android Activity.startActivityForResult的详细用法
- GridView 对列进行排序