1
#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;    
} 

我根据回复更改了我的代码,然后它也不起作用。

4

3 回答 3

1

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

你里面的代码write()也很奇怪,本地参数的地址不是很有趣,并且大小被重新计算为保持调用者大小的本地参数的大小,这是不对的。

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

于 2013-06-07T09:40:39.553 回答
0

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

您似乎还有 unwind 提到的问题,即错误地使用 'sizeof(count)' 而不是 'count' 本身。

于 2013-06-07T12:00:23.123 回答
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>标题。

于 2013-06-07T09:57:39.130 回答