From 58b0e9fc777a718d290e0fed66edbc4ab27794f8 Mon Sep 17 00:00:00 2001 From: smathis Date: Mon, 9 Mar 2026 11:37:02 +0100 Subject: [PATCH] Updated sinqMotor --- sinqMotor | 2 +- utils/writeRead.py | 51 ++++++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/sinqMotor b/sinqMotor index 92c264d..0e6c77b 160000 --- a/sinqMotor +++ b/sinqMotor @@ -1 +1 @@ -Subproject commit 92c264d0f688a5cd903d3f66f0e3aaa85410df22 +Subproject commit 0e6c77b456c22b3c3d2fe882c5dab838a4f5846c diff --git a/utils/writeRead.py b/utils/writeRead.py index 34eef0a..7248d12 100644 --- a/utils/writeRead.py +++ b/utils/writeRead.py @@ -9,45 +9,55 @@ Stefan Mathis, December 2024 import struct import socket import curses +import time + def packPmacCommand(command): - # 0x40 = VR_DOWNLOAD + # 0x40 = VR_DOWNLOAD # 0xBF = VR_PMAC_GETRESPONSE - buf = struct.pack('BBHHH',0x40,0xBF,0,0,socket.htons(len(command))) - buf = buf + bytes(command,'utf-8') + buf = struct.pack('BBHHH', 0x40, 0xBF, 0, 0, socket.htons(len(command))) + buf = buf + command.encode('utf-8') return buf -def readPmacReply(input): + +def readPmacReply(sock): msg = bytearray() expectAck = True + sock.settimeout(2.0) while True: - b = input.recv(1) - bint = int.from_bytes(b,byteorder='little') - if bint == 2 or bint == 7: #STX or BELL + try: + b = sock.recv(1) + except socket.timeout: + print('Timed out') + return None + bint = int.from_bytes(b, byteorder='little') + if bint == 2 or bint == 7: # STX or BELL expectAck = False continue - if expectAck and bint == 6: # ACK + if expectAck and bint == 6: # ACK return bytes(msg) else: - if bint == 13 and not expectAck: # CR + if bint == 13 and not expectAck: # CR return bytes(msg) else: msg.append(bint) + if __name__ == "__main__": from sys import argv try: addr = argv[1].split(':') - s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) - s.connect((addr[0],int(addr[1]))) + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((addr[0], int(addr[1]))) if len(argv) == 3: buf = packPmacCommand(argv[2]) s.send(buf) reply = readPmacReply(s) - print(reply.decode('utf-8') + '\n') + if reply: + print(reply.decode('utf-8') + '\n') else: @@ -81,13 +91,13 @@ if __name__ == "__main__": if ptr > 0: ptr -= 1 stdscr.addch("\r") - stdscr.clrtoeol() + stdscr.clrtoeol() stdscr.addstr(">> " + history[ptr]) elif c == curses.KEY_DOWN: if ptr < len(history) - 1: ptr += 1 stdscr.addch("\r") - stdscr.clrtoeol() + stdscr.clrtoeol() stdscr.addstr(">> " + history[ptr]) elif c == curses.KEY_ENTER or c == ord('\n') or c == ord('\r'): if history[ptr] == 'quit': @@ -113,7 +123,7 @@ if __name__ == "__main__": stdscr.refresh() else: - if ptr < len(history) - 1: # Modifying previous input + if ptr < len(history) - 1: # Modifying previous input if len(history[-1]) == 0: history[-1] = history[ptr] ptr = len(history) - 1 @@ -126,18 +136,20 @@ if __name__ == "__main__": if len(history[ptr]) == 0: continue (y, x) = stdscr.getyx() - history[ptr] = history[ptr][0:x-4] + history[ptr][x-3:] + history[ptr] = history[ptr][0:x-4] + \ + history[ptr][x-3:] stdscr.addch("\r") - stdscr.clrtoeol() + stdscr.clrtoeol() stdscr.addstr(">> " + history[ptr]) stdscr.move(y, x-1) stdscr.refresh() else: (y, x) = stdscr.getyx() - history[ptr] = history[ptr][0:x-3] + chr(c) + history[ptr][x-3:] + history[ptr] = history[ptr][0:x-3] + \ + chr(c) + history[ptr][x-3:] stdscr.addch("\r") - stdscr.clrtoeol() + stdscr.clrtoeol() stdscr.addstr(">> " + history[ptr]) stdscr.move(y, x+1) stdscr.refresh() @@ -169,4 +181,3 @@ if __name__ == "__main__": the reponse, before being prompted to again enter a command. Type 'quit' to close prompt. """) -