From 68a4c89c0c035ea998bc9d60949d6dfa7e36f0bf Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@ie.suberic.net>
Date: Wed, 1 Feb 2017 21:55:37 +0000
Subject: [PATCH] Work on GetConfiguration.

---
 devices/geiger/gqgmc.go | 43 ++++++++++++++++++++++++++---------------
 1 file changed, 27 insertions(+), 16 deletions(-)

diff --git a/devices/geiger/gqgmc.go b/devices/geiger/gqgmc.go
index 7702622..6875b36 100644
--- a/devices/geiger/gqgmc.go
+++ b/devices/geiger/gqgmc.go
@@ -95,7 +95,7 @@ func (gc *GQGMCCounter) Clear() error {
 	// Read up to 10 chars until nothing comes back.
 	// error otherwise.
 	for i := 0; i < 10; i++ {
-		if _, err := gc.readCmd(1); err != nil {
+		if _, err := gc.recv(1); err != nil {
 			break
 		}
 	}
@@ -169,7 +169,7 @@ func (gc *GQGMCCounter) TurnOnCPS() error {
 	}
 
 	gc.mutex.Lock()
-	gc.sendCmd(cmdTurnOnCPS)
+	gc.send(cmdTurnOnCPS)
 	gc.mutex.Unlock()
 	return nil
 }
@@ -184,7 +184,7 @@ func (gc *GQGMCCounter) TurnOffCPS() error {
 	}
 
 	gc.mutex.Lock()
-	gc.sendCmd(cmdTurnOffCPS)
+	gc.send(cmdTurnOffCPS)
 	gc.Clear()
 	gc.mutex.Unlock()
 	return nil
@@ -193,7 +193,7 @@ func (gc *GQGMCCounter) TurnOffCPS() error {
 // GetAutoCPS gets a reading once auto CPS is turned on
 func (gc *GQGMCCounter) GetAutoCPS() (uint16, error) {
 	gc.mutex.Lock()
-	buf, err := gc.readCmd(2)
+	buf, err := gc.recv(2)
 	gc.mutex.Unlock()
 
 	if err != nil {
@@ -214,7 +214,7 @@ func (gc *GQGMCCounter) TurnOnPower() {
 	}
 
 	gc.mutex.Lock()
-	gc.sendCmd(cmdTurnOnPwr)
+	gc.send(cmdTurnOnPwr)
 	gc.mutex.Unlock()
 	return
 }
@@ -229,7 +229,7 @@ func (gc *GQGMCCounter) TurnOffPower() {
 	}
 
 	gc.mutex.Lock()
-	gc.sendCmd(cmdTurnOffPwr)
+	gc.send(cmdTurnOffPwr)
 	gc.mutex.Unlock()
 	return
 }
@@ -299,7 +299,7 @@ func (gc *GQGMCCounter) setTimeParts(t time.Time) {
 		copy(cmd[11:], ">>")
 		// Mutex acquired in setTime()
 		gc.port.Write(cmd)
-		gc.readCmd(1)
+		gc.recv(1)
 	}
 }
 
@@ -315,7 +315,7 @@ func (gc *GQGMCCounter) setTimeAll(t time.Time) {
 	copy(cmd[18:], ">>")
 	// Mutex acquired in setTime()
 	gc.port.Write(cmd)
-	gc.readCmd(1)
+	gc.recv(1)
 }
 
 // GetTime gets the time
@@ -393,7 +393,7 @@ func (gc *GQGMCCounter) FactoryReset() {
 	}
 
 	gc.mutex.Lock()
-	gc.sendCmd(cmdFactoryReset)
+	gc.send(cmdFactoryReset)
 	gc.mutex.Unlock()
 	return
 }
@@ -408,7 +408,7 @@ func (gc *GQGMCCounter) Reboot() {
 	}
 
 	gc.mutex.Lock()
-	gc.sendCmd(cmdReboot)
+	gc.send(cmdReboot)
 	gc.mutex.Unlock()
 	return
 }
@@ -426,31 +426,42 @@ func (gc *GQGMCCounter) versionLT(version string) bool {
 	return gc.version < version
 }
 
-func (gc *GQGMCCounter) communicate(cmd string, length uint32) ([]byte, error) {
+func (gc *GQGMCCounter) communicate(cmd string, length int) ([]byte, error) {
 	gc.mutex.Lock()
 	defer gc.mutex.Unlock()
 	gc.Clear()
 	if len(cmd) > 0 {
-		gc.sendCmd(cmd)
+		gc.send(cmd)
 	}
 	if length != 0 {
-		return gc.readCmd(length)
+		return gc.recv(length)
 	}
 	return nil, nil
 }
 
-func (gc *GQGMCCounter) sendCmd(cmd string) {
+func (gc *GQGMCCounter) send(cmd string) {
 	gc.port.Write([]byte(cmd))
 	return
 }
 
-func (gc *GQGMCCounter) readCmd(length uint32) ([]byte, error) {
+func (gc *GQGMCCounter) recv(length int) ([]byte, error) {
 	buf := make([]byte, length)
 	n, err := gc.port.Read(buf)
 	if err != nil {
 		return nil, err
 	}
-	if uint32(n) != length {
+	if n != length {
+		for i := 0; i < 20; i++ {
+			_, err = gc.port.Read(buf[len(buf):])
+			if err != nil {
+				return nil, err
+			}
+			if len(buf) == length {
+				break
+			}
+		}
+	}
+	if n != length {
 		return nil, fmt.Errorf("Short read (got: %d, wanted: %d)", uint32(n), length)
 	}
 	return buf, nil
-- 
GitLab