linux等待串口写完,在讀取之前獲取等待串口的字節數,linux

I am converting a Win32 serial class to Linux (Ubuntu) one of the required functions of this serial class is to "peek" at the serial buffer to see how many bytes are waiting on the serial port before reading the serial port.

我正在將Win32串行類轉換為Linux(Ubuntu)這個串行類所需的功能之一是“查看”串行緩沖區,以查看在讀取串行端口之前在串行端口上等待了多少字節。

In this pedicure situation I only need to know if there are ANY bytes on the port before attempting to read it.

在這種修腳情況下,我只需要知道端口上是否有任何字節,然后再嘗試讀取它。

In windows I used COMSTATS but I can't seem to find a similar function in Linux.

在Windows中我使用了COMSTATS,但我似乎無法在Linux中找到類似的功能。

My question is:

我的問題是:

On Linux How do you read the amount of BYTES/chars waiting on a serial port without removing them from the serial port buffer?

在Linux上如何讀取等待串口的BYTES / chars數量而不將其從串口緩沖區中刪除?

2 个解决方案

#1

11

You need to use an ioctl

你需要使用ioctl

ioctl(serial_fd, FIONREAD, &bytes_avail);

This document is very much worth reading, for that and many other issues (canonical vs raw mode, etc)

對於該文檔和許多其他問題(規范與原始模式等),本文檔非常值得一讀。

http://www.cmrr.umn.edu/~strupp/serial.html

#2

2

In C language you can ask this with an ioctl :

在C語言中,您可以使用ioctl來詢問:

int bytes_avaiable;

ioctl(serial_file_descriptor, FIONREAD, &bytes_available);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值