Following Code works in python interpreter but not as a script - python

I just made a tiny code to change the colors of my led strip in Linux too (I already did it in C# on Windows).
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write(b'a')
When I type this into bash like this:
$sudo python2
>>>import serial
>>>ser = serial.Serial('/dev/ttyACM0', 9600)
>>>ser.write(b'a')
1
it's working fine, but if I then execute the .py script like this:
$sudo python2 light.py
The ser.write part seems not to work. I dont get an err msg or anything. But I know that it's communicatin with the arduino cause the Onboard LED flashes when I execute the script.

Okay, got it, the Arduino resets after getting serial input, I just added a 3 Seconds delay before writing the Serial data

Related

What is the "send" button on the Arduino serial monitor doing?

This has been resolved!!
I have to wait a few seconds after opening serial port.
I want to execute the python program that is being done with the send button on the Arduino serial monitor.
if (Serial.available() > 0){
Serial.print(hoge);
}
is written in arduino, and I want to make Serial.available ()> 0 by python program.
I tried...
1.
If I send something like A or 3 on the IDE serial monitor, the contents of hoge will be output on the serial monitor.
2.
Using pyserial
ser = serial.Serial('/dev/ttyACM0', 115200,timeout=None)
ser.write(str.encode('A'))
data = ser.readline()
print(data)
When this is executed, it waits for reception before ser.read ().
After deleting the if (Serial.available ()> 0) of the program on Arduino and executing it, the contents of hoge were printed properly on the terminal.
The contents of hoge are
b'0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\r\n'
What do I need to write to do the same thing as 'send'? How do I get Serial.available ()> 0 ...?
It would be very helpful if you could tell someone.
The first data of 'hoge[]' is NULL character. Therefore there's no chance for 'Serial.print()' function to print out the whole content.
Serial.print(hoge) immediately returns because the first character is NULL character.
ser.readline() is waiting forever because there's no newline incoming from Arduino.
Simply changing the content will solve this issue or use write() function (but if you keep the same content then you need to adapt your python code as well without using readline()).
In your python code, you need to add "time.sleep()" before "ser.write()" so that Arduino is ready to receive the serial data. More than 1 seconds would be required.

Why doesn't pyserial write on Linux?

I've coded a simple script for Windows that works fine and I have adapted it to Linux (Ubuntu). The problem is that it doesn't read the byte sent.
I tried all the different serial ports available according to the Arduino IDE but the problem persists.I also used \n and \r without success and different encodings.
Code working on win10:
import serial
import time
import keyboard
arduino = serial.Serial('COM4', 9600, timeout=0)
while True:
arduino.write('a'.encode())
time.sleep(0.1)
print(arduino.readline())
Code not working on Ubuntu:
import serial, time
arduino = serial.Serial('/dev/ttyAMC0', 9600, timeout = 0)
while True:
arduino.write('a'.encode())
time.sleep(0.1)
print(arduino.readline())
So the first script prints continuously a\r\n, the second doesn't. Simply shows b'' continuously. So I think it doesn't simply write the letter.
Solved. No idea what exactly was thr issue but worked sending capital letter.

Pyserial Com3 Port no output? Windows 7

Im trying to read data from a simple COM3 USB Serial Port with PySerial.
My Code is as follows:
import serial
import time
ser = serial.Serial("COM3", 230400, timeout=2) #Also tried timeout=None, loads to infinity then
print (ser)
while True:
line = ser.readline() #also tried read()
print(line)
time.sleep(1)
Console Outupt:
print(ser)
=>Serial<id=0x2757c50, open=True>(port='COM3', baudrate=230400, bytesize=8, parity='N', stopbits=1, timeout=2, xonxoff=False, rtscts=False, dsrdtr=False)
I tried to mess around with the boolean-attributes of the Serial object ser(e.g. xonxoff) with no success
print(line)
=> empty string or b''
I tried to test this in Python 2.7 and Python 3.6.5.
Listing all COM Ports via serial.tools.list_ports.comports() returns this:
COM3 - USB Serial Port (COM3)
COM46 - com0com - serial port emulator CNCA0 (COM46)
COM47 - com0com - serial port emulator CNCB0 (COM47)
When I use the program HDTerm I get the following output:
Screenshot_HDTerm_COM3_Port_Output
So the Port actually returns something, but I can't read the data from python somehow. The parameters are the same in my Python Code as they are in HDTerm.
Any idea why Python is printing nothing (or loading to infinity if timeout=None)?
PS.: COM46 & COM47 are working perfectly fine in Python and HDTerm. But they have other outputs (sending and receiving data from each other only).
I solved the problem by myself now.
I had to do ser.dtr=False
Even though print(ser)showed that the parameter dsrdtr=False (which I interprete as "dsr and dtr") the attributeset.dtr remained to be True (test with print(set.dtr).
You have to manually set dtr to False as shown above. Seems a bit odd to me, maybe a bugg?
if i get you right.
try
type(ser)
that way you will know the type of 'ser', if it's an iterable(list, tuple,etc...), you can try:
for s in ser:
print s
time.sleep(1)
hope this help.

Python Serial() not etablishing on start up

I've made a python script to read information from Serial port. I want this script to run at the boot of my ubuntu.
I've made my python file executable, and added a lign to the crontab : #reboot sleep 10s; python /home/MyUser/Documents/MyScript.py &
The script is indeed started when i'm booting but it's seems like the specific line ser = serial.Serial('/dev/ttyACM0', 9600) won't work. Indeed, if I try to execute something before, it will work, but not after.
I can't figure out why it's not working. When i'm executing my script from the terminal, it does work without problem.
I believe it may be because of the fact that the serial port may not be ready yet when the script start but even when adding a longer sleep before the script start, it doesn't work. I'm running out of ideas and I really need your help.
Here is my python script :
#!/usr/bin/env python
import serial
import subprocess
import os
import time
ser = serial.Serial('/dev/ttyACM0', 9600)
os.system('export DISPLAY=:0 && xset dpms force off &') #this line won't work, that's why I know the problem is coming from the serial beginning.
time.sleep(60)
urgency = 0
diffusion = 0
while 1 :
line = ser.readline().strip()
if line == "start":
#do something
else
#do something else

Disable DTR when opening the serial port from pyserial running on a Mac

I have the following code:
import sys,serial
ser = serial.Serial()
ser.baudrate=57600
ser.port = sys.argv[1]
ser.dsrdtr = True
ser.open();
ser.setDTR(level=False)
print ser.readline()
The thing is that my Arduino UNO receives a DTR and restarts, how can I disable this (in software)? My python code is running from a Mac mini with a usb connection to my UNO.
(I'm fully aware of this but hardware is not an option for me)
I'm having similar issues, but haven't been able to find a solution for a while. Looks like this is possible on Windows with some hackery, but the issue lies deeper on posix.
Ideally you should be able to setDTR before you open the connection. Like this:
import sys,serial
ser = serial.Serial()
ser.baudrate=57600
ser.port = sys.argv[1]
ser.dsrdtr = True
ser.setDTR(level=False)
ser.open();
print ser.readline()
But that throws a portNotOpenError in serialposix.py:
def setDTR(self,on=1):
"""set terminal status line"""
if not self.fd: raise portNotOpenError
if on:
fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
else:
fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_DTR_str)
I took a dive into serialposix.py, and you'll see where the root issue lies. That self.fd defined above is actually:
self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK)
If you write a little script that opens your device using os.open(device, flags), you'll see it resets, even if you open it as read only with the flag os.O_RDONLY.
Digging deeper into the meaning of the os.open flags- we find that the open command actually wraps the unix command open(2). The man pages are here.
Let me know if you ever find a more satisfactory solution.

Categories

Resources