martinling / libserialport

Unofficial personal repository for libserialport - see git://sigrok.org/libserialport for official repo
http://sigrok.org/wiki/Libserialport
GNU Lesser General Public License v3.0
65 stars 34 forks source link

Question about sp_wait #27

Closed JomerDev closed 6 years ago

JomerDev commented 6 years ago

The way I understand the sp_wait function is that it returns indication as to for which port an event occured? Is this correct? If so, is there any way to 'select' multiple ports and wait for any of them to become readable without having to check every port or input when sp_wait returns?

martinling commented 6 years ago

Hi Nathan, sorry I didn't see this when you posted it.

Unfortunately there's no way within the libserialport API to avoid having to check every event that was being waited on when sp_wait returns.

Usually it's quick enough to just check everything when a wait returns, and it's often good practice anyway as there can be various OS/driver issues that lead to spurious wakeups or misreported event sets.

If you really want to avoid checking everything, then you can call the OS's waiting APIs directly, i.e. poll or select on Unix-like OSes and WaitForMultipleObjects on Windows, both of which have mechanisms to identify which events occured. To use these APIs you would need to use sp_get_port_handle to get the OS handle of a port that was opened with libserialport.

Obviously this compromises the cross-platform advantage of using libserialport, but it's not uncommon to need to do this anyway to integrate serial events into existing event loops.