#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;
}我正在从事俄罗斯航空( leon2处理器的实时内存)的工作。尝试通过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;
} 我改变了我的代码的基础上回复,然后它也不工作。
发布于 2013-06-07 17:40:40
你的代码看起来是递归的,我所期望的就是一个缓冲区溢出。
你的write()中的代码也很奇怪,本地参数的地址不是很有趣,大小被重新计算为保存调用者大小的本地参数的大小,这是不正确的。
如果这个系统运行一个内核,并且像它看起来那样有一个类似POSIX的库,那么我不确定为什么要重新实现write()。
发布于 2013-06-07 17:57:39
从您的函数签名判断,您似乎是在使用实际的write 函数。
如果您想让自己的自定义函数调用write,请不要调用write,否则它将不断递归地调用自身,直到堆栈溢出。
例如,将其命名为write_to_port,让write_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-07 20:00:23
在对write_to_port的调用中,传递的是最后一段数据之后的位置地址,而不是第一段数据的位置。不要把大小放在括号里。
你似乎仍然有unwind提到的问题,错误地使用'sizeof(count)‘而不是'count’本身。
https://stackoverflow.com/questions/16980886
复制相似问题