148 lines
4.4 KiB
Python
148 lines
4.4 KiB
Python
import socket
|
|
import logging
|
|
import time
|
|
import binascii
|
|
|
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
# Configuration for the Smart Board:
|
|
IP_ADDRESS = "10.10.4.58" # Default IP, can be changed via command
|
|
PORT = 4660 # Default port, can be changed via command
|
|
TIMEOUT = 5.0
|
|
|
|
def log_packet(direction: str, data: bytes):
|
|
hex_data = data.hex().upper()
|
|
logging.debug(f"{direction} [{len(data)} bytes]: {hex_data}")
|
|
|
|
def connect_to_device(ip, port):
|
|
"""Create and return a connected socket"""
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.settimeout(TIMEOUT)
|
|
|
|
try:
|
|
logging.debug(f"Connecting to {ip}:{port}")
|
|
sock.connect((ip, port))
|
|
logging.info("Connected.")
|
|
return sock
|
|
except Exception as ex:
|
|
logging.exception(f"Connection failed: {ex}")
|
|
return None
|
|
|
|
def send_hex_command(sock, hex_string):
|
|
"""Send a hex command and return any response"""
|
|
try:
|
|
# Convert hex string to bytes
|
|
hex_string = hex_string.replace(" ", "") # Remove any spaces
|
|
data = binascii.unhexlify(hex_string)
|
|
|
|
print(f"Client Send: {hex_string}")
|
|
log_packet("Sent", data)
|
|
|
|
sock.sendall(data)
|
|
|
|
# Wait for response with timeout
|
|
sock.settimeout(TIMEOUT)
|
|
response = bytearray()
|
|
|
|
# Try to read data until timeout
|
|
start_time = time.time()
|
|
while time.time() - start_time < TIMEOUT:
|
|
try:
|
|
chunk = sock.recv(1024)
|
|
if not chunk:
|
|
break
|
|
|
|
response.extend(chunk)
|
|
hex_response = chunk.hex().upper()
|
|
print(f"Server Recv: {hex_response}")
|
|
log_packet("Received", chunk)
|
|
|
|
# Small delay to allow more data to arrive if any
|
|
time.sleep(0.1)
|
|
except socket.timeout:
|
|
break
|
|
|
|
return response
|
|
except binascii.Error:
|
|
print("Error: Invalid hexadecimal string")
|
|
return None
|
|
except Exception as ex:
|
|
logging.exception(f"Error sending command: {ex}")
|
|
return None
|
|
|
|
def print_help():
|
|
"""Print available commands"""
|
|
print("\nSmart Board Hex Debug Tool")
|
|
print("------------------------")
|
|
print("Available commands:")
|
|
print(" connect <ip> <port> - Connect to device (default: 10.10.4.58:4660)")
|
|
print(" send <hex> - Send hexadecimal data")
|
|
print(" help - Show this help")
|
|
print(" exit - Exit the program")
|
|
print("\nExample:")
|
|
print(" connect 10.10.4.58 4660")
|
|
print(" send A5F329AA0D")
|
|
|
|
def main():
|
|
print_help()
|
|
|
|
ip = IP_ADDRESS
|
|
port = PORT
|
|
sock = None
|
|
|
|
while True:
|
|
try:
|
|
command = input("\n> ").strip()
|
|
|
|
if not command:
|
|
continue
|
|
|
|
parts = command.split()
|
|
cmd = parts[0].lower()
|
|
|
|
if cmd == "exit":
|
|
break
|
|
|
|
elif cmd == "help":
|
|
print_help()
|
|
|
|
elif cmd == "connect":
|
|
if sock:
|
|
sock.close()
|
|
|
|
if len(parts) >= 3:
|
|
ip = parts[1]
|
|
port = int(parts[2])
|
|
|
|
sock = connect_to_device(ip, port)
|
|
if not sock:
|
|
print(f"Failed to connect to {ip}:{port}")
|
|
|
|
elif cmd == "send":
|
|
if not sock:
|
|
print("Not connected. Use 'connect' first.")
|
|
continue
|
|
|
|
if len(parts) < 2:
|
|
print("Error: No hex data provided")
|
|
continue
|
|
|
|
hex_data = parts[1]
|
|
send_hex_command(sock, hex_data)
|
|
|
|
else:
|
|
print(f"Unknown command: {cmd}")
|
|
print("Type 'help' for available commands")
|
|
|
|
except KeyboardInterrupt:
|
|
print("\nExiting...")
|
|
break
|
|
except Exception as ex:
|
|
logging.exception(f"Error: {ex}")
|
|
|
|
if sock:
|
|
sock.close()
|
|
logging.info("Connection closed.")
|
|
|
|
if __name__ == '__main__':
|
|
main() |