Page 1 of 2
Timed out and 2 parallels scripts
Posted: Tue Dec 19, 2017 2:07 pm
by DSX
Hi everybody,
I'm currently working on InterfaceKit 0/16/16 connected to a RaspberryPi 3. All my scripts are in Python, and the libraries are compiled on the Rpi (the Hello World works, and DigitalInput and Output tests works too).
My problem is to run 2 scripts (one for inputs and the other one for outputs) in parallels. In other words, that's what I want to do :
- Run a script like a daemon which watch the inputs (with a infinite loop)
- When it works alone, all is right
- Running a one shot script to activate an output for a moment (few seconds), it doesn't works and say "Timed out".
- If the first script isn't started, the second one works fine
Following my many tests, I have some questions :
- Are there a solution to listen every inputs ? The variable PHIDGET_CHANNEL_ANY doesn't work and only watch channel 0. And if I put -1 (its value) it watch 0 again.
- Why 2 scripts can't be run in the same time to access to the InterfaceKit ? Are there a solution to do the same ?
- Is the infinite loop to watch inputs the best solution ?
If you need more informations to answer me, please let me know.
Thank you for your time.
Best regards
Re: Timed out and 2 parallels scripts
Posted: Tue Dec 19, 2017 2:34 pm
by mparadis
Each channel in Phidget22 is controlled by a separate object, and each object can only listen to one channel at a time. When you use
PHIDGET_CHANNEL_ANY or -1, it will look for the first channel that it can find of the type (e.g. DigitalInput) specified.
In order to listen to multiple channels at once, create separate objects for each one, like this:
Code: Select all
ch0 = DigitalInput()
ch1 = DigitalInput()
ch2 = DigitalInput()
Then, you need to set them each to a different channel:
Code: Select all
ch0.setDeviceSerialNumber(81128)
ch0.setChannel(0)
ch1.setDeviceSerialNumber(81128)
ch1.setChannel(1)
ch2.setDeviceSerialNumber(81128)
ch2.setChannel(2)
After that you can open each channel as normal.
One of the reasons you get a Timeout error is because you cannot access the same channel with two different scripts at the same time
(although you can share different channels on the same board, e.g. one script handling inputs and another handling outputs, or one script handling output 0 and another handling output 1) (Edit: you must open remotely in order to do this). The only exception is if you open it remotely with both scripts. You can accomplish this with these lines:
Code: Select all
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE);
ch0.setIsRemote(1)
Of course, you need to set
IsRemote for each channel you want to open remotely. (Note that some Phidgets such as motor controllers cannot be simultaneously attached to two scripts or programs, even if opened remotely.)
An infinite loop may not be the best solution since it blocks your program and depends on constantly accessing the values that you're watching. A better solution is to set up event handlers for each input. You can do this using the
setOnStateChangeHandler, which will fire an event whenever that input changes. You can see more about how this works if you look at how it's used in the digital input example.
Re: Timed out and 2 parallels scripts
Posted: Tue Dec 19, 2017 5:37 pm
by DSX
First, thank you for your detailled answer.
Create multiple channel is what I've done to have a wotking script for my inputs (for a beginner with Phidget, I'm happy to have the same solution).
If I understand, the Output channel 0 and the Input channel 0 is the same ? But you say after, "one script handling inputs and another handling outputs". It's what I want to do, but it doesn't work. My first script listen 8 inputs, and the second try to set a single output just at a moment.
I used the DigitalInput example to do my script, the infinite loop is here just to prevent the script to exit. You can find my script here :
https://pastebin.com/0hY78US0
I'm sorry, but I haven't understand if remote script may be a good solution for me.
Thanks again for your help
Re: Timed out and 2 parallels scripts
Posted: Thu Dec 21, 2017 3:43 pm
by mparadis
My mistake; I realize now that you cannot access any channels on the same USB connection without using the Network Server (I thought this had changed in Phidget22). In order to do this, you must run "phidget22networkserver" from the console (more info
here).
Then, add
Code: Select all
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE)
just once before opening your channels and also add
setIsRemote(1) for each channel.If you open this way in both of your programs, they will be able to access the same Phidget at the same time.
Output channel 0 and input channel 0 are not the same. They both have an index of zero, but are accessed using different objects (DigitalOutput vs. DigitalInput).
Re: Timed out and 2 parallels scripts
Posted: Fri Dec 22, 2017 4:12 am
by DSX
Ok, thank you very much for your answer, I will try and I'll keep you informed if it's work.
Regards
Re: Timed out and 2 parallels scripts
Posted: Sun Jan 07, 2018 10:09 am
by DSX
Hi,
I've tried to use the network server and it doesn't work.
First, the configuration file doesn't exist (/etc/phidgets/phidget22networkserver.pc), then I create it and populate it regarding this documentation :
https://www.phidgets.com/docs/Phidget_Network_Server
The daemon starts fine (phidget22networkserver -D), but no server is find with the command phidget22admin -s. It returns nothing.
Here is my configuration file :
Code: Select all
phidget{
enabled: true
pidfile: '/var/run/phidget22networkserver.pid'
feature {
control {
enabled: true
}
stats {
enabled: true
}
dictionary {
enabled: true
directory: '/etc/phidgets/dictionary.d'
}
}
logging {
level: info
file: '/var/log/phidgetsnetworkserver.log'
maxfiles: 4
maxsize: 1048576
network {
enabled: true
port: 5771
}
source {
phidget22net {
level: info
}
_phidget22usb {
level: info
}
_phidget22match {
level: error
}
_phidget22channel {
level: error
}
_phidget22bridge {
level: error
}
_phidget22disp {
level: error
}
}
}
network {
keepalive: 30000
ipv4 {
address: localhost
port: 5661
}
datagram {
enabled: true
}
publish {
enabled: true
name: 'Phidget22 Server'
}
resolveaddrs: false
}
}
There are no firewall and the daemon is running (it appears in the ps command).
Thank you for helping
Re: Timed out and 2 parallels scripts
Posted: Tue Jan 09, 2018 12:27 pm
by mparadis
Does it give you any results if you run it with sudo? If you don't have your Udev rules set up you may not have permission to see any attached devices.
Re: Timed out and 2 parallels scripts
Posted: Fri Jan 12, 2018 11:10 am
by DSX
I execute commands with root account, and no result.
Which set up I have to do with udev ?
Re: Timed out and 2 parallels scripts
Posted: Fri Jan 12, 2018 1:19 pm
by Patrick
Have a look at the network server logfile. Are there any messages about zeroconf/avahi failing to start? You may need to make sure that libavahi-client3 or equivalent is installed via your distributions package system, otherwise devices are not published and won't show up in the admin tool.
-Patrick
Re: Timed out and 2 parallels scripts
Posted: Sun Jan 21, 2018 9:40 am
by DSX
Thank you for your help. I've solved this avahi problem and others in same time.
Now my daemon starts without issues and the command "phidget22admin -s" returns my localhost server.
But the command "phidget22admin -R -d" says nothing.
Here is the log file :
Code: Select all
INFO [phidget22][2018-01-21T16:44:08]:******************** Logging enabled ********************
INFO [netsrv][2018-01-21T16:44:08 server.c+167 enableLogging()]: logging: _phidget22bridge=error
INFO [netsrv][2018-01-21T16:44:08 server.c+167 enableLogging()]: logging: _phidget22channel=error
INFO [netsrv][2018-01-21T16:44:08 server.c+167 enableLogging()]: logging: _phidget22disp=error
INFO [netsrv][2018-01-21T16:44:08 server.c+167 enableLogging()]: logging: _phidget22match=error
INFO [netsrv][2018-01-21T16:44:08 server.c+167 enableLogging()]: logging: _phidget22usb=info
INFO [netsrv][2018-01-21T16:44:08 server.c+167 enableLogging()]: logging: phidget22net=info
INFO [netsrv][2018-01-21T16:44:08 phidgetserver.c+109 startPhidgetServer()]: Changed network keepalive to 30000
INFO [phidget22net][2018-01-21T16:44:08 servers.c+1444 setAllowClients()]: Client network connections allowed by default
INFO [_phidget22usb][2018-01-21T16:44:08]:Initializing libusb
INFO [phidget22net][2018-01-21T16:44:08 network.c+102 PhidgetNet_start()]: Starting Networking
INFO [_phidget22usb][2018-01-21T16:44:08]:New Phidget found in PhidgetUSBBuildList: 1/4
INFO [phidget22net][2018-01-21T16:44:09 servers.c+1049 startServer()]: Published '_phidget22server._tcp' on port 5661 for discovery
INFO [netsrv][2018-01-21T16:44:09 phidgetserver.c+131 startPhidgetServer()]: Started Phidget Server Phidget22 Server localhost:5661
And my phidget22networkserver.pc (I think it's the minimal configuration) :
Code: Select all
phidget{
enabled: true
feature {
control {
enabled: true
}
stats {
enabled: true
}
dictionary {
enabled: true
directory: '/etc/phidgets/dictionary.d'
}
}
network {
keepalive: 30000
ipv4 {
address: localhost
port: 5661
}
datagram {
enabled: true
}
publish {
enabled: true
name: 'Phidget22 Server'
}
resolveaddrs: false
}
logging {
level: info
file: '/var/log/phidgetsnetworkserver.log'
maxfiles: 4
maxsize: 1048576
source {
phidget22net {
level: info
}
_phidget22usb {
level: info
}
_phidget22match {
level: error
}
_phidget22channel {
level: error
}
_phidget22bridge {
level: error
}
_phidget22disp {
level: error
}
}
}
}
Have you any idea about this issue ?
Thanks, regards.