首页 > 其他 > 详细

Beaglebone Black 串口的操作(二)

时间:2014-02-07 22:38:41      阅读:1147      评论:0      收藏:0      [点我收藏+]

之前我写了一篇文章《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>

int
main(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了,如下:

bubuko.com,布布扣

Beaglebone Black 串口的操作(二)

原文:http://blog.csdn.net/zj651927693/article/details/18965031

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!