array's confusion
Hello everyone
Last edited by RND_pi on Tue Dec 31, 2019 7:24 am, edited 1 time in total.
Re: array's confusion
It looks as though you have a generic problem with your understanding of the Python programming language, rather than anything specific to the Raspberry Pi.
Your problem may stem from the fact that you are creating a Python list but calling it 'array'.
Strictly speaking, Python does not have built in support for an array as a data type.
See, for example: https://www.w3schools.com/python/python_arrays.asp
Your problem may stem from the fact that you are creating a Python list but calling it 'array'.
Strictly speaking, Python does not have built in support for an array as a data type.
See, for example: https://www.w3schools.com/python/python_arrays.asp
Re: array's confusion
I am not entirely sure what you are trying to get but here's an example..which will give an output like addr0:FYURJANUARY
Code: Select all
import serial
rx_data = []
addrs = [b'\x00',b'\x01',b'\x02',b'\x03',b'\x04',b'\x05',b'\x06',b'\x07',b'\x08',b'\x09',b'\x0A',b'\x0B',b'\x0C',b'\x0D']
start = b'\xb2'
Stop = b'\xA3'
stop = b'\xa3'
ser = serial.Serial("/dev/ttyS0")
ser.baudrate = 1200
while True:
rxd_bytes = ser.read()
rx_data.append(rxd_bytes)
# check for end
if rxd_bytes == stop:
# check valid
if rx_data[0] == start and (rx_data[14] == stop or rx_data[14] == Stop):
Data_out = ""
# check address
for x in range(0,14):
if(rx_data[1] == addrs[x]):
address = "addr" + str(x)
Data_out +=address + ":"
# decode data
for x in range(2,13):
Data = rx_data[x].decode("ascii","ignore")
Data_out += Data
print (Data_out)
ser.close()
Re: array's confusion
I am not sure exactly what you want but your array contains python "bytes" which are kind of a "type of string", that's why when you print them some appear as hex numbers and some appear as letters. Using Python3 these are two ways you can convert the bytes to hexadecimal numbers as strings or as integers :
Let us know if you need more ...
Code: Select all
>>>a = [b'\xb2', b'\x00', b'F', b'Y', b'U', b'R', b'J', b'A', b'N', b'U', b'A', b'R', b'Y', b' ', b'\xa3']
>>>b = [b'\xb2', b'\x01', b'F', b'R', b'I', b' ', b' ', b'W', b'A', b'W', b'A', b'L', b' ', b' ', b'\xa3']
>>>c = [b'\xb2', b'\x02', b'?', b'6', b'4', b'5', b'?', b'3', b'4', b'5', b'?', b'4', b'1', b'7', b'\xa3']
>>> b"".join(a) # if you want to see the text, \xnn for bytes that are not text characters
b'\xb2\x00FYURJANUARY \xa3'
>>> b"".join(b)
b'\xb2\x01FRI WAWAL \xa3'
>>> b"".join(c)
b'\xb2\x02?645?345?417\xa3'
Re: array's confusion
Thanks for the help
Last edited by RND_pi on Tue Dec 31, 2019 7:25 am, edited 1 time in total.
Re: array's confusion
another help would required on rs485. im getting data from rs485 and the data im getting received is continuous and byte by byte and the order of the data im receiving is random like any address is coming on my pyshell. my concern is that how should i store the incoming data and then parse the data according to the addresses which i've posted in the above.
thanks in advance
thanks in advance
Re: array's confusion
Try this..
I have it working on data from an Arduino. Note I can't find a way to detect \x00 as python seems to see it as a control and filter it out, so it assumes if no valid address found it must be 00.
It starts by finding address 00 (no address!) and ends with address 0D, then repeats. It can cope with addresses out of order BUT if 00 isn't the start and 0D the end you won't get a full set.
For RS485 I believe you will need a RS485 HAT.
I have it working on data from an Arduino. Note I can't find a way to detect \x00 as python seems to see it as a control and filter it out, so it assumes if no valid address found it must be 00.
It starts by finding address 00 (no address!) and ends with address 0D, then repeats. It can cope with addresses out of order BUT if 00 isn't the start and 0D the end you won't get a full set.
For RS485 I believe you will need a RS485 HAT.
Code: Select all
#!/usr/bin/python3
import tkinter as tk
import serial
import os
import time
class MainApplication(tk.Frame):
def __init__(self, master):
self.master = master
tk.Frame.__init__(self, self.master)
self.dataure_gui()
self.create_widgets()
self.start_serial()
self.rx_data()
def dataure_gui(self):
self.master.title('Data Input Form')
self.master.geometry('300x400')
self.master.resizable(0, 0)
def create_widgets(self):
self.create_frames()
self.create_entries()
self.create_labels()
def create_frames(self):
self.Frame10 = tk.Frame(width=300, height=400)
self.Frame10.grid_propagate(0)
self.Frame10.grid(row=0, column=0)
def create_entries(self):
global E0,E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13
E0=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E1=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E2=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E3=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E4=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E5=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E6=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E7=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E8=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E9=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E10=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E11=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E12=tk.Label(self.Frame10, height=1, width=20, bg = "white")
E13=tk.Label(self.Frame10, height=1, width=20, bg = "white")
self.keys = [E0,E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13]
for a in range(0,14):
self.keys[a].place(x=100, y= (a * 25) + 25)
def create_labels(self):
L1 = tk.Label(self.Frame10, text="addr00 :" , font = 30)
L2 = tk.Label(self.Frame10, text="addr01 :" , font = 30)
L3 = tk.Label(self.Frame10, text="addr02:" , font = 30)
L4 = tk.Label(self.Frame10, text="addr03 :" , font = 30)
L5 = tk.Label(self.Frame10, text="addr04 :" , font = 30)
L6 = tk.Label(self.Frame10, text="addr05 :" , font = 30)
L7 = tk.Label(self.Frame10, text="addr06:" , font = 30)
L8 = tk.Label(self.Frame10, text="addr07 :" , font = 30)
L9 = tk.Label(self.Frame10, text="addr08 :" , font = 30)
L10 = tk.Label(self.Frame10, text="addr09 :" , font = 30)
L11 = tk.Label(self.Frame10, text="addr10:" , font = 30)
L12 = tk.Label(self.Frame10, text="addr11 :" , font = 30)
L13 = tk.Label(self.Frame10, text="addr12 :" , font = 30)
L14 = tk.Label(self.Frame10, text="addr13 :" , font = 30)
labels = [L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12,L13,L14]
for a in range(0,14):
labels[a].place(x=20, y= (a * 25) + 25)
def start_serial(self):
global out_array,addrs,keys,start_stop
# setup serial
self.ser = serial.Serial(port="/dev/ttyS0",baudrate = 1200)
# define data
out_array = ['0','0','0','0','0','0','0','0','0','0','0','0','0','0']
addrs = [b'\x00',b'\x01',b'\x02',b'\x03',b'\x04',b'\x05',b'\x06',b'\x07',b'\x08',b'\x09',b'\x0A',b'\x0B',b'\x0C',b'\x0D']
start_stop = [b'\xb2',b'\xa3']
def rx_data(self):
global out_array,addrs,keys,start_stop
# define variables
array = []
begin = 0
end = 0
# start receiving and wait for 00 address (no address!)
while begin == 0:
rxd_bytes = self.ser.read()
while rxd_bytes != start_stop[0]:
rxd_bytes = self.ser.read()
array = []
array.append(rxd_bytes)
rxd_bytes = self.ser.read()
while rxd_bytes != start_stop[1] :
array.append(rxd_bytes)
rxd_bytes = self.ser.read()
if array[1] not in addrs:
begin = 1
Data_out = ""
Data_out += array[1].decode("ascii","ignore")
# parse data
for x in range(2,len(array)):
Data = array[x].decode("ascii","ignore")
Data_out += Data
# put in out_array
out_array[0] = Data_out
array = []
# continue receiving until the next b'\x0D' address.
while end == 0:
rxd_bytes = self.ser.read()
while rxd_bytes != start_stop[0]:
rxd_bytes = self.ser.read()
array.append(rxd_bytes)
rxd_bytes = self.ser.read()
while rxd_bytes != start_stop[1] :
array.append(rxd_bytes)
rxd_bytes = self.ser.read()
Data_out = ""
# determine address
address = 0
for x in range(0,14):
if(array[1] == addrs[x]):
address = x
if address == 0:
Data_out += array[1].decode("ascii","ignore")
# parse data
for x in range(2,len(array)):
Data = array[x].decode("ascii","ignore")
Data_out += Data
# put in out_array
out_array[address] = Data_out
# end
if array[1] == addrs[13]:
end = 1
array = []
# place on tkinter form from out_array
self.update_form()
self.after(1, self.rx_data)
def update_form(self):
global out_array
# place on tkinter form from out_array
for x in range(0,len(out_array)):
self.keys[x].config(text = out_array[x])
out_array = ['0','0','0','0','0','0','0','0','0','0','0','0','0','0']
if __name__ == '__main__':
root = tk.Tk()
main_app = MainApplication(root)
root.mainloop()
- Attachments
-
- form.jpg (37.19 KiB) Viewed 574 times
Re: array's confusion
any idea how i can add this address
I've try using
thanks in advance
into configuration file ?b'\x01
I've try using
but it didn't work.#b2
thanks in advance
Re: array's confusion
I need to add b'\x01' and b'\xb2' these 2 commands in my config.ini file.
but when im trying to do so im not able to read. any suggestions how should i add these 2 files in config file
but when im trying to do so im not able to read. any suggestions how should i add these 2 files in config file