0

I have to combine two codes and run them one after the other. Unfortunately, I can't start the gps_receiver on Thonny_ide and neighter the other code on the default Geany_ide.

Can somebody help me to solve the problem? Here's my code:

GPS_Receiver (uses the waveshare gps head)

import serial
import time

# Enable Serial Communication
port = serial.Serial("../../dev/ttyS0", baudrate=115200, timeout=1)
# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key

time.sleep(.01)

# Activate Command line
port.write("AT \r\n" .encode('utf-8'))
rcv = port.read(100)
print(rcv)
time.sleep(.01)

# Power On the gps module
port.write("AT+CGNSPWR=1 \r\n" .encode('utf-8'))
rcv = port.read(100)
print(rcv)
time.sleep(.01)

#Set the baud rate of GPS
port.write("AT+CGNSIPR=115200 \r\n" .encode('utf-8'))
rcv = port.read(100)
print(rcv)
time.sleep(.10)

chk = False
search_for = '3D'
while chk == False:

    port.write("AT+CGPSSTATUS? \r\n" .encode('utf-8'))
    rcv = port.read(100)
    print(rcv)

    is_in = search_for in rcv
    if is_in == True:
        chk = True
        break

print(chk)    
print(rcv)


#Send Data received to UART
port.write("AT+CGNSTST=1 \r\n" .encode('utf-8'))
#rcv = port.read(100)
print(rcv)
time.sleep(2.5)

port.write("AT+CGNSTST=0 \r\n" .encode('utf-8'))


#Print the GPS information
port.write("AT+CGNSINF \r\n" .encode('utf-8'))
rcv += port.read(200)
print(rcv)
time.sleep(.1)

data_raw = rcv.split(',')
print(data_raw)

index_N = data_raw.index('N')
north_str = data_raw[index_N - 1]
north_float = float(north_str)/100

index_E = data_raw.index('E')
east_str = data_raw[index_E - 1]
east_float = float(east_str)/100


location_N = str(north_float)
location_E = str(east_float)
altitude = str(data_raw[9])
satelites = str(data_raw[7])
print('location: ' + location_N + ', ' + location_E +', altitude: ' +altitude+ ', satelites: ' + satelites)
data_list = [location_N, location_E, altitude, satelites]
print(data_list)



#Stopp sending Data received to UART
port.write("AT+CGNSTST=0 \r\n" .encode('utf-8'))
#rcv = port.read(100)
#print(rcv)
#time.sleep(.5)

Error, when run on the Geany's Programing Editor

b'AT \r\r\nOK\r\n'
b'AT+CGNSPWR=1 \r\r\nOK\r\n'
b'AT+CGNSIPR=115200 \r\r\nOK\r\n'
b'AT+CGPSSTATUS? \r\r\n+CGPSSTATUS: Location 3D Fix\r\n\r\nOK\r\n'
Traceback (most recent call last):
  File "/home/pi/GPS_final.py", line 38, in <module>
    is_in = search_for in rcv

TypeError: a bytes-like object is required, not 'str'

QR, RFID Receiver (Input from RIFD Reader comes as a keyboard input):

# import packages gereral information
from datetime import date
from datetime import datetime
import time
import socket

# import packages kamera
import cv2
from pyzbar import pyzbar
import imutils
from imutils.video import VideoStream
import time

# import packages gps
import serial
import time

breakout_command = 'e'                          # ends collecting infomation and sends the information to the database
device_id = socket.gethostname()                # default device id
reader_connected = True                         # If reader is connected variable set True

information_list = []                           # Create a list where information can be stored

data_qr = {}

data_rfid = {}

def read_qr_codes():
    try:

        # initialize video stream and wait
        vs = VideoStream( usePiCamera = True ).start()
        time.sleep(2.0)
        # loop over frames
        while True:
            frame = vs.read()
            # for better performance, resize the image
            frame = imutils.resize(frame, width=400)
            # find and decode all barcodes in this frame
            barcodes = pyzbar.decode(frame)
            for barcode in barcodes:
                #do anything with that data
                print('Code \"' + barcode.data.decode("utf-8") + '\" detected')
                tag_id_qr = barcode.data.decode("utf-8")    
                id_qr = {
                'tag_id_qr' : str(tag_id_qr),
                'device_id' : str(device_id),
                'date' : str(time.strftime("%d.%m.%Y %H:%M:%S"))
                }

                #data_qr.update(id_qr)
                information_list.append(id_qr)
    except KeyboardInterrupt:
        pass            


def read_rfid_codes():
    while reader_connected == True:                 # Check if reader is connected
        tag_id_rfid = input('Receive id from tag')       # Input is expected
        if (tag_id_rfid == breakout_command):
            break
        id_rfid = {
           'tag_id_rfid' : str(tag_id_rfid),
           'device_id' : str(device_id),
           'date' : str(time.strftime("%d.%m.%Y %H:%M:%S"))
            #'GPS' : str(gps),
        }
        #data_rfid.update(id_rfid)
        information_list.append(id_rfid)
        #print(information_list)


read_qr_codes()
read_rfid_codes()
#get_gps_location()

information_list_cleared = []
for e in information_list:
    if e not in information_list_cleared:
        information_list_cleared.append(e)

print(information_list_cleared)

Error, when run in Geany's Programing Editor:

The Kamera doesn't detect the qr codes or doesn't write it in the terminal. And when pressed 'Ctrl+C' on keyboard the whole program shuts down.

In the best case I would like to run the whole prgramm in Thonny. So basicly just insert and activate the gps funktion in it.

Thanks!!

  • 1
    In normal use your programs should be run from the command line - not within an IDE. What happens if you run each program from the command line? eg python3 file.py – CoderMike Jan 9 at 9:41
  • 1
    Yes, I was also very satisfied with thonny and I'm goining to stay with that. – Philipp Jan 9 at 10:02
  • 1
    @CaderMike I ran both programs on the command line and the results are equal to thonny. The Tracking Software for the qr codes and rfid works fine and the GPS_Code throws the same error – Philipp Jan 9 at 10:03
  • 1
    Thus, I would like to change the GPS.py code in a way that it works and that I can just implement it as a parallel function in the tracking software - Any ideas? – Philipp Jan 9 at 10:04
  • 2
    Sounds like geany may be running code as python2. Try adding .decode() to your .read() in your GPS_Code so .read().decode() Then try running from command line: python3 GPS_Code.py – CoderMike Jan 9 at 11:30
3

By default Geany runs Python code using Python2. This can be changed under 'Build','Set Build Commands'. Then change Compile from python to python3 and Execute from python to python3.

The serial read commands changed between Python2 (returns string) and Python3 (returns bytes), adding a .decode() converts bytes to string.

If not sure which version of Python is being used add:

import sys
print(sys.version)
| improve this answer | |
  • Oh my goodness. No wonder some of the python programs I stole could run in Geany but not in Thonny, and sometimes vice versa. I was afraid to ask the owner, so my get around is by trials and errors. I did not go deep in Geany, so I did not notice the 'set build' commands, Even I noticed that I dared not to try to build anything, because I once I made a wrong build for Python 3.6, when default was 3.5.3, and made a big mess and had to reflash a new SD card image. – tlfong01 Jan 9 at 13:26
  • I still remember it took me a hour to find where was the newly built Python 3.6, bacause I did not know what is the meaning of "symbolic link" and other similar weird liunx stuff.. – tlfong01 Jan 9 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.