Pyserial - Embedded Systems - python

I am not expecting a code here, but rather to pick up on knowledge of the folks out there.
I have a python code - which uses pyserial for serial communication with an Micro Controller Unit(MCU). My MCU is 128byte RAM and has internal memory. I use ser.write command to write to the MCU and MCU responds with data - I read it using ser.read command.
The question here is - It is working excellently until last week. Since yesterday - I am able to do the serial communication only in the morning of the day. After a while, when I read the data the MCU responds with"NONE" message. I read the data next day, it works fine. Strange thing is - I have Hyperterminal installed and it properly communicates with the MCU and reads the data. So I was hoping if anyone have faced this problem before.
I am using threads in my python program - Just to check if running the program mulitple times with threads is causing the problem. To my knowledge, threads should only effect the Memory of my PC and not the MCU.
I rebooted my Computer and also the MCU and I still have this problem.
Note: Pycharm is giving me the answers I mentioned in the question. If I do the same thing in IDLE - it is giving me completely different answers

So, ultimately you're looking for advice on how to debug this sort of time dependent problem.
Somehow, state is getting created somewhere in your computer, your python process, or the microcontroller that affects things. (It's also theoretically possible that an external environmental factor is affecting things. As an example, if your microcontroller has a realtime clock, you could actually have a time-of-day dependent bug. That seems less likely than other possibilities).
First, try restarting your python program. If that fixes things, then you know some state is created inside python or your program that causes the problem.
Update your question with this information.
If that doesn't fix it, try rebooting your computer. If that fixes things, then you strongly suspect some state in your computer is affecting things.
If none of that works, try rebooting the micro controller. If rebooting both the PC and the micro controller doesn't fix things, include that in your question as it is very interesting data.
Examples of state that can get created:
flow control. The micro controller could be sending xoff, clearing clear-to-send or otherwise indicating it does not want data
Flow control in the other direction: your PC could be sending xoff, clearing request-to-send or otherwise indicating that it doesn't want data
Your program gets pyserial into a confused state--either because of a bug in your code or pyserial.
Serial port configuration--the serial port settings could be getting messed up.
Hyper terminal could do various things to clear flow control state or reconfigure the serial port.
If restarting python doesn't fix the problem, threading is very unlikely to be your issue. If restarting python fixes the problem threading may be an issue.

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

Is there a way to use python to automatically attach and detatch a usb connected device when conditions are met?

I am running my dad's busted old laptop (4gb ram) and controlling it remotely. I plan to leave it running to host a filezilla server and pihole, as well as anything else that could be useful.
It's running Linux mint XFCE, if that's helpful.
There's an issue - it gets hot. It's why the device was retired. The fan still runs though. I've improved it a lot by replacing the thermal paste, putting on a lighter OS (see above) and removing unnecessary things like the keyboard and screen, but it's still running hotter then I'd like.
I also have a usb connected laptop cooling pad. It turns on as soon as it is plugged in. The pad is enough to fix the issue.
This is where my question comes in. Would it be possible to write some code to automatically turn the cooling pad on when the laptop reaches a certain temperature and turn it off when it isn't needed?
My coding skills are basic. I can write things like if/then code in python but I cannot reliably create GUIs or access dictionaries.

PyCharm: After each word is typed, there is a delay where one is unable to type

In PyCharm (JetBrains), I have been having trouble with typing full statements without getting an interuption. I first thought it was due to me not having updated the software, so I updated it, but the problem remains.
So if I type any statement or word, PyCharm seems to delay before I can proceed. An example:
import csv
Even before I finish typing "import" - if I delay a keystroke - PyCharm begins to "think" and the window is not accessible for about one to two seconds (quite literally). I assume it is going to give me suggestions or show a tip/error about the code.
Any thoughts to prevent this from happening?
Edit:
Windows 8.1; PyCharm 2016.2
Code Complete turned off via Settings->Editor->General->Code Completion, but did not solve problem.
Key PC Spec:
Intel Core i5-337U
4GB Ram
64-bit
Edit2:
I receive this error when I run anything now, including simply print("test"):
Process finished with exit code -1073741511 (0xC0000139)
Will separate the question somewhere else, since this may be a separate problem altogether.
Try disabling code completion. I believe that your computer can't search through all of Python's librarys fast enough so it freezes for a bit.

Raspberry PI: Unable to handle kernel paging request at virtual address

I am having an issue with a Python script that is running on a Raspberry Pi. Frankly, the script initially runs perfectly fine and then after a certain period of time (typically >1 hour) the computer either freezes or shuts down. I am not sure if this is a software or a hardware issue. The only clue I have so far is the following error message that appeared one time when the computer froze:
[9798.371860] Unable to handle kernel paging request at virtual address e50b405c
How should this message be interpreted? What could be a good way of keep debugging the code? Any help is relevant since I am fairly new to programming and have run out of ideas on how to troubleshoot this issue..
Here is also some background to what the Python code intends to do (not sure if it makes a difference though). In short, every other second it registers the temperature through a sensor, creates a JSON file and saves it, sends this JSON object through cURL (urllib) to a web API, receives a new JSON file, changes switches based on the data in this file, sleeps for 2 seconds and repeats this process.
Thanks!

Is there a cross platform method of dealing with Sleep mode in Python?

I have a Python application that regularly dies when the computer goes to sleep. It works well on Mac, Windows and Linux on it's own, but the sleep problem is irritating.
Is there a function/library that would let me deal with sleep mode in a platform independant way?
Something like
if(OSSLEEP){
#Reduced Functionality
}
I would be happy if the program simply didn't crash, as I realize it is probable that no functionality is possible during sleep.
I researched the question:
How can I check to see if system is in standby mode?
It seems there is a Win32 API function which does this: By this reasoning there might be a Python library that does the same thing.
Notification when Windows enters in sleep mode
This seems to say it's not possible and a bad idea:
Multithreaded python urllib2-based downloader drives the computer to Standby/Sleep
I would love to know if this is do-able even if it's not advisable.
During sleep mode, no program is running. That's kind of the point, since modern CPUs use significantly virtually no power when not doing anything.
Additionally, your problem is not exclusive to the sleep mode. It will also occur if the connection drops out, or if your process doesn't get notified during hibernation, or if your network interface is restarted, or … .
So the correct way to solve your problem is: Ask a new question with your code and the error you're getting.

Categories