-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrs482.py
More file actions
30 lines (29 loc) · 1.01 KB
/
rs482.py
File metadata and controls
30 lines (29 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import RPi.GPIO as GPIO
import time
import serial
GPIO.setmode(GPIO.BOARD)
GPIO.setup(33, GPIO.OUT) #RS 485 Enable Pin
# Serial Port Settings
rs_485_ser = serial.Serial(
port='/dev/ttyS0', # Raspberry Pi's serial port's name
baudrate=9600, # Connection Speed (Baudrate)
timeout=1 # Timeout Settings
)
#RS485 Data
data = bytes([0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09]) # Total active energy
try:
GPIO.output(33, GPIO.HIGH)
rs_485_ser.write(data)
time.sleep(0.01)
GPIO.output(33, GPIO.LOW)
rs_485_ser.reset_output_buffer()
rs_485_ser.reset_input_buffer()
response = rs_485_ser.readline()
if response:
byte_array = bytearray(response) #Response is coming byte by byte, so we have to convert to array to compare datas
byte_list = list(byte_array)
print("Response :")
for i in range (len(byte_list)):
print (i+1,". byte is = ",byte_list[i])
except Exception as e:
print("RS485 an error occurred:", e)