只是一个简单的问题,我正在做一个原始的套接字编程:
fd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
并使用以下方法拉取发送缓冲区:
bytes = sendto(fd, &buffer, send_size, 0,(struct sockaddr*)&socket_addr, addr_size);
ioctl(fd, TIOCOUTQ, &bytes);
while(bytes>0)
ioctl(fd, TIOCOUTQ, &bytes);
如果bytes返回0并退出while循环,是否保证数据发送出设备?如果不是,我该如何实际检查数据是否已从设备发送出去?
最佳答案
这是我在使用 RS-485 收发器时用来驱动 TX 使能线以确保所有位都已输出的内容:
void WaitTxDone(unsigned int messageSize)
{
//wait for tty TX queue to become empty
int queued = 1;
while (queued > 0)
ioctl(m_port->getPortHandle(), TIOCOUTQ, &queued);
//number of nanoseconds it takes to transmit messageSize bytes (10 is 8 bits + start + stop)
unsigned int txTime = ((float)(messageSize * 10) / m_baudrate) * 1000000000;
//assuming it won't take longer than one second
struct timespec delay = {0, txTime};
nanosleep(&delay, NULL);
}
关于c - 使用 ioctl TIOCOUTQ 进行套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46662953/