0

Python Serial
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Python Serial

by sashaT on Mon Jan 11, 2021 3:12 pm

Hi! I am running PySerial on a raspberry pi 3, and I want to communicate with a board. I have some old code for pyserial that I had written a while ago, and it runs perfectly. Then, I copy and paste it into a new file, and it gives errors.

Code: Select all | TOGGLE FULL SIZE
import time
import random
from datetime import datetime
import serial

comport = "/dev/ttyACM1"
baudrate = 115200
s = serial.Serial(comport, baudrate)



def update():
   

   
   
    days = str(31 - int(datetime.now().strftime('%d')))
    hours = str(23 - int(datetime.now().strftime('%H')))
    mins = str(59 - int(datetime.now().strftime('%M')))
    secs = str(59 - int(datetime.now().strftime('%S')))
    print(secs)
    out = days+"D "+hours+"H "+mins+"M "+secs+"S"
    s.write(out.encode())
   



while 1:
 update()
 time.sleep(1)
       
 

Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "/home/pi/serial2.py", line 4, in <module>
    import serial
  File "/home/pi/serial.py", line 6, in <module>
    ser = serial.Serial(s_name)
AttributeError: module 'serial' has no attribute 'Serial'

sashaT
 
Posts: 31
Joined: Thu Dec 24, 2020 1:06 pm

Re: Python Serial

by tannewt on Mon Jan 11, 2021 7:14 pm

Did you name one of your files `serial.py`? Python will import that first.

tannewt
 
Posts: 2034
Joined: Thu Oct 06, 2016 8:48 pm

Re: Python Serial

by sashaT on Mon Jan 11, 2021 8:23 pm

I don't think so. When I run "python -m serial.tools.miniterm", it gives me this error. The only thing is that I am using python 3.5, not 2.7. Maybe the "python" command is out of date?

Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 163, in _run_module_as_main
    mod_name, _Error)
  File "/usr/lib/python2.7/runpy.py", line 102, in _get_module_details
    loader = get_loader(mod_name)
  File "/usr/lib/python2.7/pkgutil.py", line 464, in get_loader
    return find_loader(fullname)
  File "/usr/lib/python2.7/pkgutil.py", line 474, in find_loader
    for importer in iter_importers(fullname):
  File "/usr/lib/python2.7/pkgutil.py", line 430, in iter_importers
    __import__(pkg)
  File "serial.py", line 6, in <module>
    ser = serial.Serial(s_name)
AttributeError: 'module' object has no attribute 'Serial'

sashaT
 
Posts: 31
Joined: Thu Dec 24, 2020 1:06 pm

Re: Python Serial

by sashaT on Mon Jan 11, 2021 8:25 pm

I deleted a "serial.py' in 'home/pi/' even though the script was in 'home/pi/RadioImager'. It worked!

sashaT
 
Posts: 31
Joined: Thu Dec 24, 2020 1:06 pm

Re: Python Serial

by sashaT on Mon Jan 11, 2021 8:32 pm

Thank you so much! I was writing a python application that used the librtlsdr library to connect to an antenna and turn it into a radio telescope, bring able to take images of neutral hydrogen emissions and more.

sashaT
 
Posts: 31
Joined: Thu Dec 24, 2020 1:06 pm

Please be positive and constructive with your questions and comments.