Skip to content

Commit 1ca731d

Browse files
Fixed Serial Communication and Got RFID in
1 parent e7e4f93 commit 1ca731d

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

electroblocks/core.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
class ElectroBlocks:
66

77
last_sense_data = ""
8+
verbose = False
89

9-
def __init__(self, baudrate=115200, timeout=2):
10+
def __init__(self, baudrate=9600, timeout=2, verbose = False):
1011
self.ser = self._auto_connect(baudrate, timeout)
12+
self.verbose = verbose
1113
self._wait_for_ready()
1214

1315
def _auto_connect(self, baudrate, timeout):
@@ -22,17 +24,31 @@ def _auto_connect(self, baudrate, timeout):
2224
print(f"Failed to connect to {e}. Trying next port...")
2325
continue
2426
raise Exception("No Arduino Uno or Mega found.")
27+
28+
def _drain_serial(self):
29+
"""Drains/clears the serial port input buffer of any unread messages."""
30+
if self.ser and self.ser.is_open:
31+
self.ser.reset_input_buffer()
32+
2533

2634
def _wait_for_message(self, message):
27-
while True:
35+
count = 0
36+
while count < 10:
2837
if self.ser.in_waiting:
2938
line = self.ser.readline().decode("utf-8", errors="ignore").strip()
3039
if message in line:
3140
return line
41+
count += 1
42+
time.sleep(0.05)
43+
if self.verbose:
44+
print(f"DEBUG: MESSAGE NOT FOUND: '{message}'")
45+
return ""
3246

3347
def _get_sensor_str(self):
34-
self._send("sense")
48+
self.ser.write(b"sense|")
3549
message = self._wait_for_message("SENSE_COMPLETE")
50+
if self.verbose:
51+
print(f"FULL SENSOR MESSSAGE: {message}")
3652
message = message.replace("SENSE_COMPLETE", "")
3753
sensorsStr = message.split(";")
3854
return sensorsStr
@@ -64,6 +80,23 @@ def config_digital_read(self, pin):
6480
def digital_read(self, pin):
6581
return self._find_sensor_str(pin, "dr") == "1"
6682

83+
# RFID
84+
def config_rfid(self, rxPin, txPin):
85+
self._send(f"config:rfid={rxPin},{txPin}")
86+
87+
def rfid_card_number(self):
88+
sensedata = self._find_sensor_str("0", "rfid")
89+
datum = sensedata.split("-")
90+
print(sensedata, datum)
91+
return datum[0] if len(datum) == 2 else ""
92+
93+
def rfid_tag_number(self):
94+
sensedata = self._find_sensor_str("0", "rfid")
95+
datum = sensedata.split("-")
96+
print(sensedata, datum)
97+
return datum[1] if len(datum) == 2 else ""
98+
99+
67100
# Motion Sensors
68101
def config_motion_sensor(self, echoPin, trigPin):
69102
self._send(f"config:m={echoPin},{trigPin}")

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "electroblocks"
7-
version = "0.1.9"
7+
version = "0.1.10"
88
description = "Python client library to interact with ElectroBlocks Arduino firmware"
99
authors = [
10-
{ name = "Noah Glaser", email = "your.email@example.com" }
10+
{ name = "Noah Glaser", email = "glaserpower@gmail.com" }
1111
]
1212
license = { text = "GNU v3.0" }
1313
readme = "README.md"

rfid.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Import ElectroBlocks library
2+
from electroblocks import ElectroBlocks
3+
import time # imports the time library
4+
5+
6+
# Variable Declaration
7+
8+
9+
# Initialise the program settings and configurations
10+
eb = ElectroBlocks(verbose=True) # Create an instance of the ElectroBlocks class
11+
eb.config_rfid(2,3) # Configures the LCD Screen pins
12+
13+
for _ in range(1, 10):
14+
tag_number = eb.rfid_tag_number()
15+
card_number = eb.rfid_card_number()
16+
print(f"Tag: {tag_number} | Card: {card_number}")
17+
time.sleep(10)
18+
print("CONTINUE")

0 commit comments

Comments
 (0)