From d05e2205d61e7194f0f491a3e2a7bf01a07ceb67 Mon Sep 17 00:00:00 2001 From: Andrey Egorov Date: Mon, 29 Jul 2024 18:53:55 +0300 Subject: [PATCH] Semi debugged sms. --- api/modem/at/at.go | 7 ++++--- api/modem/modem.go | 3 ++- api/modem/sms/sms.go | 22 ++++++++++++---------- main.go | 2 +- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/api/modem/at/at.go b/api/modem/at/at.go index 25e4625..10e5ee9 100644 --- a/api/modem/at/at.go +++ b/api/modem/at/at.go @@ -33,6 +33,7 @@ type Port interface { Disconnect() error IsConnected() bool + RawSend(msg string) (string, error) Send(cmd string) (Resp, error) io.Closer @@ -89,7 +90,7 @@ func (p *atPort) IsConnected() bool { } // Low level write/read function -func (p *atPort) makeReq(msg string) (string, error) { +func (p *atPort) RawSend(msg string) (string, error) { // Write if _, err := p.port.Write([]byte(msg)); err != nil { return "", fmt.Errorf("serial port write: %w", err) @@ -107,12 +108,12 @@ func (p *atPort) makeReq(msg string) (string, error) { func (p *atPort) Send(cmd string) (Resp, error) { cmd += "\r\n" - rawResp, err := p.makeReq(cmd) + rawResp, err := p.RawSend(cmd) if err != nil { return RespNil, fmt.Errorf("make request: %w", err) } if len(rawResp) <= 4 { - return RespNil, fmt.Errorf("read too small msg: %d byte", len(rawResp)) + return RespNil, fmt.Errorf("read too small msg: %d byte - %s", len(rawResp), string(rawResp)) } resp := rawResp[2 : len(rawResp)-2] // Cut \r\n diff --git a/api/modem/modem.go b/api/modem/modem.go index ad7803e..2bc6bdc 100644 --- a/api/modem/modem.go +++ b/api/modem/modem.go @@ -291,6 +291,7 @@ func (m *modem) checkPort(portName string) (outErr error) { 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...") @@ -314,7 +315,7 @@ func (m *modem) checkPort(portName string) (outErr error) { } rightModel := "SIMCOM_SIM7600E-H" // m.logger.Printf("[% x]\n [% x]", []byte("SIMCOM_SIM7600E-H"), []byte(model)) - if model[:len(rightModel)] != rightModel { + if len(model) >= len(rightModel) && model[:len(rightModel)] != rightModel { return fmt.Errorf("invalid modem model: %s", model) } m.logger.Println("\x1b[38;2;0;255;0mOK\x1b[38;2;255;255;255m") diff --git a/api/modem/sms/sms.go b/api/modem/sms/sms.go index 72646ca..7890ad1 100644 --- a/api/modem/sms/sms.go +++ b/api/modem/sms/sms.go @@ -44,17 +44,19 @@ func (d *dialer) Init() error { } func (d *dialer) Send(number, msg string) error { - cmd := fmt.Sprintf("AT+CMGS=\"%s\"\r%s%c", number, msg, 26) - if resp, err := d.port.Send(cmd); err != nil || !resp.Check() { - if err != nil { - return fmt.Errorf("AT+CGMS= request: %w", err) - } - if errCode, err := GetError(resp.Bytes()); err != nil { - return fmt.Errorf("failed to send with SMS error: %s", DecodeError(errCode)) - } - return fmt.Errorf("failed to send SMS") + d.port.Send(fmt.Sprintf("AT+CMGS=\"%s\"", number)) // Because it will throw error + resp, err := d.port.RawSend(fmt.Sprintf("%s\x1A", msg)); // Add additional \r\n because there is not supposed to be + if err != nil { + return fmt.Errorf("message request: %w", err) } - return nil + if at.Resp(resp).Check() { + return nil + } + errCode, err := GetError([]byte(resp)) + if err != nil { + return fmt.Errorf("send sms failed and failed to get error: %w", err) + } + return fmt.Errorf("failed to send with SMS error: %d - %s", errCode, DecodeError(errCode)) } // Reads all new messages diff --git a/main.go b/main.go index 1eaf633..5b5cdbd 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ func mainE() error { log.Println(m.GetInfo()) log.Println("||||||||||||||||| SEND SMS |||||||||||||||||") - if err := m.Sms().Send("+", "CGSG forever"); err != nil { + if err := m.Sms().Send("+79218937173", "CGSG forever"); err != nil { return err } return nil