Python Forum

Full Version: Noob needing guidance....
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I keep getting issues trying to do a serial thing. I'm copying the lines from another script that kind of works okay.

Objective: Send serial modem commands to a Raspberry Pi Hat.

root@UPS-pico:/home/binary/GIT/Raspbery_PI_Scripts/Waveshare-GSM-GPRS-GNSS_HAT# python3.6 waveshare_init.py
Error:
waveshare_init.py:7: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(7, GPIO.IN) on Traceback (most recent call last): File "waveshare_init.py", line 20, in <module> ser.write(W_buff[0]) File "/usr/local/lib/python3.6/dist-packages/serial/serialposix.py", line 532, in write d = to_bytes(data) File "/usr/local/lib/python3.6/dist-packages/serial/serialutil.py", line 63, in to_bytes raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq)) TypeError: unicode strings are not supported, please encode to bytes: 'AT+CGNSSEQ="RMC"\r\n'
The script I'm working on.
import RPi.GPIO as GPIO
import serial
import time
ser = serial.Serial("/dev/ttyS0", 115200)

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)
GPIO.setup(7, GPIO.OUT)
GPIO.setwarnings(False)
pin7 = GPIO.input(7)
W_buff = ["AT+CGNSSEQ=\"RMC\"\r\n", "AT+CGNSINF\r\n"]

if pin7 == 1:
    pin7mode = "off"
    GPIO.output(7, GPIO.LOW)
    time.sleep(4)
elif pin7 == 0:
    pin7mode = "on"
print(pin7mode)
ser.write(W_buff[0])
ser.flushInput()


GPIO.cleanup()
The script that kind of works...

#!/usr/bin/python3.6
import serial
import time
ser = serial.Serial("/dev/ttyS0",115200)

W_buff = ["AT+CGNSPWR=1\r\n", "AT+CGNSSEQ=\"RMC\"\r\n","AT+CGNSINF\r\n", "AT+CGNSURC=2\r\n", "AT+CGNSTST=1\r\n"]
ser.write(W_buff[0])
ser.flushInput()
data = ""
num = 0

try:
	while True:
		while ser.inWaiting() > 0:
			data += ser.read(ser.inWaiting())
		if data != "":
			print(data)
			time.sleep(0.5)
			ser.write(W_buff[num+1])
			num =num +1
			if num == 4:
				time.sleep(0.5)
				ser.write(W_buff[4])
			data = ""
except keyboardInterrupt:
	if ser != None:
		ser.close()