Copy files from embedded Linux to Windows via serial cable? - python

I use Python 2.7/PySerial scripts to run tests on devices with an embedded Linux. Due to a recent software change, the Linux box generates a number of log files in .csv format. I need to fetch them. I can't enable any server features in the Linux; I only have a serial connection.
I can of course read the file content out and capture it as text, but this is clumsy and unreliable - I would rather copy the files. Two days of search, and I'm still clueless (Generic problem with me!).
Any hints, please? Please be gentle - this is my first question... :)

Once you get a serial terminal you can use sz (part of lrzsz) to send the files via ZModem. Simply use a serial comm program on the other side (Hyperterminal?) that understands ZModem and the files can be transferred over.

I thank you very much for the proposed solutions. Unfortunately, neither work (I can not enable anything extra on the Linux box), and they are both outside the desired Python environment.
I think it's a kludge, but i'll have to ask for a
cat logfile
as a text string, and attempt to catch the prompt at the end.
Thank you for your time and effort.

Related

Barcode scanners (Netum Scan) in python

I'm writing code which takes input from several barcode scanners, and I want a way to differentiate between each scanner. I'm on windows 10, and my python version is 3.11.1. And as the title suggests, I'm using scanners from a company called Netum.
I've tried using them as HID devices, but this seems incredibly slow and impractical. If the computer sees them each as keyboards, I have to validate each individual keystroke, rather than entire barcodes. So I've abandoned this approach under that assumption.
I discovered these scanners have what's called "USB COM Port Emulation," and that seems promising. It almost never works, but when it does, it just shoves an entire barcode into my code as a string. The problem is that, most of the time, when it's in this mode, it disconnects the USB dongle the instant I scan anything. But it makes a reference to "needing drivers" for this mode. I checked the device manager, which tells me all my drivers are totally up-to-date. This is really the crux of the problem, as my code works if this mode works reliably.
Also, I'm using PySerial to decode the inputs from the scanners. I dunno if that matters, but I figured I'd mention it. The following is the code I'm using to talk to these janky scanners, and it only works when the scanners don't disconnect from my PC for seemingly no reason.
import serial
scanner = serial.Serial(port='COM3', baudrate=9600, bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE)
string = scanner.read()
print(string.decode())
scanner.close()
If anyone has any advice, insights, or even just general directions to point me in, it would be appreciated. I'm at the point where I don't even know how to begin solving this.
Comment to answer
Have you installed any Virtual Com Port (VCP) drivers such as from FTDI? ftdichip.com/drivers/vcp-drivers They do a executable for easy install - this may help with your stability issues

write text from terminal of python code to a txt file?

I made a simple project in python that pings a server every few seconds and I want to store the ping data in a .txt file. (It might also be cool to put it in a GUI but I need it in a txt file for now). Also, it just shows the ping in the terminal so I have no idea how I would make it go into a txt because I'm new at coding.
(here's my code btw)
import os
import time
while 1:
os.system('ping 1.1.1.1 -n 1')
time.sleep(5)
I didn't try much because I couldn't figure out anything I looked up stuff and nothing was what I wanted.
(also I'm a noob at coding anyways)
You'll have to run your code with Popen instead of os.system (which is a bad idea in most cases, anyway, for security reasons).
With Popen (python.org -> documentation is your friend!) you can capture the output of the programs you run. You can then write that to a file object. (That's a built-in type in python. Again, official documentation on this is good and comes with examples!)
I honestly don't see a reason to write the results of ping to a file. Wouldn't you just care about whether that ping worked and was reasonably fast? Maybe extract that information instead and just log it instead!

Python 2.7 keeping console in the background open

At the begining i would like to state that i did look for an answer before posting my question, but if i missed anything I'm really sorry.
Ok to the point.
I'm trying to create a tool that will monitor behaviour of my 2 external devices comunicating over BT(communication over BT i have pretty much solved). but what i'm strugling with is monitoring them.
So Manually i open cmdline 2 times and from there i use putty to connect to devices and do stuff.
Now I want(and pretty much need) to do the manual part in python. So i tried using subprocess.Popen to connect to cmdline(and from there to putty) but the problem is that this only works as request/response. what i need is to open (and keep) cmdline streamlike connection and pass and receive commands/response without closing.
P.S. I'm using windows enviroment and python 2.7.
Thank You for any response.
Kind Regards.

Python: Running Daemon Processes in Windows7

I had a program that Scraped certain data from certain Web-Pages, and when the Web-Pages changed, acted accordingly.
How would one set up the program so it continues to run in the background?
I don't need any specifics
I'm just really confused on this concept and would appreciate whatever help anybody has to offer.
start path-to-pythonw.exe your-code.py
pythonw means without console.
start means start on background.
if your python is installed system-wide, you can probably start your-code.pyw
.pyw is associated with pythonw.exe
remember you cannot use print (to stdout) in this case.
If you want to be able to just start your process and have it background itself and do a few more typical things that "daemon" processes do in Unix, look here: How do you create a daemon in Python?
There is no concept of "background" in Windows. But the UNIX shell concept of a background process can be reasonably emulated by running your Python script as a Windows service. There are a couple of suggestions in this question: Is it possible to run a Python script as a service in Windows? If possible, how?
For casual use, I suggest that you learn how to use srvany from the second answer.
You simply need to leave your program running! Please google "python daemon" and see how to implement a persistent background process in Python.
Now, you cannot know when a website changes unless you poll it. If the website is well designed, the page you are trying to poll will have a "Last-Modified" header, you can make a "HEAD" request every so often (be nice: don't poll like crazy) and act when Last-Modified is >= than the one on record. If the site is not well designed, it will not have a reliable Last-Modified or ETAG header, in that case you will have to parse manually and check for changes yourself.
Cheers.

Using Python, how do I close a file in use by another user over a network?

I have a program that creates a bunch of movie files. I runs as a cron job and every time it runs the movies from the previous iteration are moved to a 'previous' folder so that there is always a previous version to look at.
These movie files are accessed across a network by various users and that's where I'm running into a problem.
When the script runs and tries to move the files it throws a resource busy error because the files are open by various users. Is there a way in Python to force close these files before I attempt to move them?
Further clarification:
JMax is correct when he mentions it is server level problem. I can access our windows server through Administrative Tools > Computer Management > Shared Folders > Open Files and manually close the files there, but I am wondering whether there is a Python equivalent which will achieve the same result.
something like this:
try:
shutil.move(src, dst)
except OSError:
# Close src file on all machines that are currently accessing it and try again.
This question has nothing to do with Python, and everything to do with the particular operating system and file system you're using. Could you please provide these details?
At least in Windows you can use Sysinternals Handle to force a particular handle to a file to be closed. Especially as this file is opened by another user over a network this operation is extremely destabilising and will probably render the network connection subsequently useless. You're looking for the "-c" command-line argument, where the documentation reads:
Closes the specified handle (interpreted as a hexadecimal number). You
must specify the process by its PID.
WARNING: Closing handles can cause application or system instability.
And if you're force-closing a file mounted over Samba in Linux, speaking from experience this is an excruciating experience in futility. However, others have tried with mixed success; see Force a Samba process to close a file.
As far as I know you have to end the processes which access the file. At least on Windows
The .close() method doesn't work on your object file?
See dive into Python for more information on file objects
[EDIT] I've re-read your question. Your problem is that users do open the same file from the network and you want them to close the file? But can you access to their OS?
[EDIT2] The problem is more on a server level to disconnect the user that access the file. See this example for Windows servers.

Categories