Commit 68a4c89c authored by Kevin Lyda's avatar Kevin Lyda 💬

Work on GetConfiguration.

parent ed1aaa1f
Pipeline #1232 passed with stage
in 5 minutes and 13 seconds
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment