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
  • 3

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
  • 62,573
  • 32
  • 113
  • 225
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,882
  • 3
  • 17
  • 22
vnc t.
  • 1
  • 1