Some fixes.

This commit is contained in:
Andrey Egorov 2024-07-23 22:04:15 +03:00
parent 211ff6c63a
commit eb899f0b45
6 changed files with 61 additions and 20 deletions

View File

@ -83,13 +83,13 @@ func (p *atPort) IsConnected() bool {
// Low level write/read function // Low level write/read function
func (p *atPort) makeReq(msg string) (string, error) { func (p *atPort) makeReq(msg string) (string, error) {
// Write // Write
p.port.ResetInputBuffer()
if _, err := p.port.Write([]byte(msg)); err != nil { if _, err := p.port.Write([]byte(msg)); err != nil {
return "", fmt.Errorf("serial port write: %w", err) return "", fmt.Errorf("serial port write: %w", err)
} }
time.Sleep(time.Millisecond)
// Read // Read
readLen, err := p.port.Read(p.inputBuf) readLen, err := p.port.Read(p.inputBuf)
p.logger.Println(msg, "RAWREAD:", string(p.inputBuf[:readLen])) // p.logger.Println(msg, "\x1b[38;2;150;150;150mRAWREAD:", string(p.inputBuf[:readLen]), "\x1b[38;2;255;255;255m")
if err != nil { if err != nil {
return "", fmt.Errorf("port read: %w", err) return "", fmt.Errorf("port read: %w", err)
} }

View File

@ -8,7 +8,7 @@ import (
) )
type gpioPin struct { type gpioPin struct {
logger *log.Logger Logger *log.Logger
Pin gpio.Pin Pin gpio.Pin
} }
@ -18,22 +18,22 @@ func (p gpioPin) Init() error {
func (p gpioPin) sendOnOffSignal() { func (p gpioPin) sendOnOffSignal() {
p.Pin.Output() p.Pin.Output()
p.logger.Println("Power on 0/3 + 100ms") p.Logger.Println("Power on 0/3 + 100ms")
p.Pin.Low() p.Pin.Low()
p.Pin.Toggle() p.Pin.Toggle()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
p.logger.Println("Power on 1/3 + 3s") p.Logger.Println("Power on 1/3 + 3s")
p.Pin.High() p.Pin.High()
p.Pin.Toggle() p.Pin.Toggle()
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
p.logger.Println("Power on 2/3 + 30s") p.Logger.Println("Power on 2/3 + 30s")
p.Pin.Low() p.Pin.Low()
p.Pin.Toggle() p.Pin.Toggle()
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
p.logger.Println("Power on 3/3") p.Logger.Println("Power on 3/3")
} }
func (p gpioPin) PowerOn() { func (p gpioPin) PowerOn() {

View File

@ -50,8 +50,10 @@ func (gps *GpsInfo) decode(str string) error {
if err != nil { if err != nil {
return fmt.Errorf("parse longitude: %w", err) return fmt.Errorf("parse longitude: %w", err)
} }
newGpsInfo.Latitude /= 100
newGpsInfo.Longitude /= 100
newGpsInfo.LatitudeIndicator = strs[1] newGpsInfo.LatitudeIndicator = strs[1]
newGpsInfo.LatitudeIndicator = strs[3] newGpsInfo.LongitudeIndicator = strs[3]
newGpsInfo.Date = strs[4] newGpsInfo.Date = strs[4]
newGpsInfo.Time = strs[5] newGpsInfo.Time = strs[5]
newGpsInfo.Altitude, err = strconv.ParseFloat(strs[6], 64) newGpsInfo.Altitude, err = strconv.ParseFloat(strs[6], 64)

View File

@ -43,6 +43,8 @@ type Modem interface {
// CalculateSpeed(newLatitude, newlongitude float64) // CalculateSpeed(newLatitude, newlongitude float64)
Update() error Update() error
GetInfo() (string, error) GetInfo() (string, error)
GetShortInfo() string
SaveGPS(path string) error
TestGPS() error TestGPS() error
} }
@ -50,18 +52,18 @@ func New(logger *log.Logger) Modem {
return &modem{ return &modem{
logger: logger, logger: logger,
baudrate: 115200, baudrate: 115200,
onOffPin: gpioPin{Pin: 6}, onOffPin: gpioPin{Logger: logger, Pin: 6},
lastUpdateTime: time.Now(), lastUpdateTime: time.Now(),
} }
} }
func (m *modem) Init() error { func (m *modem) Init() error {
// Turn module on // Turn module on
// log.Println("=============================== Turn on module") log.Println("=============================== Turn on module")
// if err := m.onOffPin.Init(); err != nil { if err := m.onOffPin.Init(); err != nil {
// return fmt.Errorf("gpio pin init: %w", err) return fmt.Errorf("gpio pin init: %w", err)
// } }
// onOffPin.PowerOn() // m.onOffPin.PowerOn()
// Search // Search
m.logger.Println("=============================== Search") m.logger.Println("=============================== Search")
@ -214,6 +216,21 @@ func (m *modem) SaveGPS(path string) error {
} }
return nil return nil
} }
func (m *modem) testConsole() {
for {
var inStr string
fmt.Scanln(&inStr)
if inStr == "exit" {
return
}
resp, err := m.port.Send(inStr)
if err != nil {
m.logger.Println("ERROR:", err.Error())
}
log.Println(resp)
log.Println("------------------")
}
}
func (m *modem) checkPort(portName string) (outErr error) { func (m *modem) checkPort(portName string) (outErr error) {
defer func() { defer func() {
@ -235,16 +252,25 @@ func (m *modem) checkPort(portName string) (outErr error) {
} }
defer m.port.Disconnect() // Do not bother about errors... defer m.port.Disconnect() // Do not bother about errors...
// Reset input
if err := m.port.GetSerialPort().ResetInputBuffer(); err != nil {
return fmt.Errorf("reset input buffer: %w", err)
}
// Reset output
if err := m.port.GetSerialPort().ResetOutputBuffer(); err != nil {
return fmt.Errorf("reset output buffer: %w", err)
}
m.port.Send("ATE0") // This shit sometimes enables echo mode... why... when... but it can
// Ping // Ping
m.logger.Println("Ping...") m.logger.Println("Ping...")
if err := m.ping(); err != nil { if err := m.ping(); err != nil {
return fmt.Errorf("ping error: %w", err) return fmt.Errorf("ping error: %w", err)
} }
m.logger.Println("\x1b[38;2;0;255;0mOK\x1b[38;2;255;255;255m")
// Check model // Check model
m.logger.Println("Check model...") m.logger.Println("Check model...")
resp, err := m.port.Send("AT+CGMM") resp, err := m.port.Send("AT+CGMM")
if err != nil { if err != nil {
return fmt.Errorf("get model: %w", err) return fmt.Errorf("get model: %w", err)
@ -261,7 +287,7 @@ func (m *modem) checkPort(portName string) (outErr error) {
if model[:len(rightModel)] != rightModel { if model[:len(rightModel)] != rightModel {
return fmt.Errorf("invalid modem model: %s", model) return fmt.Errorf("invalid modem model: %s", model)
} }
m.logger.Println("Model right") m.logger.Println("\x1b[38;2;0;255;0mOK\x1b[38;2;255;255;255m")
return nil return nil
} }
@ -282,7 +308,7 @@ SearchLoop:
m.logger.Printf("Checking port %s ...\n", p) m.logger.Printf("Checking port %s ...\n", p)
if err := m.checkPort("/dev/" + p); err != nil { if err := m.checkPort("/dev/" + p); err != nil {
m.logger.Printf("Check failed: %s\n", err.Error()) m.logger.Printf("\x1b[38;2;255;0;0mCheck failed: %s\x1b[38;2;255;255;255m\n", err.Error())
continue SearchLoop continue SearchLoop
} }
@ -306,10 +332,14 @@ func (m *modem) ping() error {
func (m *modem) switchToGpsMode() error { func (m *modem) switchToGpsMode() error {
m.logger.Println("Enabling GPS mode...") m.logger.Println("Enabling GPS mode...")
// Reset intput // Reset input
if err := m.port.GetSerialPort().ResetInputBuffer(); err != nil { if err := m.port.GetSerialPort().ResetInputBuffer(); err != nil {
return fmt.Errorf("reset input buffer: %w", err) return fmt.Errorf("reset input buffer: %w", err)
} }
// Reset output
if err := m.port.GetSerialPort().ResetOutputBuffer(); err != nil {
return fmt.Errorf("reset output buffer: %w", err)
}
// Check gps mode status // Check gps mode status
resp, err := m.port.Send("AT+CGPS?") resp, err := m.port.Send("AT+CGPS?")

View File

@ -14,4 +14,4 @@ services:
devices: devices:
- /dev/i2c-1 - /dev/i2c-1
- /dev/ttyS0 - /dev/ttyS0
restart: unless-stopped restart: unless-stopped

View File

@ -17,9 +17,18 @@ func main() {
func mainE() error { func mainE() error {
m := modem.New(log.New(os.Stdout, "modem:", log.LstdFlags)) m := modem.New(log.New(os.Stdout, "modem:", log.LstdFlags))
log.Println("||||||||||||||||| INIT |||||||||||||||")
if err := m.Init(); err != nil { if err := m.Init(); err != nil {
return err return err
} }
log.Println("||||||||||||||||| GET INFO |||||||||||||||||")
if info, err := m.GetInfo(); err != nil {
return err
} else {
log.Println(info)
}
log.Println("||||||||||||||||| GET SHORT INFO ||||||||||||||||")
log.Println(m.GetShortInfo())
return nil return nil
// ports, err := serial.GetPortsList() // ports, err := serial.GetPortsList()
// if err != nil { // if err != nil {