紧急求助,qnx驱动怎样实现读设备文件和写设备文件内容一致?写函数感觉没问题,读函数求帮忙啊?

1.写函数如下,感觉没什么问题,用cp /etc/services /dev/example或echo hi > /dev/example都成功了,显示出来写的字节数和内容。
写函数如下:


全局指针:char *buf=NULL;
int
io_write (resmgr_context_t *ctp, io_write_t *msg, RESMGR_OCB_T *ocb)
{
int status;
int nb;

if ((status = iofunc_write_verify(ctp, msg, ocb, NULL)) != EOK)
return status;

// No special xtypes
if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE) {
return ENOSYS;
}

/* first process any data already in the receive buff */

// skip the io_write header to get to the data
buf = (char *)(msg+1); // buf = ((char *)msg) + sizeof( *msg );

// calculate number of bytes of client data in receive buffer
nb = ctp->info.msglen - (ctp->offset + sizeof(*msg) );

status = write( STDOUT_FILENO , buf, nb ); // fd 1 is stdout
if( -1 == status )
{
return errno;
}

// Is there more data that didn’t fit into our receive buffer?
if( msg->i.nbytes > nb)
{

int count;
// handle more data all at once – simple way

buf = malloc( msg->i.nbytes -nb );
if (NULL == buf )
return ENOMEM;
count = resmgr_msgread(ctp, buf, msg->i.nbytes-nb, sizeof(*msg)+nb);
count = write(1, buf, count ); // fd 1 is stdout
free(buf);
if( -1 == count )
return errno;
nb += count;


}
_IO_SET_WRITE_NBYTES (ctp, nb);

if (nb > 0)
ocb->attr->flags |= IOFUNC_ATTR_MTIME | IOFUNC_ATTR_CTIME;

return _RESMGR_NPARTS (0);
}

2.读函数有问题,求帮忙,怎么改啊?cat /dev/example时,只能读取一部分都内容,如果是用echo hi >/dev/example时,无反应
int
io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb)
{

int status;
int nb;

if ((status = iofunc_read_verify(ctp, msg, ocb, NULL)) != EOK) {
if (optv) printf(“read failed because of error %d\n”, status );
return status;
}

// No special xtypes
if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE) {
return ENOSYS; // causes MsgError( ctp->rcvid, ENOSYS );
}

//nb = strlen(data );
nb=strlen(buf);

nb = min( nb, msg->i.nbytes );

_IO_SET_READ_NBYTES (ctp, nb); // ctp->status = nb
SETIOV( ctp->iov, buf, nb );
write(1, buf,nb);

if (nb > 0)
ocb->attr->flags |= IOFUNC_ATTR_ATIME;

return _RESMGR_NPARTS (1); // causes MsgReplyv( ctp->rcvid, ctp->status, ctp->iov, 1 );
}

你好,小弟最近在搞Qnx驱动,菜鸟一枚,希望以后能多多交流啊 。。。谢谢 :slight_smile:

Reading anything from here is very good.