I'm trying to send a file (a .jpg image in this case) over a serial port.
Currently it works by calling an external script:
subprocess.Popen(['./sendFile.sh','myImage.jpg']).communicate()
where sendFile.sh is:
cp /home/pi/$1 /dev/ttyAMA0
This method works but is somehow unstable. Sometimes my Python program stops after the transferring the file.
I'm wondering if there's a way to do this in Python instead of calling a script for help? I searched about XMODEM, but it seems like both ends must have it installed. I'm sending the image to a GPRS chip, which doesn't allow me to modify any code on it. So installing something to the receiving end is impossible.
import serial
s = serial.Serial("/dev/ttyAMA0")
s.write(open("target.txt","rb").read())
You can use os.open to directly open a dev:
f = os.open('/dev/ttyAMA0', os.O_RDWR)
f.write(open("myImage.jpg").read))
f.close()
Related
What I put into my otherwise mostly empty main function was basically:
import uos
uos.dupterm(None, 1)
I uploaded this code to my microcontroller and it stopped being able to connect to my computer. My aim was trying to connect to a Bluetooth module (ZS-040). Now I can't even connect to the microcontroller.
I was intending to make a serial connection to the Bluetooth module while keeping the UART0 bus separate for USB and REPL connection. Now, I am stranded, having done quite the opposite. How do I fix this?
If you have enabled the webrepl, try using it to connect & upload a new, blank 'main.py'.
https://docs.micropython.org/en/latest/esp8266/tutorial/repl.html?highlight=webrepl
(this works on my ESP8266 after running dupterm(None, 1)). Failing that, re-flash the firmware to erase 'main.py' and re write the script to either issue dupterm manually at the start or to issue 'dupterm(UART(0, 115200), 1)' after some later event to re-attach the repl.
I'm currently working on a little script that I found on GitHub, made by RoGeorge : https://github.com/RoGeorge/DS1054Z_screen_capture.
This script should allows me to get a screenshot of my digital storage oscilloscope (DS1054z from RIGOL).
So I've putted this script on my Raspberry Pi to have use it, but once everything was correctly setup, the script seems to receive nothing from my scope. This is surprising because : I can ping the scope and I can communicate with him by the telnet command on the terminal.
So I've made a little script (that I will put just below) that permit me to see if the Telnet library for Python work or not.
Result : It do not work when we went to read, but it's send without problem.
import telnetlib
tn = telnetlib.Telnet('193.168.1.2','5555')
tn.set_debuglevel(100)
tn.write("*idn?")
tn.read_until('\n',1)
Output :
>> Telnet(193.168.1.2,5555): send '*idn?'
>> ''
Is my code correct ? Did anyone have an idea of what is happening ?
Thanks in advance !
PS : I've tested the script from GitHub and mine just above on my Windows 10 PC also. Don't work.
When you send a command you have to end it with a newline like this:
tn.write("*idn?\n")
I'm currently attempting to write over USB serial to an Arduino Nano of mine using Python. However, what I've discovered is that (using the exact same code), the code works perfectly when I type it into IDLE, but when I save it to a file and attempt to run from there, for some reason the Arduino is never receiving the data. I've checked and in both locations the correct version of Python is being used (2.7.9) (I unfortunately can't use Python 3 due to other libraries I'm using).
The code I'm using:
import serial
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600)
ser.write('0')
print ser.readline()
When I run it in IDLE just by typing in the lines individually, the correct behavior is seen: the Arduino responds (turning a servo) and echoes back the data it was sent, which is printed correctly. Running from a saved file however, the servo does not respond and no echo is received.
Any ideas?
I somehow missed this answer on SO before (pySerial write() works fine in Python interpreter, but not Python script), but it turns out that I needed to add a time.sleep(2) after opening the serial port. My guess is that in IDLE the time it took for me to type the next line accounted for this delay, but it was happening instantly in code.
I am using pyserial to communicate with my Multimeter (Keithley2000) over an FTDI based usb-to-rs232 adapter (Using the Mac OS X built-in driver).
Before I started with Pyserial I verified that the Hardware & Driver works by using a terminal app (Serial.app) to communicate with the Multimeter.
This works just fine.
But if I try the same thing with the simple program below, it doesn't work.
Writing works fine (I can for example reset the instrument), but I can't read anything back.
import serial
import io
port = serial.Serial("/dev/cu.usbserial-FT8VXG35", 9600, timeout=1)
port.write("*idn?\r")
print(port.readline())
Now here comes the weird part. If i leave Serial.app open in den background, the program actually works.
Does anyone know the reason for this behaviour?
Here are my settings in Serial.app
Some peripherals need the flow control lines (RTC/CTS, DSR/DTR) set to specific levels - maybe having serial.app running the background sets these lines the way your multimeter needs?
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.