gpt4 book ai didi

c - 如何避免阻塞读取功能?

转载 作者:行者123 更新时间:2023-12-02 08:35:43 25 4
gpt4 key购买 nike

我正在尝试从 cygwin 命令中心的 USB 端口写入和读取数据。我已经设法在连接设备时写入和读取数据,但我想要一种方法来检测其他设备是否不存在或无法发回任何数据。我当前的测试代码如下所示(我尝试了很多不同的东西,但似乎没有任何效果)。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include "USB_com.h"
#include "unistd.h"
void main()
{
int fd, value, bytes_read, bytes_written, nbytes, i, j;
char buffR[20];
char buffS[20];
fd = USB_init("/dev/com1");
printf("enter a message (write exit to terminate the session): ");
fgets(buffS, 19, stdin);
while (strncmp("exit", buffS, 4) != 0)
{
bytes_written = write(fd, buffS, 19);
sleep(1);
bytes_read = read(fd, buffR, 19);
printf("string recieved : %s\n", buffR);
memset(buffS, '\0', 19);
printf("enter a message (write exit to terminate the session): ");
fgets(buffS, 19, stdin);
}
USB_cleanup(fd);
}

我的 USB_init.c 用于写入和读取 USB 设备如下所示。

#include "USB_init.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>


/* baudrate settings are defined in <asm/termbits.h>, which is
* included by <termios.h> */
#ifndef BAUDRATE
#define BAUDRATE B9600
#endif

#define _POSIX_SOURCE 1 /* POSIX compliant source */

static int fd, c, res;
static struct termios oldtio, newtio;
static char *device;

int USB_init(char *modemdevice)
{
/*
* Open modem device for reading and writing and not as controlling tty
* because we don't want to get killed if linenoise sends CTRL-C.
**/
device = modemdevice;
//fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY);
fd = open (device, O_RDWR | O_NOCTTY );
if (fd < 0)
{
perror (device);
exit(-1);
}

tcgetattr (fd, &oldtio); /* save current serial port settings */
bzero (&newtio, sizeof (newtio)); /* clear struct for new port settings */

/*
*BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
*CRTSCTS : output hardware flow control (only used if the cable has
*all necessary lines. )
*CS8 : 8n1 (8bit,no parity,1 stopbit)
*CLOCAL : local connection, no modem contol
*CREAD : enable receiving characters
**/
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;

/*
*IGNPAR : ignore bytes with parity errors
*ICRNL : map CR to NL (otherwise a CR input on the other computer
* will not terminate input)
* otherwise make device raw (no other input processing)
**/
newtio.c_iflag = IGNPAR | ICRNL;

/*
* Map NL to CR NL in output.
* */
#if 0
newtio.c_oflag = ONLCR;
#else
newtio.c_oflag = 0;
#endif


/*
* ICANON : enable canonical input
* disable all echo functionality, and don't send signals to calling program
**/
#if 1
newtio.c_lflag = ICANON;
#else
newtio.c_lflag = 0;
#endif

/*
* initialize all control characters
* default values can be found in /usr/include/termios.h, and are given
* in the comments, but we don't need them here
* */
newtio.c_cc[VINTR] = 0; /* Ctrl-c */
newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
newtio.c_cc[VERASE] = 0; /* del */
newtio.c_cc[VKILL] = 0; /* @ */
newtio.c_cc[VEOF] = 4; /* Ctrl-d */
newtio.c_cc[VTIME] = 0; /* inter-character timer unused*/
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives*/
newtio.c_cc[VSWTC] = 0; /* '\0' */
newtio.c_cc[VSTART] = 0; /* Ctrl-q */
newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
newtio.c_cc[VEOL] = 0; /* '\0' */
newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
newtio.c_cc[VEOL2] = 0; /* '\0' */

/*
* now clean the modem line and activate the settings for the port
**/
tcflush (fd, TCIFLUSH);
tcsetattr (fd, TCSANOW, &newtio);

/*
* terminal settings done, return file descriptor
**/

return fd;
}

void USB_cleanup(int ifd){
if(ifd != fd) {
fprintf(stderr, "WARNING! file descriptor != the one returned by serial_init()\n");
}
/* restore the old port settings */
tcsetattr (ifd, TCSANOW, &oldtio);
}

有人能告诉我如何做类似 read(fd,buffR,19) 的事情,但如果我没有收到任何数据并在一段时间后中止它并打印类似printf("没有接触到设备")?

我非常感谢有关如何解决此问题的任何建议!

最佳答案

在处理连续剧时,您有两个选择:使用带超时的select/poll,或使用termios 配置端口。

对于selectpoll,方法是先等待描述符上的事件,然后 读取成功结果。这种方法的优点是它也适用于网络套接字和其他一些描述符类型:

int fd = ...
fd_set fds;
stuct timeval timeout;

timeout.tv_sec = 10; /* timeout in secs */
timeout.tv_usec = 0;
FD_ZERO(&fds)
FD_SET(fd, &fds)
if (select(fd, fds, NULL, NULL, &timeout) > 0)
read(...)
else ... timeout

poll 示例与上面非常相似。

但是,在使用任何类型的串行设备时,还有另一种方法可以使读取正确超时。您需要使用 VMINVTIME 属性:

newtio.c_cc[VTIME] = timeout; /* timeout in 1/10 of second  1==100ms, 10==1sec*/
newtio.c_cc[VMIN] = 0;

就是这样。

关于c - 如何避免阻塞读取功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21800118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com