Skip to content

Set up environment and pushed test file#2

Open
Neil-B1 wants to merge 2 commits intoSARP-UW:mainfrom
Neil-B1:feature/communication-controller-init
Open

Set up environment and pushed test file#2
Neil-B1 wants to merge 2 commits intoSARP-UW:mainfrom
Neil-B1:feature/communication-controller-init

Conversation

@Neil-B1
Copy link
Copy Markdown
Member

@Neil-B1 Neil-B1 commented Oct 21, 2025

No description provided.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR sets up the initial project environment by adding a test file and implementing a complete RS485 UART interface module for Raspberry Pi.

  • Adds RS485Interface class with GPIO-controlled transceiver direction (DE/RE pins)
  • Implements framed messaging protocol with CRC16 checksums and background receive thread
  • Includes example usage in main() function

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.

File Description
test.txt Test file confirming successful push to repository
RS485Interface.py Complete RS485 UART interface implementation with GPIO control, CRC16 framing, and threaded reception

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

buffer = bytearray()
while self._running.is_set():
try:
data = self.ser.read(64)
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential race condition: self.ser is accessed without holding self._lock in the background receive thread (_receiver_loop), while the send() method accesses it with the lock held. This could lead to undefined behavior if send and receive operations occur simultaneously. Consider protecting serial port access with the lock consistently, or document why it's safe.

Suggested change
data = self.ser.read(64)
with self._lock:
data = self.ser.read(64)

Copilot uses AI. Check for mistakes.

return body[1:]

def send(self, payload: bytes) -> None:
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing input validation: The send() method doesn't validate that payload is of type bytes. If a non-bytes object is passed, it will fail later in _build_frame(). Add validation at the API boundary: if not isinstance(payload, bytes): raise TypeError("Payload must be bytes")

Suggested change
def send(self, payload: bytes) -> None:
def send(self, payload: bytes) -> None:
if not isinstance(payload, bytes):
raise TypeError("Payload must be bytes")

Copilot uses AI. Check for mistakes.
self._running.clear()
if self._rx_thread:
self._rx_thread.join(1.0)
if self.ser and self.ser.is_open:
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The close() method should be idempotent and handle being called multiple times safely. Additionally, it should check if self.ser exists before accessing it, as initialization could fail before the serial port is created. Consider adding: if hasattr(self, 'ser') and self.ser and self.ser.is_open:

Suggested change
if self.ser and self.ser.is_open:
if hasattr(self, 'ser') and self.ser and self.ser.is_open:

Copilot uses AI. Check for mistakes.
Comment on lines +210 to +234
rs = RS485Interface(
port="/dev/ttyAMA0",
baudrate=115200,
de_pin=18,
re_pin=23
)

try:
i = 0
while True:
payload = f"Hello {i}".encode()
print("TX", payload)
rs.send(payload)

rx = rs.read_frame(timeout=1.0)
if rx is not None:
print("RX", rx)

i += 1
time.sleep(2)

except KeyboardInterrupt:
print("Exiting")
finally:
rs.close()
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation detected. The main() function body uses 8 spaces (double indentation) instead of the standard 4 spaces used throughout the rest of the file. This should be corrected to use 4-space indentation to match the project's style.

Suggested change
rs = RS485Interface(
port="/dev/ttyAMA0",
baudrate=115200,
de_pin=18,
re_pin=23
)
try:
i = 0
while True:
payload = f"Hello {i}".encode()
print("TX", payload)
rs.send(payload)
rx = rs.read_frame(timeout=1.0)
if rx is not None:
print("RX", rx)
i += 1
time.sleep(2)
except KeyboardInterrupt:
print("Exiting")
finally:
rs.close()
rs = RS485Interface(
port="/dev/ttyAMA0",
baudrate=115200,
de_pin=18,
re_pin=23
)
try:
i = 0
while True:
payload = f"Hello {i}".encode()
print("TX", payload)
rs.send(payload)
rx = rs.read_frame(timeout=1.0)
if rx is not None:
print("RX", rx)
i += 1
time.sleep(2)
except KeyboardInterrupt:
print("Exiting")
finally:
rs.close()

Copilot uses AI. Check for mistakes.
@@ -0,0 +1 @@
Neil has pushed this to github No newline at end of file
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling error: "Github" should be "GitHub" (with a capital H). The correct capitalization is important for proper noun accuracy.

Suggested change
Neil has pushed this to github
Neil has pushed this to GitHub

Copilot uses AI. Check for mistakes.
continue

except Exception as e:
print("RS485 RX thread error", e)
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message lacks sufficient detail. It should include the exception type and message to aid debugging. Consider changing to: print(f"RS485 RX thread error: {type(e).__name__}: {e}", file=sys.stderr)

Suggested change
print("RS485 RX thread error", e)
print(f"RS485 RX thread error: {type(e).__name__}: {e}", file=sys.stderr)

Copilot uses AI. Check for mistakes.
Comment on lines +143 to +149
def send(self, payload: bytes) -> None:
frame = self._build_frame(payload)
with self._lock:
self._set_transmit_mode()
self.ser.write(frame)
self.ser.flush()
time.sleep(len(frame) * 10 / self.baudrate)
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic number used without explanation. The value 10 in the sleep calculation len(frame) * 10 / self.baudrate appears to represent bits per byte (1 start + 8 data + 1 stop = 10 bits). This should be defined as a named constant like BITS_PER_BYTE = 10 with a comment explaining the calculation for better maintainability.

Suggested change
def send(self, payload: bytes) -> None:
frame = self._build_frame(payload)
with self._lock:
self._set_transmit_mode()
self.ser.write(frame)
self.ser.flush()
time.sleep(len(frame) * 10 / self.baudrate)
# Number of bits per UART byte: 1 start bit + 8 data bits + 1 stop bit = 10 bits
BITS_PER_BYTE = 10
def send(self, payload: bytes) -> None:
frame = self._build_frame(payload)
with self._lock:
self._set_transmit_mode()
self.ser.write(frame)
self.ser.flush()
time.sleep(len(frame) * self.BITS_PER_BYTE / self.baudrate)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants