Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to connect with virtual comport #4

Open
pkoevesdi opened this issue Jan 8, 2023 · 38 comments
Open

Failed to connect with virtual comport #4

pkoevesdi opened this issue Jan 8, 2023 · 38 comments

Comments

@pkoevesdi
Copy link

I use socat pty,link=/dev/virtualcom0,raw tcp:beamerctl:23 on my home assistant installation to make a tcp connection to a tcp2serial converter (hostname "beamerctl") on my Benq W1100.
Everything works, I can send commands with echo -e "\r*pow=?#\r" > /dev/virtualcom0 and receive answers from projector with cat /dev/virtualcom0 in a different terminal.
But when I try to give /dev/virtualcom0 into the device settings of the device assistant of homeassistant-benqprojector, I always get "Failed to connect" or "Unexpected error" (for instance when trying /dev/virtualcom0/ instead of /dev/virtualcom0).
What am I doing wrong?
What is the right way of giving a (pty) serial port?
What would be the right baud rate to choose, if it is a pty serial port?

@rrooggiieerr
Copy link
Owner

I have no experience with tcp2serial, maybe you can try enabling logging and get more details of the error?

Add these lines to your configuration.yaml to enable logging:

logger:
  # default: info
  logs:
    custom_components.benqprojector: debug
    benqprojector: debug

@pkoevesdi
Copy link
Author

Ok, thanks.
When I put an non-existing serial port /dev/virtualcom1 into the webform, the webgui says "Unexpected error" and I see this in the log:

Jan 08 18:59:28 homeserver Home Assistant[6285]: 2023-01-08 18:59:28.346 ERROR (MainThread) [custom_components.benqprojector.config_flow] Unexpected exception: Device /dev/virtualcom1 does not exists
Jan 08 18:59:28 homeserver Home Assistant[6285]: Traceback (most recent call last):
Jan 08 18:59:28 homeserver Home Assistant[6285]:   File "/mnt/dietpi_userdata/homeassistant/custom_components/benqprojector/config_flow.py", line 65, in async_step_setup_serial
Jan 08 18:59:28 homeserver Home Assistant[6285]:     info = await self.validate_input_setup_serial(user_input, errors)
Jan 08 18:59:28 homeserver Home Assistant[6285]:   File "/mnt/dietpi_userdata/homeassistant/custom_components/benqprojector/config_flow.py", line 105, in validate_input_setup_serial
Jan 08 18:59:28 homeserver Home Assistant[6285]:     raise vol.error.PathInvalid(f"Device {serial_port} does not exists")
Jan 08 18:59:28 homeserver Home Assistant[6285]: voluptuous.error.PathInvalid: Device /dev/virtualcom1 does not exists

Totally fine, no unexpected thing with an non-existing port.

But, if I put in my up-n-running /dev/virtualcom0 in the webform, the webgui says "Failed to connect" and I see exactly nothing happening in the log.

@pkoevesdi
Copy link
Author

pkoevesdi commented Jan 8, 2023

Btw.: the webgui also doesn't show me any existing serial ports, not even the activated hardware ports:
grafik
I am on a RockPi. I didn't use it's internal serial ports yet, but I switched them on and consider them available. The component doesn't recognize them, neither automatically as a choice, nor manually, i.e. /dev/ttyS2

@rrooggiieerr
Copy link
Owner

Are you using the Home Assistant Operating System?

@pkoevesdi
Copy link
Author

No, it is a debian and an unsupervised home assistant installation.
Does this project use this: https://github.com/rrooggiieerr/benqprojector.py ?
Maybe I could go more into the latter one to cook the error down?

@rrooggiieerr
Copy link
Owner

Yes it does, but the serial port detection is done in the HA integration, not in the BenQ library. If you're a bit familiar with Python you could have a look at the config_flow.py on line 42 I get the list of serial ports.

The Baud rate you should be able to find in your projector configuration, but I don't know if that's a thing if you're using a virtual com port.

What hardware do you use for the serial port at the projector side, some ESP based device?

Would be great if you can make it work an I could add your method to the Readme

@rrooggiieerr
Copy link
Owner

On line 104 of config_flow.py the code is checking the existence of the serial port. To me the code looks good, it's just checking the existence of a file name. Are the permissions to the virtual serial port right? read and write permissions for the user running the HA process?

@pkoevesdi
Copy link
Author

Yes it does, but the serial port detection is done in the HA integration, not in the BenQ library. If you're a bit familiar with Python you could have a look at the config_flow.py on line 42 I get the list of serial ports.

Ok, I'll try it next weekend.
The permissions of the port I already checked an played around with them. I have no problem on the command line as the home assistant user using the serial port.

The Baud rate you should be able to find in your projector configuration, but I don't know if that's a thing if you're using a virtual com port.

Nope, actually not, this is only relevant on the other end of the TCP connection.

What hardware do you use for the serial port at the projector side, some ESP based device?

Yes, I welded in an ESP8266 into the projector (TX, RX, GND and 3.3 V are taken direktly from the main board). So, the projector provides an WiFi interface now, interfacing with the serial port of the projector. On the ESP I use ESPlink-Firmware.

@rrooggiieerr
Copy link
Owner

I've got a couple of ESPs laying around, so might play a bit with that too if I have time

@pkoevesdi
Copy link
Author

Btw. I already use it inside home assistant for 6 months with a shell command launched from home assistant. From my configuration.yaml:

switch:
  - platform: command_line
    switches:
      videobeamer:
        friendly_name: Videobeamer
        command_on: echo -e "\r*pow=on#\r" | nc beamerctl 23
        command_off: echo -e "\r*pow=off#\r" | nc beamerctl 23
        unique_id: videobeamer

@rrooggiieerr
Copy link
Owner

That's great. This integration is going to give you a bunch more options and also keeps up with the real status of your projector so you can use the remote. So I think if you can make it work you're going to have a better integration.

According to the BenQ documentation the projector uses port 8000 to control network enabled projectors. Somewhere in the future networked projectors might be supported by the integration although I don't have such projector. If you would use port 8000 on your ESP this would follow the BenQ standard and a future integration might work without the virtual com ports

@pkoevesdi
Copy link
Author

That's great. This integration is going to give you a bunch more options and also keeps up with the real status of your projector so you can use the remote. So I think if you can make it work you're going to have a better integration.

Exactly, that's why I wanted to switch to Your integration and was happy to find it.

According to the BenQ documentation the projector uses port 8000 to control network enabled projectors. Somewhere in the future networked projectors might be supported by the integration although I don't have such projector. If you would use port 8000 on your ESP this would follow the BenQ standard and a future integration might work without the virtual com ports

But I think the projectors who use network protocol directly have a different command language anyway?

@rrooggiieerr
Copy link
Owner

But I think the projectors who use network protocol directly have a different command language anyway?

To my understanding network enabled BenQ projectors support both their proprietary command set (which this integration uses) on port 8000 and the PJLink protocol on port 4352

@rrooggiieerr
Copy link
Owner

rrooggiieerr commented Jan 9, 2023

Do you have a picture of your ESP installation? I'm quite interested on applying this myself

@pkoevesdi
Copy link
Author

pkoevesdi commented Jan 9, 2023

As a matter of fact, I didn't make photos. I'll make some, in case I have to reopen the device again. Do You also have an W1100?
As far as I remeber, I used the Pins 5, 7, 19, 20 (GND, VCC, RX, TX) of the chip R5F21336CNFP on the main board, data sheet is here: https://www.renesas.com/us/en/document/dst/r8c33c-group-datasheet?r=1053681
I directly connected them to the ESP's VCC, GND, TX, RX.
You find the chip on the top side of the main board, only need to open the housing of the W1100. For GND and VCC go somewhere easier than on the chip directly, find some easier spots to weld on the PCB with a multimeter. For RX and TX I saw no other choice than to weld directly on the chip, which is very tiny and You'll need a microscope and a VERY thin welding iron.

@pkoevesdi
Copy link
Author

As a matter of fact, I didn't make photos. I'll make some, in case I have to reopen the device again. Do You also have an W1100?
As far as I remeber, I used the Pins 5, 7, 19, 20 (VCC, GND, RX, TX) of the chip R5F21336CNFP on the main board, data sheet is here: https://www.renesas.com/us/en/document/dst/r8c33c-group-datasheet?r=1053681
I directly connected them to the ESP's VCC, GND, TX, RX.
You find the chip on the top side of the main board, only need to open the housing of the W1100. For GND and VCC go somewhere easier than on the chip directly, find some easier spots to weld on the PCB with a multimeter. For RX and TX I saw no other choice than to weld directly on the chip, which is very tiny and You'll need a microscope and a VERY thin welding iron.

@rrooggiieerr
Copy link
Owner

No, I've got the W1110, but it's always good to get some inspiration. But if you have to solder straight to the chip I don't know if I find that worth the risk since I got it now working over serial. I did order a rs232 to TTL converter on Ali, so when I get that I can build an ESP to serial ESPLink device

@pkoevesdi
Copy link
Author

In the meantime I have got a schematic of the W1100 (at that time I didn't have one), and I think, it's possible to find other pads to get to TX and RX signal than directly on the tiny chip pads.

@pkoevesdi
Copy link
Author

Ok, so I'm looking into it. Since I am no developer, nor have a development environment, I just tried
python3 -m serial.tools.list_ports on the CLI of my home assistant installation. It throws no ports found. Could You check in Your environment, if this command works and gives out the serial ports, and if they are the same as in the gui setup of the integration?

@rrooggiieerr
Copy link
Owner

Hi! Great debugging command, I didn't think if that. For me I do get serial ports listed

@pkoevesdi
Copy link
Author

Ok, thank You. So, that's a giant leap towards narrowing down the error.

@rrooggiieerr
Copy link
Owner

Yes, good work!

@rrooggiieerr
Copy link
Owner

What command/tool/instruction are you using to create the virtual serial port on you server?

@pkoevesdi
Copy link
Author

See first posting, first line.

@pkoevesdi
Copy link
Author

Ich managed now to get a external USB2serial device listed, as well on the gui of the integration setup, as on the CLI with python3 -m serial.tools.list_ports

@rrooggiieerr
Copy link
Owner

I mentioned line 104 of config_flow.py before where the code is checking the existence of the serial port. It's not using any serial commands there, just a filesystem command to see if the path exist. So it might be interesting to see if you can reproduce that test

@pkoevesdi
Copy link
Author

pkoevesdi commented Jan 14, 2023

on a python console, I do:

>>> os.path.exists('/dev/virtualcom0')
True

So (of course, somehow) the path exists, also for python. Already that I can pipe my echo output into it (see first comment) shows, that it exists. What can it make it "not exist" inside the setup routine?

@rrooggiieerr
Copy link
Owner

You could also try to use the BenQ library directly without HA
https://github.com/rrooggiieerr/benqprojector.py

pip3 install benqprojector

After installing use the CLI to connect
python3 -m benqprojector /dev/virtualcom0 <baud> status

For <baud> you probably can use anything since it's a virtual port

@pkoevesdi
Copy link
Author

This works somehow: only the model number is read out correctly. But it's a proof, that the connection works.

Unexpected response: b''
Unexpected response: b''
Command macaddr=? not supported
Device /dev/virtualcom0 available
Model: w1100
Position: None
Power on
Direct power on  : None
Lamp time        : None hours
3D               : None
Picture mode     : None
Aspect ratio     : None
Brilliant color  : None
Blank            : None
Brightness       : None
Color value      : None
Contrast         : None
Color temperature: None
High altitude    : None
Lamp mode        : None
Quick auto search: None
Sharpness        : None
Video Source     : None
Volume           : None
Muted            : None
Supported video sources: ['dp', 'dvia', 'dvid', 'hdbaset', 'hdmi', 'hdmi2', 'network', 'rgb', 'rgb2', 'rgb3', 'svid', 'usbdisplay', 'usbreader', 'vid', 'wireless', 'ypbr', 'ypbr2', 'smartsystem']

@rrooggiieerr
Copy link
Owner

rrooggiieerr commented Jan 14, 2023

Excellent! You can add --debugto the command to get more details

@pkoevesdi
Copy link
Author

2023-01-14 14:49:54,219 ERROR    Unexpected response: b''
2023-01-14 14:49:54,270 ERROR    Unexpected response: b''
2023-01-14 14:49:54,271 DEBUG    command *modelname=?#
2023-01-14 14:49:54,368 DEBUG    Response: >
2023-01-14 14:49:54,369 DEBUG    Response is command prompt >
2023-01-14 14:49:54,420 DEBUG    Response: 
2023-01-14 14:49:54,421 DEBUG    Empty line
2023-01-14 14:49:54,435 DEBUG    Response: *modelname=?#
2023-01-14 14:49:54,435 DEBUG    Command successfully send
2023-01-14 14:49:54,436 DEBUG    Response: 
2023-01-14 14:49:54,436 DEBUG    Empty line
2023-01-14 14:49:54,488 DEBUG    Response: *modelname=W1100#
2023-01-14 14:49:54,488 DEBUG    LC Response: *modelname=w1100#
2023-01-14 14:49:54,488 DEBUG    Raw response: '*modelname=w1100#'
2023-01-14 14:49:54,489 DEBUG    Processed response: w1100
2023-01-14 14:49:54,489 ERROR    Command macaddr=? not supported
2023-01-14 14:49:54,489 INFO     Device /dev/virtualcom0 available
2023-01-14 14:49:54,540 ERROR    Unexpected response: b''
2023-01-14 14:49:54,558 ERROR    Unexpected response: b'>'
2023-01-14 14:49:54,558 DEBUG    command *pow=?#
2023-01-14 14:49:54,609 DEBUG    Response: 
2023-01-14 14:49:54,609 DEBUG    Empty line
2023-01-14 14:49:54,655 DEBUG    Response: *pow=?#
2023-01-14 14:49:54,655 DEBUG    Command successfully send
2023-01-14 14:49:54,656 DEBUG    Response: 
2023-01-14 14:49:54,656 DEBUG    Empty line
2023-01-14 14:49:54,708 DEBUG    Response: *pow=on#
2023-01-14 14:49:54,708 DEBUG    LC Response: *pow=on#
2023-01-14 14:49:54,709 DEBUG    Raw response: '*pow=on#'
2023-01-14 14:49:54,709 DEBUG    Processed response: on
2023-01-14 14:49:54,710 INFO     Model: w1100
2023-01-14 14:49:54,710 INFO     Position: None
2023-01-14 14:49:54,710 INFO     Power on
2023-01-14 14:49:54,711 INFO     Direct power on  : None
2023-01-14 14:49:54,711 INFO     Lamp time        : None hours
2023-01-14 14:49:54,712 INFO     3D               : None
2023-01-14 14:49:54,712 INFO     Picture mode     : None
2023-01-14 14:49:54,712 INFO     Aspect ratio     : None
2023-01-14 14:49:54,713 INFO     Brilliant color  : None
2023-01-14 14:49:54,713 INFO     Blank            : None
2023-01-14 14:49:54,713 INFO     Brightness       : None
2023-01-14 14:49:54,714 INFO     Color value      : None
2023-01-14 14:49:54,714 INFO     Contrast         : None
2023-01-14 14:49:54,714 INFO     Color temperature: None
2023-01-14 14:49:54,715 INFO     High altitude    : None
2023-01-14 14:49:54,715 INFO     Lamp mode        : None
2023-01-14 14:49:54,715 INFO     Quick auto search: None
2023-01-14 14:49:54,716 INFO     Sharpness        : None
2023-01-14 14:49:54,716 INFO     Video Source     : None
2023-01-14 14:49:54,717 INFO     Volume           : None
2023-01-14 14:49:54,717 INFO     Muted            : None
2023-01-14 14:49:54,717 INFO     Supported video sources: ['dp', 'dvia', 'dvid', 'hdbaset', 'hdmi', 'hdmi2', 'network', 'rgb', 'rgb2', 'rgb3', 'svid', 'usbdisplay', 'usbreader', 'vid', 'wireless', 'ypbr', 'ypbr2', 'smartsystem']

Not much meaningful to me. Do You think the failed setup inside home assistant is connected to the obvious communication errors I see here? Like the Unexpected response: b''s?

@rrooggiieerr
Copy link
Owner

Well, actually that looks very promising. The library is not detecting the status of the other features because the w1100 features are not yet known by the library, so that explains why the state of those in None.

If you use the command python3 -m benqprojector /dev/virtualcom0 <baud> examine it will detect the supported features of your projector

@pkoevesdi
Copy link
Author

Thank you
I did that command already, but status as well as examine both don't get a correct response for some commands that I know, the projector knows. But can you quickly point me to the library of commands, I could update them there and make a pull request.
What does Unexpected response: b''mean?

@rrooggiieerr
Copy link
Owner

rrooggiieerr commented Jan 14, 2023

Unexpected response: b'' means it received an empty string while it was expecting something else. The b part of the response means it's a byte representation if the string. It was probably expecting a command prompt '>' but received an empty line first. I'm trying to work around some implementation inconsistencies where the BenQ documentation and the reality are not in sync so there are a lot of check to detect the response state. Can you name a command that you're missing?

The known commands can be found in https://github.com/rrooggiieerr/benqprojector.py/blob/main/benqprojector/config.py

@pkoevesdi
Copy link
Author

Ok, I figured some stuff out and made a configuration for my model. I'll push it to the repo (or make a pull request) if ready.

  • In line 117 in benqprojector.py the regex seems too harsh for my W1100. Some commands do not need a =, some responses do not contain a = or a #. I'm not sure, whether this regex is only used on detecting command echos or also on responses?
  • The lamp time cannot be processed, maybe because of the space before the number or the missing # after?:
2023-01-15 06:41:58,107 DEBUG    command *ltim=?#
2023-01-15 06:41:58,157 DEBUG    Response: 
2023-01-15 06:41:58,158 DEBUG    Empty line
2023-01-15 06:41:58,208 DEBUG    Response: 
2023-01-15 06:41:58,208 DEBUG    Empty line
2023-01-15 06:41:58,259 DEBUG    Response: *ltim=?#
2023-01-15 06:41:58,259 DEBUG    Command successfully send
2023-01-15 06:41:58,260 DEBUG    Response: 
2023-01-15 06:41:58,260 DEBUG    Empty line
2023-01-15 06:41:58,311 DEBUG    Response: *ltim= 1383
2023-01-15 06:41:58,311 DEBUG    LC Response: *ltim= 1383
2023-01-15 06:41:58,311 DEBUG    Raw response: '*ltim= 1383'
2023-01-15 06:41:58,312 ERROR    Unexpected response format, response: *ltim= 1383
  • The command menu doesn't provide reading (*menu=?#), it can only be written, i.e. *menu=on#:
2023-01-15 06:41:59,485 DEBUG    command *menu=?#
2023-01-15 06:41:59,536 DEBUG    Response: 
2023-01-15 06:41:59,536 DEBUG    Empty line
2023-01-15 06:41:59,586 DEBUG    Response: 
2023-01-15 06:41:59,587 DEBUG    Empty line
2023-01-15 06:41:59,638 DEBUG    Response: *menu=?#
2023-01-15 06:41:59,638 DEBUG    Command successfully send
2023-01-15 06:41:59,638 DEBUG    Response: 
2023-01-15 06:41:59,639 DEBUG    Empty line
2023-01-15 06:41:59,690 DEBUG    Response: Unsupported item
2023-01-15 06:41:59,690 DEBUG    LC Response: unsupported item
2023-01-15 06:41:59,690 DEBUG    Raw response: 'unsupported item'
2023-01-15 06:41:59,691 ERROR    Unexpected response format, response: unsupported item

But in general the benqprojector module seems to work. So, now, what next step should I do to figure out how to find the error in the home assistant integration?

@rrooggiieerr
Copy link
Owner

rrooggiieerr commented Jan 15, 2023

@pkoevesdi, Thanks for your detailed investigation.

Thanks for creating a new issue for the regex part of your findings. Maybe also one for the menu command not working? (I already have a response for that)

Please make a pull request in the benqlibrary.py repository with the configuration for your projector.

My proposal is to first make the library work and then focus on the integration. What did you do to to get the external USB2serial device listed in Python?

@rrooggiieerr
Copy link
Owner

Any progress on making the integration work with your virtual com port? Today I'm receiving some parts to build an esp-link device myself, so I'm going to play a bit with that too

@pkoevesdi
Copy link
Author

No real news. benqprojector.py works quite good with a timeout of 0.5 s. I didn't check yet, how low I can go with that value.
And, as You suggested, the integration in home assistant I plan to address not until benqprojector.py examine and status both work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants