Aeroflex gaisler(带有leon2处理器的RTEMS)无法通过UART接口发送字符

时间:2013-06-07 09:37:21

标签: c string pointers serial-port embedded

#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ioctl.h>
#include <apbuart.h>
#include <rtems.h>
#include <drvmgr/drvmgr.h>
#include <unistd.h>
#define size 1024
#define APBUART_databits 8

int writetoport(int fd, char *b, size_t count)
{
    if(!write(fd, &b[0], sizeof(count)))
        {
        //printLastError();
        return -1;
        }
        return 1;
}

int main()
{
        char a[size] = {'h','e','l','l','o'};
        int fd;
        fd = open("/dev/apbuart2", O_WRONLY );
        ioctl(fd, APBUART_SET_BAUDRATE, 9600);
        ioctl(fd, APBUART_START, NULL);
        ioctl(fd, APBUART_STOP, 1);
        ioctl(fd, APBUART_databits, 8);
        writetoport(fd, &a[size], sizeof(a));
        return 0;
}

我正在研究Aeroflex Gaisler(带有leon2处理器的RTEMS)。尝试通过UART接口发送字符。但是我无法发送角色。下面是代码行。

#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ioctl.h>
#include <apbuart.h>
#include <rtems.h>
#include <drvmgr/drvmgr.h>
#include <unistd.h>
#define size 1024
#define APBUART_databits 8

int writetoport(int fd, char *b, size_t count)
{
    if(!write(fd, &b[0], sizeof(count)))
        {
        //printLastError();
        return -1;    
        }
    return 1;
}

int main()
{
        char a[size] = {'h','e','l','l','o'};
        int fd;
        fd = open("/dev/apbuart2", O_WRONLY );
        ioctl(fd, APBUART_SET_BAUDRATE, 9600);
        ioctl(fd, APBUART_START, NULL);
        ioctl(fd, APBUART_STOP, 1);
        ioctl(fd, APBUART_databits, 8);

        writetoport(fd, &a[size], sizeof(a));
        return 0;    
} 

我根据回复更改了我的代码,然后它也无效。

3 个答案:

答案 0 :(得分:1)

您的代码看起来是递归的,我所期望的是缓冲区溢出。

write()里面的代码也很奇怪,本地参数的地址不是很有趣,而且重新计算的大小只是持有调用者大小的本地参数的大小,可以没错。

如果这个系统运行内核并且看起来像一个类似POSIX的库,我不确定你为什么要重新实现write()

答案 1 :(得分:0)

根据您的函数签名判断,您似乎是在实际的write 函数之后。

如果您正在尝试使用自己调用write的自定义函数,请不要将其称为write,否则它将保持递归调用,直到您获得堆栈溢出。

将其称为write_to_portwrite_to_port调用write,如果调用不成功,请添加一些错误处理(如您目前所做)。

可能是这样的:

int write_to_port(int fd, const void *buf, size_t count)
{
    if (write(fd, buf, count) == -1)
    {
        /* add some error handling */
        return -1;
    }

    return 1; /* all good */
}

此外,要使用write,您需要添加<unistd.h>标题。

答案 2 :(得分:0)

在您对write_to_port的调用中,您传递的是最后一段数据后面的位置地址,而不是第一段的位置。不要把尺寸放在括号里。

您似乎仍然存在放松所提出的问题,错误地使用'sizeof(count)'而不是'count'本身。