Skip to content

Commit

Permalink
Merge pull request #74 from SiboVG/catch-serial-none
Browse files Browse the repository at this point in the history
Add extra checks for when serial is None
  • Loading branch information
Chr157i4n authored Aug 1, 2024
2 parents 062e205 + 7a5549f commit 0846122
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## version 0.5.2

- added extra error handling for when the UART serial is not set

## version 0.5.1

- added toff setting
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = TMC_2209_Raspberry_Pi
version = 0.5.1
version = 0.5.2
author = Christian Köhlke
author_email = christian@koehlke.de
description = this is a Python libary to drive a stepper motor with a Trinamic TMC2209 stepper driver and a Raspberry Pi
Expand Down
5 changes: 4 additions & 1 deletion src/TMC_2209/_TMC_2209_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ def set_max_speed(self, speed):
speed = -speed
if self._max_speed != speed:
self._max_speed = speed
self._cmin = 1000000.0 / speed
if speed == 0.0:
self._cmin = 0.0
else:
self._cmin = 1000000.0 / speed
# Recompute _n from current speed and adjust speed if accelerating or cruising
if self._n > 0:
self._n = (self._speed * self._speed) / (2.0 * self._acceleration) # Equation 16
Expand Down
29 changes: 24 additions & 5 deletions src/TMC_2209/_TMC_2209_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self,
if serialport is None:
return
try:
self.ser = serial.Serial (serialport, baudrate)
self.ser = serial.Serial(serialport, baudrate)
except Exception as e:
errnum = e.args[0]
self.tmc_logger.log(f"SERIAL ERROR: {e}")
Expand All @@ -61,15 +61,18 @@ def __init__(self,
with \"sudo usermod -a -G dialout pi\"""")

self.mtr_id = mtr_id
# adjust per baud and hardware. Sequential reads without some delay fail.
self.communication_pause = 500 / baudrate

if self.ser is None:
return

self.ser.BYTESIZES = 1
self.ser.PARITIES = serial.PARITY_NONE
self.ser.STOPBITS = 1

# adjust per baud and hardware. Sequential reads without some delay fail.
self.ser.timeout = 20000/baudrate
# adjust per baud and hardware. Sequential reads without some delay fail.
self.communication_pause = 500/baudrate


self.ser.reset_output_buffer()
self.ser.reset_input_buffer()
Expand Down Expand Up @@ -112,6 +115,9 @@ def read_reg(self, register):
Args:
register (int): HEX, which register to read
"""
if self.ser is None:
self.tmc_logger.log("Cannot read reg, serial is not initialized", Loglevel.ERROR)
return False

self.ser.reset_output_buffer()
self.ser.reset_input_buffer()
Expand Down Expand Up @@ -146,6 +152,9 @@ def read_int(self, register, tries=10):
register (int): HEX, which register to read
tries (int): how many tries, before error is raised (Default value = 10)
"""
if self.ser is None:
self.tmc_logger.log("Cannot read int, serial is not initialized", Loglevel.ERROR)
return -1
while True:
tries -= 1
rtn = self.read_reg(register)
Expand Down Expand Up @@ -183,6 +192,9 @@ def write_reg(self, register, val):
register (int): HEX, which register to write
val (int): value for that register
"""
if self.ser is None:
self.tmc_logger.log("Cannot write reg, serial is not initialized", Loglevel.ERROR)
return False

self.ser.reset_output_buffer()
self.ser.reset_input_buffer()
Expand Down Expand Up @@ -219,6 +231,9 @@ def write_reg_check(self, register, val, tries=10):
val: value for that register
tries: how many tries, before error is raised (Default value = 10)
"""
if self.ser is None:
self.tmc_logger.log("Cannot write reg check, serial is not initialized", Loglevel.ERROR)
return False
ifcnt1 = self.read_int(reg.IFCNT)

if ifcnt1 == 255:
Expand All @@ -230,7 +245,7 @@ def write_reg_check(self, register, val, tries=10):
ifcnt2 = self.read_int(reg.IFCNT)
if ifcnt1 >= ifcnt2:
self.tmc_logger.log("writing not successful!", Loglevel.ERROR)
self.tmc_logger.log("ifcnt:",ifcnt1,ifcnt2, Loglevel.DEBUG)
self.tmc_logger.log(f"ifcnt: {ifcnt1}, {ifcnt2}", Loglevel.DEBUG)
else:
return True
if tries<=0:
Expand Down Expand Up @@ -304,6 +319,10 @@ def test_uart(self, register):
register (int): HEX, which register to read
"""

if self.ser is None:
self.tmc_logger.log("Cannot test UART, serial is not initialized", Loglevel.ERROR)
return False

self.ser.reset_output_buffer()
self.ser.reset_input_buffer()

Expand Down
1 change: 1 addition & 0 deletions tests/test_TMC_2209_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def setUp(self):

def test_read_int(self):
"""test_read_int"""
self.tmc_uart.ser = 1 # to avoid early return, due to ser being None
with mock.patch.object(TMC_UART, 'read_reg', return_value=
b'U\x00o\x03\x05\xffo\xc0\x1e\x00\x00\xca'):
reg_ans = self.tmc_uart.read_int(0x00)
Expand Down

0 comments on commit 0846122

Please sign in to comment.