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
原创粉丝点击