I am taking input through the serial port (using an Arduino) and I want to parse the data. Here is what I have so far:
package main
import (
"log"
"github.com/tarm/serial"
"bufio"
"sync"
"fmt"
)
func readFirstLine(data []string, wg *sync.WaitGroup){
defer wg.Done()
fmt.Printf("This is the sensor data:
%q
%q", data[0], data[1])
}
func readSecondLine(data []string, wg *sync.WaitGroup){
defer wg.Done()
fmt.Printf("This is the actuator data:
%q", data[2])
}
func main() {
usbRead := &serial.Config{Name: "COM5", Baud: 9600, ReadTimeout: 0}
port, err := serial.OpenPort(usbRead)
var wg sync.WaitGroup
wg.Add(2)
if err != nil {
log.Fatal(err)
}
data := []string{}
scanner := bufio.NewScanner(port)
for scanner.Scan() {
data = append(data, scanner.Text())
}
for {
go readFirstLine(data, &wg)
go readSecondLine(data, &wg)
wg.Wait()
}
}
The serial port currently prints this (looped):
{"temperature":[27.7],"humidity":[46.9],"sensor":"DHT22"}
{"temperature":[25.41545],"sensor":"LM35DZ"}
{"blink":["true"],"actuator":"arduinoLED"}
I am trying to use goroutines to parse the data, and print this (should be looped as well):
This is the sensor data:
{"temperature":[27.7],"humidity":[46.9],"sensor":"DHT22"}
{"temperature":[25.41545],"sensor":"LM35DZ"}
This is the actuator data:
{"blink":["true"],"actuator":"arduinoLED"}
However, I am not getting an output. The program simply isn't printing. I think it has to do with the way I am saving the data. Does anyone know how to fix this? And if it's fixed, whether this use of goroutines is the correct method to achieving what I want?
Thank you so much.