I was not really satisfied with the above solution and the TCP binding is unstable. So I did a totally other appraoch. I already have some MQTT items and why not let the projector chat with openHAB via MQTT?
My solution is a small python script that does the work. Just in case it would be handy for someone:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "Jürgen Gluch"
"""
Copyright 2018 Jürgen Gluch
Script: beamer_tcp_mqtt.py
Author: Jürgen Gluch
Purpose: Communicate with Projector RS232 over the TCP-RS232 bridge with MQTT messages
"""
import socket
import paho.mqtt.client as mqtt
# define the TCP conection to the beamer
TCP_IP = '192.168.XX.XX'
TCP_PORT = 23
BUFFER_SIZE = 128
# define the MQTT connection for communication with openHAB
broker = "192.168.XX.XX"
username = "mqtt"
password="XXXXXX"
# send data to beamer
def on_message_tobeamer(mosq, obj, msg):
mqttmsg = str(msg.payload.decode("utf-8"))
print("to beamer: " + mqttmsg )
msgtcp = bytes(mqttmsg+"\r", 'utf-8')
s.send(msgtcp)
### Main programm
if __name__ == '__main__':
try:
# establish TCP connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
# Add message callbacks that will only trigger on a specific subscription match.
client = mqtt.Client()
client.message_callback_add("groundfloor/living/projector/send", on_message_tobeamer)
# Connect to MQTT and wait for messages
client.on_message = on_message_mqtt
client.username_pw_set(username, password)
client.connect(broker, 1883, 60)
client.subscribe("groundfloor/living/projector/send", 0)
client.loop_start()
# infinite loop ...
print("Wait for signals:")
while True:
# receive data from beamer
data = str( s.recv(BUFFER_SIZE) )
# cut the "b'" at front and "\r" at end
data = data[2:len(data)-3]
print("Received data from beamer: " + data)
client.publish("groundfloor/living/projector/get", data)
except KeyboardInterrupt:
print("Stopped by user")
except:
print("Exit by error")
finally:
s.close()
print("The End.")