This commit is contained in:
Ben Charlton 2018-07-10 21:26:12 +01:00
parent 84fbe98f90
commit 26409e22ba

View file

@ -1,15 +1,14 @@
package main package main
import ( import (
"bufio"
"flag"
"fmt" "fmt"
"net" "net"
"bufio"
"time"
"os" "os"
"flag"
"strings"
"strconv" "strconv"
"strings"
"time"
) )
var chancount = flag.Int("channels", 4, "number of channels to collect") var chancount = flag.Int("channels", 4, "number of channels to collect")
@ -58,21 +57,21 @@ func main() {
tdone := time.Now() tdone := time.Now()
fmt.Printf("%s, %s, %s\n", tdone.Format(time.RFC3339), result, tdone.Sub(tstart)) fmt.Printf("%s, %s, %s\n", tdone.Format(time.RFC3339), result, tdone.Sub(tstart))
time.Sleep(time.Duration(*interval)*time.Second) time.Sleep(time.Duration(*interval) * time.Second)
} }
} }
func queryScope (conn net.Conn, query string) string { func queryScope(conn net.Conn, query string) string {
fmt.Fprintf(conn, fmt.Sprintf("%s\n", query)) fmt.Fprintf(conn, fmt.Sprintf("%s\n", query))
c1, _ := bufio.NewReader(conn).ReadString('\n') c1, _ := bufio.NewReader(conn).ReadString('\n')
return strings.TrimSuffix(c1, "\n") return strings.TrimSuffix(c1, "\n")
} }
func getScreenshot (conn net.Conn) []byte { func getScreenshot(conn net.Conn) []byte {
// Args are Colour, Invert, Format. // Args are Colour, Invert, Format.
fmt.Fprintf(conn, ":DISP:DATA? ON,FALSE,PNG\n") fmt.Fprintf(conn, ":DISP:DATA? ON,FALSE,PNG\n")
reader := bufio.NewReader(conn) reader := bufio.NewReader(conn)
header1,_ := reader.ReadByte() header1, _ := reader.ReadByte()
// should be a # // should be a #
if header1 != 35 { if header1 != 35 {
@ -80,32 +79,32 @@ func getScreenshot (conn net.Conn) []byte {
} }
// Next character shows us the length of the length of the datastream! // Next character shows us the length of the length of the datastream!
header2,_ := reader.ReadByte() header2, _ := reader.ReadByte()
buffsize := int(header2 - 48) buffsize := int(header2 - 48)
header3 := make([]byte, buffsize) header3 := make([]byte, buffsize)
for i:=0; i<buffsize; i++ { for i := 0; i < buffsize; i++ {
t,_ := reader.ReadByte() t, _ := reader.ReadByte()
header3[i] = t header3[i] = t
} }
// This is now the image buffersize // This is now the image buffersize
buffsize,_ = strconv.Atoi(string(header3)) buffsize, _ = strconv.Atoi(string(header3))
imgdata := make([]byte, buffsize) imgdata := make([]byte, buffsize)
for i:=0; i<buffsize; i++ { for i := 0; i < buffsize; i++ {
t,_ := reader.ReadByte() t, _ := reader.ReadByte()
imgdata[i] = t imgdata[i] = t
} }
return imgdata return imgdata
} }
func writeScreenshot (img []byte) { func writeScreenshot(img []byte) {
// Filename safe date format // Filename safe date format
tstamp := time.Now().Format("2006-01-02.150405") tstamp := time.Now().Format("2006-01-02.150405")
filename := fmt.Sprintf("%s.%s.png", "screenshot", tstamp) filename := fmt.Sprintf("%s.%s.png", "screenshot", tstamp)
f, err := os.Create(filename) f, err := os.Create(filename)
check(err) check(err)
defer f.Close() defer f.Close()
@ -113,10 +112,10 @@ func writeScreenshot (img []byte) {
check(err) check(err)
} }
func buildQuery () (string,string) { func buildQuery() (string, string) {
var command []string var command []string
var header []string var header []string
for i:=0; i<*chancount; i++ { for i := 0; i < *chancount; i++ {
// Stacking queries together seems to knock about 10% off the query time // Stacking queries together seems to knock about 10% off the query time
// compared to requesting one measurement at a time. // compared to requesting one measurement at a time.
@ -154,11 +153,11 @@ func buildQuery () (string,string) {
header = append(header, fmt.Sprintf("CH%d freq", i+1)) header = append(header, fmt.Sprintf("CH%d freq", i+1))
} }
} }
return strings.Join(command,";"), strings.Join(header,", ") return strings.Join(command, ";"), strings.Join(header, ", ")
} }
func check(e error) { func check(e error) {
if e != nil { if e != nil {
panic(e) panic(e)
} }
} }