openhab / openhab-core

Core framework of openHAB
https://www.openhab.org/
Eclipse Public License 2.0
897 stars 415 forks source link

Unified support for hardware vs virtual (e. g. over IP) serial #1912

Open Sonic-Amiga opened 3 years ago

Sonic-Amiga commented 3 years ago

I am currently writing two bindings for different hardware pieces, both using serial line. I am looking at SonyProjector as a reference, and i see it implements multiple types of Bridge: serial, serial over TCP, serial over UDP, etc. But this applies not only to Sony projectors, but also to any other serial devices. It's possible to order IP-to-serial bridge as a discrete component and use it with any device you want. Very useful in many practical situations. Currently bindings have to code such a support themselves, and it's not good. What if openhab's core could provide some abstract service for handling both physical and virtual serial ports ?

lolodomo commented 3 years ago

Your example with the Sony Projector binding is not the best choice as the protocol is different for serial and IP. The core framework already provide an abstraction for serial and serial over IP but in practice it does not work well with certain bindings when you use serial over IP with the serial stuff. That is the reason why developers create a special case for serial over IP.

Sonic-Amiga commented 3 years ago

Sony is in fact even more complex. Some projectors feature a native Ethernet port, and for them communication protocol is different from those which have RS232 connector. But you could also route that RS232 via a 3rd party TCP bridge, and in this case the protocol would be the same, the binding supports such a configuration too. Well, offtopic :) Could you tell me about the abstraction API we already have ? Perhaps i'll find them convenient to use.

lolodomo commented 3 years ago

I am the author of the Sony Projector binding so yes I know it well lol

The serial abstraction is just the serial stuff from the core framework we use in bindings. If you want to use serial over IP, you just have to use a special serial port name starting with RFCxxxx. Sorry I don't remember the number. That is the theory. In practice, I was not able to make it work when developing one binding. But I did not try again recently.

wborn commented 3 years ago

What if openhab's core could provide some abstract service for handling both physical and virtual serial ports ?

The core already supports serial over IP using RFC2217. To use such a port you configure the hostname/port of the server using a URL like rfc2217://192.168.1.1:1234 instead of the serial port name (/dev/ttyUSB0 or COM1) in your Thing configuration.

You can for instance run a cheap RFC2217 server using a Raspberry Pi and ser2net. See for instance this community topic on how to configure ser2net.

Sometimes a binding needs to be slightly adapted before it supports RFC2217 connections, see: