将指针文件描述符转换为数字文件描述符而不关闭文件

时间:2019-04-13 22:50:26

标签: apache file pointers serial-port termios

我正在编写一个与串行接口对接的apache模块。 目前,我可以使用以下类型的代码对其进行读写:

char* dp=mydata; //mydata is declared elsewhere and contains output
apr_file_t* fd;
apr_pool_t* apool;
apr_status_t ps=apr_pool_create_ex(&apool,NULL,NULL,NULL);
apr_file_open(&fd,port,APR_FOPEN_READ | APR_FOPEN_BINARY, APR_OS_DEFAULT, apool);
apr_size_t sz=mydatasz; //mydatasz is equal to # of bytes to output
apr_status_t fsr=apr_file_write(fd,dp,&sz);
apr_status_t fsc=apr_file_close(fd);

我发现这也适用于串行设备(/ dev / ttyS0)。我现在想做的是能够在程序中设置串行选项。我试图创建一个原始的串行IO模式,速率为9600bps,没有流控制,并包含以下片段:

struct termios tmine;
apr_file_t* fd;
apr_pool_t* apool;
apr_status_t ps=apr_pool_create_ex(&apool,NULL,NULL,NULL);
apr_status_t fs=apr_file_open(&fd,port,APR_FOPEN_WRITE | APR_FOPEN_READ | APR_FOPEN_BINARY, APR_OS_DEFAULT, apool);
cfmakeraw(&tmine);
cfsetispeed(&tmine,B9600);
cfsetospeed(&tmine,B9600);
tmine.c_cflag |= (CLOCAL|CREAD);
tmine.c_cflag &= ~CSTOPB;
tmine.c_cflag &= ~CRTSCTS;
tmine.c_cc[VMIN]=0;c_cc[TIME]=10;
tcsetattr(&fd,TCSANOW,&tmine); //ERROR
apr_status_t fsc=apr_file_close(fd);

现在,我已经包含了适当的标题。但是我遇到的问题是:

utils.c:64: warning: passing argument 1 of ‘tcgetattr’ makes
            integer from pointer without a cast               
utils.c:71: error: ‘c_cc’ undeclared (first use in this function)

我知道发生第一个问题是因为文件描述符的类型是apr_file_t *,但是termios系列函数希望使用整数,而不是apr_file_t *类型。

是否可以将apr_file_t *类型的文件转换为等效的整数,以便可以在程序中使用termios设置? (就像是否存在我可以使用的功能可以为我做到这一点?)

0 个答案:

没有答案