diff --git a/README.md b/README.md index 4f0c8f5..4c621f3 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,7 @@ $GPGAA,HHMMSS.SS,llll.ll,a,yyyyy.yy,a,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx ### Sender issues -- [ ] data stops being transmitted from sender after some short period time +- [x] data stops being transmitted from sender after some short period time ### Monitor app issues diff --git a/monitor/main.go b/monitor/main.go index 06c619f..e08ef46 100644 --- a/monitor/main.go +++ b/monitor/main.go @@ -1,10 +1,8 @@ package main import ( - "log" - - gui "foglar/monitor/gui" - p "foglar/monitor/parse" + "foglar/monitor/gui" + "foglar/monitor/parse" "foglar/monitor/serial_read" "github.com/gopxl/pixel" @@ -30,9 +28,8 @@ func run() { // Initialize serial connection serialHandler, err := serial_read.NewSerialHandler() if err != nil { - log.Fatal(err) + panic(err) } - defer serialHandler.Close() // Create window @@ -69,11 +66,11 @@ func run() { data, err := serialHandler.ReadSerial() defer serialHandler.Close() if err != nil { - log.Fatal(err) + panic(err) } // Parsing data - info := p.Parser(data) + info := parse.Parser(data) // Clear screen values temperature.Clear() diff --git a/monitor/serial_read/serial.go b/monitor/serial_read/serial.go index 2ef1035..cd8d0a5 100644 --- a/monitor/serial_read/serial.go +++ b/monitor/serial_read/serial.go @@ -1,23 +1,32 @@ package serial_read import ( - "github.com/tarm/serial" + "fmt" "log" + "os" + "strconv" + + "github.com/tarm/serial" ) // TODO: // - Validation of port and baudrate // - And input of port and baudrate -// SerialHandler is a struct to handle serial communication +// SerialHandler struct type SerialHandler struct { port *serial.Port } -// NewSerialHandler initializes a new SerialHandler +// Initialize new SerialHandler func NewSerialHandler() (*SerialHandler, error) { - // CALL port and baudrate input from user !!! - s, err := serial.OpenPort(&serial.Config{Name: "/dev/ttyACM0", Baud: 9600}) + port := inputPort() + baudrate, err := inputBaudrate() + if err != nil { + fmt.Println("Error - Baudrate is not valid number") + } + + s, err := serial.OpenPort(&serial.Config{Name: port, Baud: baudrate}) if err != nil { log.Fatal(err) return nil, err @@ -26,7 +35,7 @@ func NewSerialHandler() (*SerialHandler, error) { return &SerialHandler{port: s}, nil } -// ReadSerial reads from the serial port and returns the received data as a string +// Reads from the serial port and returns the received data as a string func (sh *SerialHandler) ReadSerial() (string, error) { buf := make([]byte, 128) n, err := sh.port.Read(buf) @@ -42,3 +51,59 @@ func (sh *SerialHandler) ReadSerial() (string, error) { func (sh *SerialHandler) Close() error { return sh.port.Close() } + +func inputPort() string { + var s_port string + + for { + fmt.Print("Enter port ([Enter] - /dev/ttyACM0): ") + fmt.Scanln(&s_port) + + if isPort(s_port) == true { + break + } else { + fmt.Println("Error - Invalid Port") + } + } + + return s_port +} + +func inputBaudrate() (int, error) { + var s_baud string + + for { + fmt.Print("Enter baudrate (for example 9600): ") + fmt.Scanln(&s_baud) + + if isBaud(s_baud) == true { + break + } else { + fmt.Println("Error - Invalid Baudrate") + } + } + + return strconv.Atoi(s_baud) +} + +func isPort(port string) bool { + _, err := os.Stat(port) + if !os.IsNotExist(err) { + return true + } else { + return false + } +} + +func isBaud(baud string) bool { + switch baud { + case "4800": + return true + case "9600": + return true + case "115200": + return true + default: + return false + } +}