This commit is contained in:
2026-01-10 16:04:38 +01:00
parent f172678560
commit 0ce18d87bc
13 changed files with 779 additions and 226 deletions

View File

@@ -103,17 +103,20 @@ func (c *Client) Stop() {
}
func (c *Client) pollLoop() {
ticker := time.NewTicker(500 * time.Millisecond)
ticker := time.NewTicker(2 * time.Second) // Increased from 500ms to 2s
defer ticker.Stop()
for {
select {
case <-ticker.C:
// Try to connect if not connected
c.connMu.Lock()
if c.conn == nil {
log.Printf("Ultrabeam: Not connected, attempting connection...")
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", c.host, c.port), 5*time.Second)
if err != nil {
log.Printf("Ultrabeam: Connection failed: %v", err)
c.connMu.Unlock()
// Mark as disconnected
@@ -151,12 +154,6 @@ func (c *Client) pollLoop() {
// Mark as connected
status.Connected = true
// Query element lengths
lengths, err := c.queryElementLengths()
if err == nil {
status.ElementLengths = lengths
}
// Query progress if motors moving
if status.MotorsMoving != 0 {
progress, err := c.queryProgress()
@@ -312,7 +309,7 @@ func (c *Client) sendCommand(cmd byte, data []byte) ([]byte, error) {
}
// Read reply with timeout
c.conn.SetReadDeadline(time.Now().Add(2 * time.Second))
c.conn.SetReadDeadline(time.Now().Add(1 * time.Second)) // Reduced from 2s to 1s
// Read until we get a complete packet
var buffer []byte
@@ -341,6 +338,11 @@ func (c *Client) sendCommand(cmd byte, data []byte) ([]byte, error) {
return nil, fmt.Errorf("failed to parse reply: %w", err)
}
// Log for debugging unknown codes
if replyCmd != UB_OK && replyCmd != UB_BAD && replyCmd != UB_PAR && replyCmd != UB_ERR {
log.Printf("Ultrabeam: Unknown reply code %d (0x%02X), raw packet: %v", replyCmd, replyCmd, buffer)
}
// Check for errors
switch replyCmd {
case UB_BAD:
@@ -352,7 +354,10 @@ func (c *Client) sendCommand(cmd byte, data []byte) ([]byte, error) {
case UB_OK:
return payload, nil
default:
return nil, fmt.Errorf("unknown reply code: %d", replyCmd)
// Unknown codes might indicate "busy" or "in progress"
// Treat as non-fatal, return empty payload
log.Printf("Ultrabeam: Unusual reply code %d, treating as busy/in-progress", replyCmd)
return []byte{}, nil
}
}
@@ -390,17 +395,59 @@ func (c *Client) queryElementLengths() ([]int, error) {
return nil, err
}
// Debug: log raw bytes
log.Printf("Ultrabeam element lengths raw reply (%d bytes): %v", len(reply), reply)
// Try to extract 6 words - the protocol says 6 words (12 bytes)
// But we're receiving 14 bytes, so there might be padding
if len(reply) < 12 {
return nil, fmt.Errorf("element lengths reply too short")
return nil, fmt.Errorf("element lengths reply too short: %d bytes", len(reply))
}
lengths := make([]int, 6)
// Try different interpretations
log.Printf("=== Attempting different parsings ===")
// Method 1: Standard little-endian from byte 0
log.Printf("Method 1 (little-endian from 0):")
for i := 0; i < 6 && i*2+1 < len(reply); i++ {
lo := int(reply[i*2])
hi := int(reply[i*2+1])
val := lo | (hi << 8)
log.Printf(" Element %d: bytes[%d,%d] = [%d,%d] => %d mm", i, i*2, i*2+1, lo, hi, val)
}
// Method 2: Big-endian from byte 0
log.Printf("Method 2 (big-endian from 0):")
for i := 0; i < 6 && i*2+1 < len(reply); i++ {
hi := int(reply[i*2])
lo := int(reply[i*2+1])
val := lo | (hi << 8)
log.Printf(" Element %d: bytes[%d,%d] = [%d,%d] => %d mm", i, i*2, i*2+1, hi, lo, val)
}
// Method 3: Skip first 2 bytes, then little-endian
log.Printf("Method 3 (skip 2 bytes, little-endian):")
for i := 0; i < 6 && i*2+3 < len(reply); i++ {
lo := int(reply[i*2+2])
hi := int(reply[i*2+3])
val := lo | (hi << 8)
log.Printf(" Element %d: bytes[%d,%d] = [%d,%d] => %d mm", i, i*2+2, i*2+3, lo, hi, val)
}
// For now, use method 1 (original)
for i := 0; i < 6; i++ {
if i*2+1 >= len(reply) {
break
}
lo := int(reply[i*2])
hi := int(reply[i*2+1])
lengths[i] = lo | (hi << 8)
}
log.Printf("Final lengths: %v", lengths)
return lengths, nil
}