Anyone here good at Python?

Status
This old topic is closed. If you want to reopen this topic, contact a moderator using the "Report Post" button.
I've just downloaded and installed Python 3.8.6 and Pyserial. Never used Python before.

I'm skimming through the tutorials but aren't currently finding what I need to know. My Google-fu might be off this evening and I've run out of time again.

I've got a binary file of approx. 1Mbyte in size. I need to write a script to load/import said binary file and to then sequentially send each byte of said binary file out the serial port.

That's it.

Thanks.
 
What does Python have to do with this? You have a file that needs to go through a serial port. Where
is this file stored? Hard drive, flash drive, optical drive? Serial port implies baud rates, start-stop bits parity bits.
You need a processor (computer) that can read the file and have whatever port needed to send to wherever.
You may need nothing more than a USB to serial port adapter and simply drag/drop.

You can use Python but may not NEED Python.

 
Last edited:
You need a processor (computer) that can read the file and have whatever port needed to send to wherever.




Thank you Sherlock Holmes.


That "processor (computer)" is my PC. The binary file is on my PC. On what kind of drive it resides is completely irrelevant.


I need to program my computer to do what I explicitly stated. That is what Python and the Pyserial extension is for.
 
I had a brief look over there and all I see is a million questions with zero answers. Couldn't be stuffed signing up to another forum. I'd rather keep reading until I figure it out myself.

A colleague did this for me a few years ago and it was like three or four lines of code in < 5 minutes, though rather than a binary file the input file was a comma-delimited text file ("255, 33, 54, 129, 32...... etc").
 
Last edited:
Member
Joined 2007
Paid Member
Yeah, I wouldn't think it would take too many lines. It's been a while since I've played with Python on Saturdays. Yeah, you need a line to read the file from your local machine to load into memory and call pyserial. Sorry, I know I'm not much help here.
 
Python has got to be the biggest propeller-head mess of a language ever. 90% of the help I find on the net is now obsolete and no longer applicable to the latest version(s) of the language (of which there a multiple variants).

I've figured out how to import and manipulate binary files, but I have NFI about how to install pyserial.

Following the instructions here: pySerial — pySerial 3.4 documentation

... I end up here: pyserial * PyPI
...and find the pyserial-3.5.tar.gz which when unpacked is supposed to contain a "setup.py" file, which you apparently run from its resident folder with the command "python setup.py install", but there is no "setup.py" file, just a file called "pyserial-3.5.tar". I have NFI what I am supposed to do with this *.tar file and nothing I can find so far is helping or working.

I give up and am going to bed.
 
AX tech editor
Joined 2002
Paid Member
I guess that there is *something* at the serial output that is receiving the file? A common serial i/o utility under windows can copy any file to any other device.
Insisting to use Python will bring a lot of effort and the need to learn a new language. Are you sure you want to go through all that?

Jan
 
I'd say Python is a good tool for the job. Here's an example that does what you described in the first post:
raspberry pi - Send file over serial port from Python - Stack Overflow

The full example is just three lines of code:
import serial
s = serial.Serial("/dev/ttyAMA0")
s.write(open("target.txt","rb").read())

Your application may need some additional details like baud rate and stuff when you open the serial port, but the general idea is there.
 
On any UNIX (like Linux) system (no python requirement),
you could:

cat {filename} > /dev/{whatever your serial device is called}

I was thinking the same, but on second thought I thought that you first need to open and configure the serial port (baud rate, stop bits, etc.). But maybe I was thinking too much...?
 
Last edited:
Python has got to be the biggest propeller-head mess of a language ever. 90% of the help I find on the net is now obsolete and no longer applicable to the latest version(s) of the language (of which there a multiple variants).

I've figured out how to import and manipulate binary files, but I have NFI about how to install pyserial.

Following the instructions here: pySerial — pySerial 3.4 documentation

... I end up here: pyserial * PyPI
...and find the pyserial-3.5.tar.gz which when unpacked is supposed to contain a "setup.py" file, which you apparently run from its resident folder with the command "python setup.py install", but there is no "setup.py" file, just a file called "pyserial-3.5.tar". I have NFI what I am supposed to do with this *.tar file and nothing I can find so far is helping or working.

I give up and am going to bed.
Normally you'd use pip to install Python packages. "pip install pyserial" for instance. No need to mess with install files once you've got pip up and running
(or pip3 for Python3 if you have both Python2 and Python3 installed).
Home - pip documentation v21.0.1
 
I guess that there is *something* at the serial output that is receiving the file? A common serial i/o utility under windows can copy any file to any other device.
Insisting to use Python will bring a lot of effort and the need to learn a new language. Are you sure you want to go through all that?

Jan




The receiver is a microcontroller. I'm using (or trying to) python because later I need the flexibility of sending only select bytes of the binary file and programming delays between sent bytes, amongst other things.
 
.tar file is another package, like zip or gz but not compressed. the gz was to compress the tar :)

You can extract tar packages in windows with 7-zip, and possibly many others but this is what I've been using: 7-zip.org/Download



Oh FFS. I downloaded 7-zip last night to unpack the .gz, as it isn't a recognized windows format. Now with a fresh head I've unpacked the .tar and all of the files that are supposed to be there are so.

Why not distribute windows files in the standard windows compressed file format instead of bundling twice with two propriety formats? It's only several hundred kilobytes FFS. The propeller-heads have gone mental here.

Anyway, I'm still no closer to running pyserial. I'm following the instructions to the letter, but all I keep getting is "SyntaxError: invalid syntax".
python setup.py install isn't working as a recognized command. Pip is part of Python 3.8, but not a single command to invoke it is working for me - I just get the syntax error to absolutely everything.

To make the commands for changing and printing the working directory operational I had to import os. Is there some other thing that needs to be imported before all of the Pip and install operations can work? I sure as hell can't find the answer.


And Python 3.8 by default installs itself into the hidden location at C:\Users\<username>\AppData.
Why? The location is supposed to be reserved only for "custom settings and other information needed by applications", not the f%$ing applications themselves.

The installation has somehow managed to corrupt something in AppData and OneDrive is now borked, throwing up error messages and no longer operable.
Thank you Python.
 
Last edited:
The pip package manager needs to be run outside of Python, not inside the Python shell!

I can't help you with all the Windows grumble, though. Except maybe ditch Windoze and change to Linux, but I guess that's not (yet) your piece of cake.

I guess diyAudio isn't quite the right place for this. I'd say stackoverflow et al. would be more suitable for your question.
 
Last edited:
The pip package manager needs to be run outside of Python, not inside the Python shell!
Just logged on to say I've figured that out (that goes for "python pyserial install" as well).
I was using the IDLE Shell and just assumed everything was supposed to be run from that (none of the basic instructions that I was following explicitly stated otherwise).

I couldn't get python operational in Command Prompt because by default python does not add itself into the list of Windows path variables - that's a check box that you have to manually enable when initially installing.The quickest way to fix that problem was to just uninstall python and reinstall. This is kinda important to know beforehand! Argh!

Also fixed the stupid issue of python plonking itself into the hidden AppData folder - when installing python, elect to do the custom installation and check "install for all users". Python then installs into C:\program files - much more sensible and manageable.

I now have pyserial installed and running (Command Prompt as Administrator) and am exactly where I need to be.
 
Last edited:
Status
This old topic is closed. If you want to reopen this topic, contact a moderator using the "Report Post" button.