Trigger an cognex camera by script - python

I have got a Cognex Advantage 100 camera connected to my PC via ethernet.
After pressing F5 in the inSight Explorer to trigger the camera I can use the captured image in a Python script.
Can I make the Python script trigger the image capture itself?

I'm not very familiar with the Advantage series, but I am quite familiar with the other In-Sight cameras. I'm going to assume the Advantage is similar to other In-Sight cameras.
You should be able to achieve a trigger from python by opening a telnet connection to the camera (on port 23), logging in (default username: admin, password: ), and sending the command 'SE8'. The camera trigger mode must be set to External, Manual or Network. If the command is successful, it will respond with a '1'. I'd suggest trying this with a telnet client before trying it in python. Suggested telnet clients: Putty or Hercules.
More information can be found in the In-Sight Explorer help file. From the contents, go to 'Communications Reference -> Native Mode Communications'.

Possibly you could simulate a key press. This answer here and this answer here, might help you do that.
Apart from that, your camera software doesn't allow you to interact with it via python, but it does supply it own method of programming the camera here. Try that instead, it seems to be the indented way of doing this.

Related

Watch module /sys/module/uvcvideo/refcnt for changes (to trigger action on webcam use)

I would like to switch an On Air sign at my work-from-home office when my webcam is on.
Detecting whether the webcam is on can easily be done via
cat /sys/module/uvcvideo/refcnt
if it outputs 0 no app is using the webcam, otherwise it is.
I already have a Wifi plug which can be switched using a HTTP GET request.
My only question is: Do I have to poll the output of the above command regularly, or can I get notified in some Bash, Python or PHP script from the kernel?
The idea would be to not generate unnessary system load by polling - or doesn't that matter?
I already tried inotifywait but it does not seem to notice changes on the above pseudo file.
Based on jasonharpers answer I implemented it as a daemon that uses polling
https://github.com/amenk/OnAirDaemon

Remotely sending inputs to a running Python script

I'll try to be as clear as possible with what I'm trying to aim for.
I have a running Python script on my Raspberry Pi and I'd like multiple users to send inputs to the script remotely (through SSH or anything else that might work better).
So for example if I have this script running:
Name = input ("Please type in your name. \n")
type (Name)
print ("Hello there" , Name)
time.sleep(3) # Pause for 3 seconds.
I want users to send names to this script remotely from devices that are connected to the same network as the Raspberry Pi.
If possible, I also want to implement the following functionalities:
Sending the output (aka the printed text) back to the specific device the input came from.
A queuing system: If multiple users send names at the same time, the script will take the names in order, one by one.
I know it's a lot to ask for, but I'd really appreciate if someone could help me get started with this by pointing me in the right direction. I've searched around quite a bit for the past few days but I haven't really come across anything that fits my needs.
Edit: I'm running this on PYTHON 3
Your comment that you would like to communicate (via network) to the script directly, opens up a world of possibilities. You have to modify your Python script a little though, because it won't communicate via stdin/stdout any longer.
I'm still not entirely sure how you want things to work but it does sound to me that a solution based around RPC can possibly work for you. May I suggest you have a look at Pyro4? Basically what that does for you is enable you to do normal Python method calls, but over the network, to code running on another computer.
So you can set up a server on your Pi (that needs to run continuously) which accepts remote calls from other computers, and can then call into your python code on the pi. It can process calls in parallel or in sequence. You didn't say if you need any form of security, but some basic security features are provided (no built-in encryption or communication over TLS yet, sorry).
A simple example is here and lots more are on github so you can have a look to see if this fits your requirements?
Another solution that doesn't require third party libraries is perhaps to write a WSGI http server that calls your script, run this on the pi, and access it via HTTP from your other computers.

Python3 - curses input/output over a network socket?

So I've decided to learn Python and after getting a handle on the basic syntax of the language, decided to write a "practice" program that utilizes various modules.
I have a basic curses interface made already, but before I get too far I want to make sure that I can redirect standard input and output over a network connection. In effect, I want to be able to "serve" this curses application over a TCP/IP connection.
Is this possible and if so, how can I redirect the input and output of curses over a network socket?
This probably won't work well. curses has to know what sort of terminal (or terminal emulator, these days) it's talking to, in order to choose the appropriate control characters for working with it. If you simply redirect stdin/stdout, it's going to have no way of knowing what's at the other end of the connection.
The normal way of doing something like this is to leave the program's stdin/stdout alone, and just run it over a remote login. The remote access software (telnet, ssh, or whatever) will take care of identifying the remote terminal type, and letting the program know about it via environment variables.

Is it possible to send "resize pty" command with telnetlib?

I am looking for a way to resize terminal when using telnetlib. I have achieved a similar effect on SSH with Paramiko's resize_pty, but I need to support telnet protocol too. Is this even possible (does telnet even have a control stream)?
Note that telnetlib is not a requirement - if there is a better library I would be happy to use it.
UPDATE (more background): I am building a web-based interface for connecting to networking devices. Frontend is built using JS/AJAX, it basically just sends keystrokes to backend and receives screen content from it. Backend is written in Python and takes care of opening a SSH/telnet session to a device, sends keystrokes to it and fetches an output stream, which is then passed through VT100 virtual terminal (pyte). The contents of virtual screen are then sent back to frontend. The problem arises when user wants to resize the terminal screen size in his browser. With SSH I just send resize_pty() through Paramiko and then also resize the pyte's virtual terminal screen size. But with telnet I was unable to find the appropriate resize function that would tell the device that it should resize its terminal. Is this possible?
Ok, I've been able to assemble the following masterpiece:
naws_command = struct.pack('!BBBHHBB',
255, 250, 31, # IAC SB NAWS
width, height,
255, 240) # IAC SE
t.get_socket().send(naws_command)
Now a few words of explanation.
First of all, telnetlib does not support sending commands directly; it simply escapes them. Thus, to send the command we have to use the underlying socket directly. We do that using the get_socket() method of the Telnet object instance (t here).
The NAWS command assembled here is defined by RFC 1073. The width and height variables are regular Python integers which get packed into two 16-bit unsigned integers.
Note that this isn't a perfect solution and I'm not sure if it will actually work for you. Most importantly, during the capabilities negotiation, telnetlib will inform the server that it WON'T NAWS, so a particular server may actually ignore the commands.
If that's the case, you'd probably have to use set_option_negotiation_callback(). Sadly, that means you will have to handle all the options which normally telnetlib does for you. And AFAIK it has no conveniences for that.

How can I detect when a flash drive is plugged in under Linux?

How can I detect when a flash drive is plugged in? I'm using a bare Debian installation, without any GUI and want to be notified in my Python script when a new flash drive appears... I know that D-BUS distributes such information, but i dont want to use D-BUS. Is there a more bare access to that information? Shouldn't that be available under /proc or /sys? How can I "connect" to that source?
Bye
falstaff
All mayor Linux distros include udev, that allows you to write custom scripts on hardware events.
You can read uevents from kernel via a Netlink socket and look for events where "ACTION" is "add" (you can also watch if a partition from a device was mounted or unmounted, or if a device is being removed). That's as close to the source of events as one can get in user space. As far as I know, this is how udev detects inserted removable media.
But probably using D-Bus/HAL API via Python bingings will be much easier (no uevents data parsing, etc). Not sure why you are opposed to this. Since you are using Python, I suspect that resources are not really the issue.
If you are targetting an embedded device, then you can run mdev instead of udev.
Then you can write mdev rules that are quite simple and triggers a script.
Of course you are not directly monitoring from your script, mdev is doing it, but you can launch any command. You can probably do the same thing with udev, but it always looked quite complicated to me.
When an USB device is plugged in syslog writes messages concerning this to /var/log/messages. The "dmesg" command shows this log. You can check near the end of the log to see which channel the device was attached to, it is usually /dev/sd(letter)(number) depending on the partitions and number of serial disks plugged into the system.
/proc/partitions shows all the partitions known to the kernel.
I did this using zenity in a script and udev with rule on rhel6 with:
KERNEL=="sd[b-d]", DRIVERS=="usb", ACTION=="add", RUN+="/path/to/script"

Categories

Resources