Read values from a sensor in python - python

First of all, I'm a noob. I'm doing my best here. I really do.
I have a pressure sensor called KITA KP70. This is it's manual:
https://drive.google.com/file/d/1ED1kr3cW1mmgM_-hSxhoo-Cbr2zI3ZSo/view
I'm trying to read anything from it using python without any luck. All I get back is an empty response after the code times out.
This is the code I'm using:
import serial
port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=2)
while True:
port.write("30H30H31H32H".encode())
rcv = port.read(10)
print (rcv)
Now the thing is connected as this:
The sensor itself is connected to UART to RS485 Converter. This is the device:
https://www.amazon.com/HiLetgo-Reciprocal-Hardware-Automatic-Converter/dp/B082Y19KV9
The converter is connected to c232hm-ddhsl-0. This is the product:
https://ftdichip.com/products/c232hm-ddhsl-0-2/
And the c232hm-ddhsl-0 is connected using USB to my PC.
I can see the device when checked using the terminal:
ahmad#Ahmad-PC:~/Desktop$ sudo dmesg | grep tty
[ 0.073387] printk: console [tty0] enabled
[ 13.131371] usb 3-1: FTDI USB Serial Device converter now attached to ttyUSB0
When I execute the command above, the write LED blinks, but nothing else happens.
I've checked the cables and everything seems to be correctly connected and to the right color.
Can anyone help me read anything from this device? The list of codes this device accept are listed in the manual.
Thank you in advanced!

You have been fooled by poor documentation. When they say "30H30H31H32H", they are using an antique method of indicating hex digits. What they mean is the four-byte sequence 0x30 0x30 0x31 0x32, which happens to be the string "0012".
For what it's worth, that number style comes from Microsoft's MASM assembler.

Related

How to read file input from a text file in pc to arduino

So, basically I have a file on my pc which contains some statements (string). I want to send those strings character by character to arduino uno. fopen() doesn't work in arduino (if that's what i read is correct).
I don't know if serial port will work as taking input from a file on my pc.
Is there a way to send character by character data to arduino?If yess, then please guide me.
If data can be sent through python and/or command prompt(terminal) please tell me how to do it.
Thanks in advance.
There are countless ways to communicate with an Arduino, given you have the respective shields. Wifi, Ethernet, Bluetooth, ...
You can use a USB to Serial converter, connected to the Arduinos Rx/Tx pins. Most if not all Arduinos come with an integrated USB to serial converter onboard. Connect the Arduino to your PC with a USB cable. Make sure you have the drivers for that USB-serial converter installed and you're ready to go.
All you need to know is how to send data from your PC through the virtual COM port of that USB-serial device and how to read that data within your Arduino sketch.
Then you open a file on your computer and send it to the Arduino byte by byte.
There are countless tutorials available. Just search the web for "Arduino serial" and "Python serial" in case you want to do the PC part in Python.

Moving Sign Communication Protocol v1.2 with Raspberry Pi

This is my first post here so forgive me if some questions might be out of scope, but basically I'm trying to write my own program to push messages from a Raspberry Pi to a LED scrolling sign via serial communication (USB on Pi -> RS232 adapter -> LED sign).
Im not very familiar with serial communication in general, but am attempting to use the PySerial library on the Pi (http://pyserial.sourceforge.net/pyserial.html) in the format of Moving Sign Protocol V1.2 (http://www.brgprecision.com/pdffiles/Protocol12.pdf).
Here is my python code thus far, error free, yet the plugged in sign doesnt receive any data.
import serial
#default port is /dev/tty/USB0
#portname, baudrate, timeout
port = serial.Serial('/dev/ttyUSB0', 9600)
port.open() port.write('0x00\0x01\"FF"\"03"\0x02\'A'\'A'\'A'\'2'\'2'\'7F'\'0100'\'1200'\'000'\'1'\"OMFG"\0x03\"0564"\0x04')
port.close()
Basically, I dont know how to parse the message Im trying to write into proper serial data packages. Do I send the protocol and text message all in one go like above? Or must I parse each of the fields and send them separately like:
port.write('01') # start of head
port.write('46') # pc address
port.write('46') # number 1 display
ect...
I should mention that I also have sniffed the USB serial communication on my PC and can confirm this serial information is correct, I just have no real idea how to use it on a RPi. Any help will be much appreciated!
you want to use bytes
0x00 == 0 == "\x00"
which is not the same as "0x00" which is really "\x30\x78\x30\x30" which is 4 characters not the single byte that you are hoping to achieve
so to send the part A
ser.write("\x00"*5) # 5 null bytes
to send the SOH part
ser.write("\x01")
Im not sure what the address stuff is... but that hopefully gives you an idea

Determine which USB device gives the input in Raspberry Pi

I'm working on a project with Raspberry Pi. I have two identical keyboard emulator devices as inputs. In my program, I need to know which one gave the input. Is there a way to do this in Python? Thank you!
Do you have control over these devices? Could you change the USB protocol to something more reasonable, like a USB CDC ACM virtual serial port?
Do they have to by identical? If not, I would do something simple like have one of the devices only send capital letters and have the other device only send lower-case, but I guess that doesn't extend so well if you need to send a number.
With two keyboard emulators, you have to worry about what happens if the messages overlap. For example, if device 1 tries to type "banana" and device 2 tried to type apple, there is nothing to prevent your python program from reading something like "applbaneana".

Identifying serial/usb device python

The solution to this problem is probably pretty simple, but I am new to interfacing with a device dynamically. What I'm doing is I am making a python executable code, so the user doesn't have to have Idle on their computer or any kind of python interpreter, which means I don't know which USB port the device will be plugged in to. The program needs to be able to open a connection to a device that is connected through a serial to usb converter. How can I determine which connected device is the correct device to open a port to? I am using pySerial to interact with the device. Any help would be greatly appreciated.
No matter how you configure your device, at some point you're probably going to have to ask the user where the port is, or poll all serial devices for a known response. (Polling has it's pitfalls though, so read on!). Unlike USB devices, there is no vendor/device ID that is made known to the OS when you attach a plain-old serial device.
First you need to find the serial ports. Here's a question that might help: What is the cross-platform method of enumerating serial ports in Python (including virtual ports)?.
Once you have a list of serial ports, you could ask the user whether they know which one to use. If they do, problem solved!
If they don't, you could offer to poll ALL serial devices with some data that you know will yield a certain response from your device. Keep in mind though that if the user has other serial devices attached, your string of "hello" bytes might actually be the self-destruct sequence for some other device! Hence, you should warn the user that polling may interfere with other devices, and always prompt them before you do so.
Without knowing more about your code (eg. what comms framework, if any, you're using; are you doing this in the console or are you using a GUI toolkit, etc), it's impossible to say what the best way to code this might be. In the simplest case, you could just loop over all of your serial devices, send the greeting and check for a response. (You could also do this in parallel: loop once for the greeting, and loop again to check what's in the buffer. If you're going to get more fancy than that, use a proper library.)
Side note: You might be able to get around this if you have a built-in converter that you can set the vendor/device ID for, but the converter will still be automatically detected by any modern OS and enumerated as a serial port; you won't get to talk to it directly as a USB device. It could be possible to figure out which port goes with which ID, but I've never tried to do that. But this approach is useless if you're not the one who gets to pick the converter (eg. if it's a user-supplied cable).
This is the way that I used for identify Serial-USB conveter device like Arduino.
It checks 'USB' String of device description.
import serial as ser
import serial.tools.list_ports as prtlst
global COMs
COMs=[]
def getCOMs():
global COMs
pts= prtlst.comports()
for pt in pts:
if 'USB' in pt[1]: #check 'USB' string in device description
COMs.append(pt[0])
The only way I can think to get around the problem with probing unknown devices is to have the device send unsolicited "hello" responses continually. That was you can just connect to all serial devices and listen for the "hellos". Connecting and listening to a serial device shouldn't ever mess it up.
The downside is you have these messages cluttering up your serial stream. You could have a "I'm here now, stfu" command but then you can only connect to it once.
FTDI chips have a method of identification but you have to use their library to access the data.

Programmatically talking to a Serial Port in OS X or Linux

I have a Prolite LED sign that I like to set up to show scrolling search queries from a apache logs and other fun statistics. The problem is, my G5 does not have a serial port, so I have to use a usb to serial dongle. It shows up as /dev/cu.usbserial and /dev/tty.usbserial .
When i do this everything seems to be hunky-dory:
stty -f /dev/cu.usbserial
speed 9600 baud;
lflags: -icanon -isig -iexten -echo
iflags: -icrnl -ixon -ixany -imaxbel -brkint
oflags: -opost -onlcr -oxtabs
cflags: cs8 -parenb
Everything also works when I use the serial port tool to talk to it.
If I run this piece of code while the above mentioned serial port tool, everthing also works. But as soon as I disconnect the tool the connection gets lost.
#!/usr/bin/python
import serial
ser = serial.Serial('/dev/cu.usbserial', 9600, timeout=10)
ser.write("<ID01><PA> \r\n")
read_chars = ser.read(20)
print read_chars
ser.close()
So the question is, what magicks do I need to perform to start talking to the serial port without the serial port tool? Is that a permissions problem? Also, what's the difference between /dev/cu.usbserial and /dev/tty.usbserial?
Nope, no serial numbers. The thing is, the problem persists even with sudo-running the python script, and the only thing that makes it go through if I open the connection in the gui tool that I mentioned.
/dev/cu.xxxxx is the "callout" device, it's what you use when you establish a connection to the serial device and start talking to it. /dev/tty.xxxxx is the "dialin" device, used for monitoring a port for incoming calls for e.g. a fax listener.
have you tried watching the traffic between the GUI and the serial port to see if there is some kind of special command being sent across? Also just curious, Python is sending ASCII and not UTF-8 or something else right? The reason I ask is because I noticed your quote changes for the strings and in some languages that actually is the difference between ASCII and UTF-8.

Categories