serial read in monitor app
created new go package monitor, prepared serial monitor sub-package, sender_module, and reciever_module updated
This commit is contained in:
parent
97d57b139b
commit
11ba173218
4
.gitignore
vendored
4
.gitignore
vendored
@ -6,3 +6,7 @@ serial_read/go.mod
|
||||
serial_read/go.sum
|
||||
|
||||
*.exe
|
||||
|
||||
monitor/go.sum
|
||||
|
||||
monitor/go.mod
|
||||
|
||||
26
monitor/main.go
Normal file
26
monitor/main.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"foglar/monitor/serial_read"
|
||||
"log"
|
||||
// "foglar/monitor/parse"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Initialize serial connection
|
||||
serialHandler, err := serial_read.NewSerialHandler()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer serialHandler.Close()
|
||||
|
||||
// Read serial data
|
||||
data, err := serialHandler.ReadSerial()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("Received data:", data)
|
||||
}
|
||||
3
monitor/parse/parse.go
Normal file
3
monitor/parse/parse.go
Normal file
@ -0,0 +1,3 @@
|
||||
package parse
|
||||
|
||||
func parse() {}
|
||||
44
monitor/serial_read/serial.go
Normal file
44
monitor/serial_read/serial.go
Normal file
@ -0,0 +1,44 @@
|
||||
package serial_read
|
||||
|
||||
import (
|
||||
"github.com/tarm/serial"
|
||||
"log"
|
||||
)
|
||||
|
||||
// TODO:
|
||||
// - Validation of port and baudrate
|
||||
// - And input of port and baudrate
|
||||
|
||||
// SerialHandler is a struct to handle serial communication
|
||||
type SerialHandler struct {
|
||||
port *serial.Port
|
||||
}
|
||||
|
||||
// NewSerialHandler initializes a new SerialHandler
|
||||
func NewSerialHandler() (*SerialHandler, error) {
|
||||
// CALL port and baudrate input from user !!!
|
||||
s, err := serial.OpenPort(&serial.Config{Name: "/dev/ttyACM0", Baud: 9600})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &SerialHandler{port: s}, nil
|
||||
}
|
||||
|
||||
// ReadSerial 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)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(buf[:n]), nil
|
||||
}
|
||||
|
||||
// Close closes the serial port
|
||||
func (sh *SerialHandler) Close() error {
|
||||
return sh.port.Close()
|
||||
}
|
||||
@ -15,7 +15,7 @@ void setup() {
|
||||
void loop() {
|
||||
//Read the data if available in buffer
|
||||
if (radio.available()) {
|
||||
char text[32] = { 0 };
|
||||
char text[64] = { 0 };
|
||||
radio.read(&text, sizeof(text));
|
||||
Serial.println(text);
|
||||
}
|
||||
|
||||
@ -50,10 +50,42 @@ void loop() {
|
||||
|
||||
imuDataGet( &stAngles, &stGyroRawData, &stAccelRawData, &stMagnRawData);
|
||||
pressSensorDataGet(&s32TemperatureVal, &s32PressureVal, &s32AltitudeVal);
|
||||
char result[8];
|
||||
|
||||
char temp_str[8], pressure_str[8], altitude_str[8];
|
||||
char roll_str[8], pitch_str[8], yaw_str[8];
|
||||
|
||||
float temperature = (s32TemperatureVal / 100);
|
||||
dtostrf(temperature, 6, 2, result);
|
||||
//Send message to receiver
|
||||
radio.write(&result, sizeof(result));
|
||||
//delay(1000);
|
||||
}
|
||||
float pressure = (s32PressureVal / 100);
|
||||
float altitude = (s32AltitudeVal / 100);
|
||||
|
||||
float roll = stAngles.fRoll;
|
||||
float pitch = stAngles.fPitch;
|
||||
float yaw = stAngles.fYaw;
|
||||
|
||||
dtostrf(temperature, 6, 2, temp_str);
|
||||
dtostrf(pressure, 6, 2, pressure_str);
|
||||
dtostrf(altitude, 6, 2, altitude_str);
|
||||
|
||||
dtostrf(roll, 6, 2, roll_str);
|
||||
dtostrf(pitch, 6, 2, pitch_str);
|
||||
dtostrf(yaw, 6, 2, yaw_str);
|
||||
|
||||
//Serial.println(roll, pitch, yaw);
|
||||
//Serial.println(temperature, pressure, altitude);
|
||||
|
||||
char msg[64];
|
||||
|
||||
String ctemp = ("$1;"+String(temp_str)+"*");
|
||||
String cpressure = ("$2;"+String(pressure_str)+"*");
|
||||
String caltitude = ("$3;"+String(altitude_str)+"*");
|
||||
String croll = ("$4;"+String(roll_str)+"*");
|
||||
String cpitch = ("$5;"+String(pitch_str)+"*");
|
||||
String cyaw = ("$6;"+String(yaw_str) + "*");
|
||||
|
||||
ctemp.toCharArray(msg, sizeof(msg));
|
||||
radio.write(&msg, sizeof(msg));
|
||||
cpressure.toCharArray(msg, sizeof(msg));
|
||||
radio.write(&msg, sizeof(msg));
|
||||
|
||||
delay(200);
|
||||
}
|
||||
|
||||
@ -73,6 +73,7 @@ func main() {
|
||||
var port string
|
||||
var baudrate int
|
||||
|
||||
// parsing command line arguments
|
||||
if len(args) == 0 {
|
||||
port, baudrate = UserInput()
|
||||
} else {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user