Pyserial microcontroller to host communication - python

I am using a Beaglebone Black (BBB) with Python and pyserial to communicate with an OBD-II reader. I am essentially trying to build a customizable digital gauge panel. Ideally I would like to use Flash for the GUI. Sadly Linux support for Flash is pretty weak. I would like to be able to send data from the BBB using Python to a OSX host computer.
I am currently using terminal to shell into the BBB to run code. I would need to be able to send data from the BBB via a USB/serial interface to the OSX computer running Flash. What would be the best method of accomplishing this?

I have not used beaglebone. I have worked with arduino's serial I/O. But this post says you have multiple serial I/O ports on BBB. Find appropriate connectors/convertors for serial to USB.
Then use the pyserial python module.
On OSX, you will find your device when connected on a path like /dev/ttyo1 where dev is my system name and ttyo1 or something similar will be your device.
import serial as s
device = "/dev/tty01"
bbb = s.Serial(device, 4800) #the second param is baudrate
while(True):
bbb.readline()
# do what you want with the output.
bbb.write('input')
This will read till the end of line character and give you a string. and then write "input" to the serial io on bbb. You will need a similar program running on BBB to read this input and do what you want to do with it.
So there will be two python programs. One on the OSX and the other on the BBB
That way you can give commands from OSX.py, let your BBB.py process and send a response. Which the OSX.py will read and do what is to be done.
You will have to design the input/output cycle properly.
Also if flash is not really necessary you can check out pyside.

Related

Is there a way to view what is being written on serial from a Python script without connecting a device?

In short, I have written a Python program to write a series of commands to a serial device I'm working with, and I want to make sure they're working in the way I want them to.
For sure, no device connected means no response/nothing to read...
Did you consider trying a serial port sniffer, if you google for serial port sniffer, you'll find more than you need.
Connect any serial device and sniff what you send.

Connect two BBB to one PC using USB

My question is how to connect two BBB (Beagle Bone Black) into one PC using USB and communication with them at the same time.
I am trying to write a python script that two BBB boards to communicate with each other. The idea is that these boards should communicate with each other using iperf and an external cape (OpenVLC).
The problem is that I need to use the iperf on the server-side and then read the data on the client-side.
For this purpose, I need to connect them to one PC to be able to read commands and write the results.
##USB_NETWORK_DISABLED=yes
###To Run Multple Boards on one USB HOST, change the 3rd octal on every
###USBx_SUBNET define and USBx_ADDRESS
###BeagleBone Number 2: USB0: 192.168.11.2 USB1: 192.168.10.2
###BeagleBone Number 3: USB0: 192.168.13.2 USB1: 192.168.12.2
###And So On..
This is from bb-boot on the images from rcn-ee.net/com.
That's going to be a struggle. Both BBB grab the same IPs (192.168.6.2 and/or 192.168.7.2 depending on your PC's operating system) on their virtual Ethernet adapters and assign the same address to the PC side (192.168.6.1 and/or 192.168.7.1). You can change that in the startup scripts (google for details). Then you'd need to set your PC up to route traffic between the two which depends on your operating system. If you haven't done networking before, it's going to be hard.
Instead of USB, I'd strongly recommend connecting all devices to a simple router box using Ethernet. It just works.

Sniffing data of serial port without removing data from buffer

I am using the bgapi library to manage bluetooth communication using a USB dongle. The library will take a command from my program and will handle all serial communications through the COM port in its own thread. I want to echo back all data coming out of the COM port, but the library only gives me access to what it chooses to parse out itself.
I could go into the bgapi library and change the functionality of the code, setting up a variable or function to return what data is currently being read, but other people are also working on this project and changing the library could cause larger problems or invalidate updates.
Is there any way for me to access the data coming into the COM port without interfering with the library, like sniffing the data going through the COM port without taking it out of the buffer for the library? The library holds the port open for itself and discards the extra data that I want to see.
I'm not sure I understood completely how your library works so I'm not sure this will work for you but you can give it a try anyway.
What you can do (on Windows) is use Termite as a man-in-the-middle with port forwarding.
Since you probably want to keep everything inside one computer you can use com0com to create a couple of virtual ports.
To activate port forwarding on Termite you have to go to settings and then forward on the bottom-left side of the screen. You'll see a menu where you can choose the port you want to forward to. On the following screenshot I can forward from COM1 to COM2:
After selecting the right settings for COM1 you accept and connect by clicking on the big button marked COM1 57000 bps... and you'll see everything incoming on COM1 forwarded to COM2 and displayed on the console.

Issue with raspberry pi serial port

I'm using raspberry pi 2 to control 2 types of printers. I found at this site how to send serial commands in hexadecimal format using python, like:
cmdTest=b"\x12\x02\x42"
ser.write(cmdTest)
This code works true for one printer but fail for another!
knowing that:
I test both devices using a normal computer and both works.
Both are from same manufacturer (ELTRASISTEMI).
I am using the appropriate commands for every printer
I am specifying the correct baud rate knowing that both are of the same baud rate.
My problem solved, Although something still not clear.
The commands were correct the problem is something related to the serial convertor I were using. Now I used a usb to serial convertor instead of a shield convertor and it works great with same code and same commands. Although it worked, I'm still confused why a printer worked with RS232 shield and other not.

Python: get keyboard input from MagStripe reader

For my DIY access control system, I'm running Debian on a Raspberry Pi attached to a MagStripe card reader. When a card is swiped, the reader writes the data from the magnetic stripe over the USB port by acting like a keyboard. For example, plug it into your computer, open a text editor, and swipe a card and you'll see a string of data printed out like you typed it with your keyboard.
Simple connection diagram:
Client <--ssh--> Host + card reader
The only problem is that I'm running my Python script over ssh, which doesn't directly hear the keyboard input.
How can I get Python to listen to the keyboard events coming from the MagStripe reader? (do I need to use a keylogger? PyGame's keyboard bindings?)
Thanks for the help!
On Linux, USB keyboards can be accessed via /dev/input.
See: format of /dev/input/event*? - the answers to that question include two different Python modules for getting events, and links to more documentation.
To translate from key codes to ASCII, see How can I translate Linux keycodes from /dev/input/event* to ASCII in Perl?
You will probably need to be root, or change the permissions on /dev/input.
It may be worth dividing the program into a two pieces: one as a service that starts at boot time on the raspberry pi which will get its stdin from your keyboard device(the reader) by default, and another that provides remote access functionality over ssh. This way you can avoid having to deal with devices in /dev directly.

Categories

Resources