0

I'm trying to get a TTY to USB converter hooked up with my Raspberry Pi. I'm using Pyserial to setup the connection and write data to the port in a script called pc_test.py. For some reason the script is able to run but it won't write anything to the port and will not throw any errors. PuTTy will hang there until I hit control C, so I know it's doing something, just not the correct thing. And my TTY to USB converter has a little blue LED that flashes when data is read/written and this LED does not flash when this script runs. Also, it only runs if I type either "Python3 pc_test.py", "Python pc_test.py", and "Sudo Python pc_test.py"; however, if I try to run it with "Sudo Python3 pc_test.py" It gives an error saying:

Traceback (most recent call last): File "pc_test.py", line 1, in import serial ModuleNotFoundError: No module named 'serial'

I feel like this has to be something to do with the issue as I had very similar code running on a different Pi that I ran with sudo Python3. I did have some extreme difficulties installing pyserial this time for some reason. Simply typing "python3 -m pip install pyserial" would not work so I eventually got it by using the command:

python3 -m pip install --trusted-host=pypi.org --trusted-host=files.pythonhosted.org --user pyserial

Maybe this is what messed things up?

Here is the pc_test.py:

import serial
import RPi.GPIO as GPIO
import time,csv

ser=serial.Serial("/dev/ttyAMA0",9600) print(ser.name) while True: write_ser=ser.write("1".encode()) time.sleep(0.1)

LukeyP
  • 3
  • 2
  • Is your "TTY to USB" convertor compatible with 3.3V logic on the UART side? If not it may not work with the pi, and possibly has already damaged the serial interface pins on the pi. – Elliot Alderson Oct 24 '21 at 11:51

2 Answers2

1

It is far from clear what you have connected, what OS, what code you are actually using or how you installed modules.

No module named 'serial' means the code won't compile because you haven't installed it.

Normally you would install with sudo apt install python3-serial.

Even if you fix the code it won't work as /dev/ttyAMA0 is connected to Bluetooth on most Pi, but it certainly isn't "a TTY to USB converter".

See How do I make serial work on the Raspberry Pi3 or later

The following is a simple test program (not elegant python) that works on the Pi default serial port.

#! /usr/bin/env python3
import serial

ser=serial.Serial("/dev/serial0",115200) print(ser.name) ser.write('This is a test\r\n'.encode('ascii')) ser.close

Milliways
  • 59,890
  • 31
  • 101
  • 209
  • I'm facepalming myself right now. The setup guide I was using to install pyserial used "sudo apt install python-serial". I never once thought to add python3 instead of python to that command. That and using serial0 instead of ttyAMA0 worked. Thanks so much!!! – LukeyP Oct 24 '21 at 20:10
0

That's because sudo == root, you == pi.

You did --user on pip install so it installed on user pi not system or root.

If you want to keep the --user run sudo pip install or su pip and then do normal pip install, or just don't use --user and install it on system, system is always better.

MatsK
  • 2,791
  • 3
  • 16
  • 20
vnc t.
  • 1
  • 1
  • Ohhhhhh that makes total sense now. I guess thats what I get for blindly following guides without understanding them. Thank you! – LukeyP Oct 24 '21 at 20:11