Fixed error logging. Added more logging of SIM info.

This commit is contained in:
yotia 2024-08-04 15:59:58 +03:00
parent 90a06e6afa
commit 026c1aa3bb
6 changed files with 65 additions and 38 deletions

View File

@ -23,7 +23,11 @@ func (st Status) String() string {
"ActiveSatelitesCounte: " + string(st.ActiveSatelitesCounte) + "\n"
}
var StatusNil = Status{}
var StatusNil = Status{
GotResponses: false,
FoundSatelitesCount: 0,
ActiveSatelitesCounte: 0,
}
func (g *gps) CheckStatus() (Status, error) {
// Provides more information about signal and possible problems using NMEA reports

View File

@ -48,7 +48,7 @@ func (g *gps) Update() error {
return fmt.Errorf("receive GPS data: %w", err)
}
if !resp.Check() {
return fmt.Errorf("error response")
return fmt.Errorf("get GPS info: error response: %s", resp)
}
if err := g.data.decode(strings.Split(strings.Replace(resp.RmFront("+CGPSINFO:").String(), "\r", "", -1), "\n")[0]); err != nil {
return fmt.Errorf("decode: %w", err)
@ -77,7 +77,7 @@ func (g *gps) switchGpsMode(on bool) error {
return fmt.Errorf("make at ask: %w", err)
}
if !resp.Check() {
return fmt.Errorf("error response")
return fmt.Errorf("get GPS mode: error response: %s", resp)
}
ans := strings.Replace(strings.Split(strings.Split(resp.RmFront("+CGPS:").String(), "\n")[0], ",")[0], " ", "", -1)
if ans == onStr {
@ -87,10 +87,10 @@ func (g *gps) switchGpsMode(on bool) error {
// Modem is not in GPS mode
resp, err = g.port.Send("AT+CGPS=" + onStr)
if err != nil {
return fmt.Errorf("try to switch to gps: %w", err)
return fmt.Errorf("set GPS mode: %w", err)
}
if !resp.Check() {
return fmt.Errorf("switch tp GPS failed")
return fmt.Errorf("set GPS mode: error response: %s", resp)
}
return nil
}

View File

@ -54,28 +54,12 @@ func (g *gps) rawCollect(flags nmeaFlags) (string, error) {
// Setup gps
// Receive data from all contries' satelites, enable DPO(Dynamic Power Optimization) mode
// if resp, err := g.port.Send("AT+CGNSSMODE=15,1"); err != nil {
// return "", fmt.Errorf("AT+CGNSSMODE= request: %w", err)
// } else {
// if !resp.Check() {
// return "", fmt.Errorf("AT+CGNSSMODE= error response: %s", resp)
// }
// }
// Receive all types of data
// if resp, err := g.port.Send("AT+CGPSNMEA=200191"); err != nil {
// return "", fmt.Errorf("AT+CGPSNMEA= request: %w", err)
// } else {
// if !resp.Check() {
// return "", fmt.Errorf("AT+CGPSNMEA= error response: %s", resp)
// }
// }
// Set output rate to 10Hz
if resp, err := g.port.Send("AT+CGPSNMEARATE=1"); err != nil {
return "", fmt.Errorf("AT+CGPSNMEARATE= request: %w", err)
} else {
if !resp.Check() {
return "", fmt.Errorf("AT+CGPSNMEARATE= error response: %s", resp)
return "", fmt.Errorf("set output rate: error response: %s", resp)
}
}

View File

@ -103,7 +103,7 @@ func (c *conn) checkReqs() error {
return fmt.Errorf("AT+CPIN? request: %w", err)
}
if !resp.Check() {
return fmt.Errorf("SIM card is not inserted")
return fmt.Errorf("validate SIM: error response: %s", resp)
}
return nil
}
@ -115,7 +115,7 @@ func (c *conn) ConfigurePPP() error {
return fmt.Errorf("AT+CSPN? request: %w", err)
}
if !resp.Check() {
return fmt.Errorf("failed to check SIM provider")
return fmt.Errorf("get provider: error response: %s", resp)
}
strs := strings.Split(string(resp), "\"")
if len(strs) < 3 {

View File

@ -250,6 +250,50 @@ func (m *modem) testConsole() {
}
}
func (m *modem) setupPort() error {
// Reset input
if err := m.port.SerialPort().ResetInputBuffer(); err != nil {
return fmt.Errorf("reset input buffer: %w", err)
}
// Reset output
if err := m.port.SerialPort().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
// Turn on errors' describtion
if resp, err := m.port.Send("AT+CMEE=2"); err != nil || !resp.Check() {
if err != nil {
return fmt.Errorf("AT+CMEE=2 request: %w", err)
}
return fmt.Errorf("turn on errors: error response: %s", resp)
}
// Display other values
m.logger.Println("DEBUG SIM VALUES:")
// ICCID
if resp, err := m.port.Send("AT+CCID"); err != nil || !resp.Check() {
if err != nil {
return fmt.Errorf("AT+CCID request: %w", err)
}
return fmt.Errorf("get ICCID: error response: %s", resp)
} else {
m.logger.Println("ICCID:", strings.Split(resp.String(), "\n")[0])
}
// ICCID
if resp, err := m.port.Send("AT+CNUM"); err != nil || !resp.Check() {
if err != nil {
return fmt.Errorf("AT+CNUM request: %w", err)
}
return fmt.Errorf("get CNUM: error response: %s", resp)
} else {
m.logger.Println("CNUM:", strings.Split(resp.String(), "\n")[0])
}
return nil
}
func (m *modem) checkPort(portName string) (outErr error) {
defer func() {
if outErr != nil { // Clear port if there is error
@ -270,16 +314,9 @@ func (m *modem) checkPort(portName string) (outErr error) {
}
defer m.port.Disconnect() // Do not bother about errors...
// Reset input
if err := m.port.SerialPort().ResetInputBuffer(); err != nil {
return fmt.Errorf("reset input buffer: %w", err)
if err := m.setupPort(); err != nil {
return fmt.Errorf("setup port: %w", err)
}
// Reset output
if err := m.port.SerialPort().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
// m.port.Send("\r\n")
// Ping
m.logger.Println("Ping...")
@ -295,7 +332,7 @@ func (m *modem) checkPort(portName string) (outErr error) {
return fmt.Errorf("get model: %w", err)
}
if !resp.Check() {
return fmt.Errorf("error response: %s", resp)
return fmt.Errorf("get model: error response: %s", resp)
}
model := strings.Split(resp.String(), "\n")[0]
if err != nil {
@ -341,10 +378,10 @@ SearchLoop:
func (m *modem) ping() error {
resp, err := m.port.Send("AT")
if err != nil {
return err
return fmt.Errorf("AT request: %w", err)
}
if !resp.Check() {
return fmt.Errorf("connection lost")
return fmt.Errorf("AT request: error response: %s", resp)
}
return nil
}

View File

@ -33,12 +33,13 @@ func (d *dialer) Init() error {
if !d.port.IsConnected() {
return fmt.Errorf("serial port is not connected")
}
// Ensure text format
if resp, err := d.port.Send("AT+CMGF=1"); err != nil || !resp.Check() {
if err != nil {
return fmt.Errorf("AT+CMGF=1 request: %w", err)
return fmt.Errorf("set to text format request: %w", err)
}
return fmt.Errorf("failed to set SMS format")
return fmt.Errorf("set SIM format: error response: %s", resp)
}
return nil
}
@ -52,6 +53,7 @@ func (d *dialer) Send(number, msg string) error {
if at.Resp(resp).Check() {
return nil
}
d.logger.Println("SEND RESPONSE:", resp)
errCode, err := GetError([]byte(resp))
if err != nil {
return fmt.Errorf("send sms failed and failed to get error: %w", err)