Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noob needing guidance....
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  needing some help to write some code for a game calculator rymdaksel 1 404 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  Needing to Check Every Quarter Hour bill_z 10 2,987 Feb-09-2022, 07:23 PM
Last Post: menator01
  Need some coding guidance for a task peny 5 2,172 Sep-27-2021, 02:02 PM
Last Post: peny
  Your Guidance caslor 1 2,134 Mar-28-2021, 09:34 PM
Last Post: Larz60+
  Global Variables - Some Points Needing Clarification. adt 4 2,952 Nov-30-2019, 01:23 PM
Last Post: adt
  Beginner needing advice with data files JFI2019 2 2,195 Nov-06-2019, 04:56 PM
Last Post: JFI2019
  Typical beginner needing some help foxter00 1 2,650 May-08-2019, 11:46 PM
Last Post: michalmonday
  New user/beginner needing help oldDog 3 3,213 Apr-17-2018, 02:31 PM
Last Post: oldDog
  General Guidance in Project JMiller184 1 3,023 Oct-24-2017, 10:27 PM
Last Post: Larz60+
  Beginner needing help! Franco 2 3,233 Jul-29-2017, 12:56 PM
Last Post: Franco

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020