Manipulasi data menggunakan pustaka Binascii Hexlify dan Unhexlify untuk komunikasi LORA menggunakan Grove LORA-E5 dan Circuitpython

Penghantar/Transmitter

import board import busio import digitalio import time import supervisor import binascii uart = busio.UART(board.D6, board.D7, baudrate=9600) get_input = True message_started = False message_print = [] allstring = "" printshow = False while True: if supervisor.runtime.serial_bytes_available: allstring="" b = "AT+TEST=TXLRPKT, \"3C"+bytes(binascii.hexlify(input().encode('utf-8'))).decode()+"3E\"" b = bytes(b,'utf-8') #userinput.type() #b = bytes(userinput, 'utf-8') #print(b) uart.write(bytes("AT+MODE=TEST",'utf-8')) time.sleep(2) uart.write(bytes("AT+TEST",'utf-8')) time.sleep(2) #print("Data Dalam bentuk 'byte' yang hendak dihantar") #print(b) uart.write(b) continue byte_read = uart.readline()# read up to 32 bytes if byte_read != None: allstring += byte_read.decode() printshow = True else: if printshow == True: if allstring != "": #print(allstring) left ='"3C' right ='3E"' try: dataRead =allstring[allstring.index(left)+len(left):allstring.index(right)] print("\nData yang dihantar dalam bentuk 'bytes'") print(b) print("\nData dalam bentuk HEX:") print(dataRead) print("\nTukar data dari Hex kepada bentuk 'utf8' atau 'ASCII': ") print(binascii.unhexlify(dataRead).decode('utf8')) #print(b) except ValueError: print(ValueError) pass allstring="" printshow ==False
Code language: PHP (php)

Penerima/Receiver

import board import busio import digitalio import time import supervisor import binascii import re uart = busio.UART(board.D0, board.D1, baudrate=9600) get_input = True message_started = False message_print = [] allstring = "" printshow = False while True: if supervisor.runtime.serial_bytes_available: allstring="" userinput = input().strip() b = bytes(userinput, 'utf-8') uart.write(b) continue byte_read = uart.readline()# read up to 32 bytes if byte_read != None: allstring += byte_read.decode() printshow = True else: if printshow == True: if allstring != "": print(allstring) left ='"3C' right ='3E"' try: b =allstring[allstring.index(left)+len(left):allstring.index(right)] print(binascii.unhexlify(b).decode('utf8')) #print(b) except ValueError: print(ValueError) pass allstring="" printshow ==False
Code language: PHP (php)

Komponen yang digunakan

Link Github di sini

Leave a Reply

Alamat e-mel anda tidak akan disiarkan.

Related Post