Semi debugged sms.

This commit is contained in:
Andrey Egorov 2024-07-29 18:53:55 +03:00
parent 1b741c7dab
commit d05e2205d6
4 changed files with 19 additions and 15 deletions

View File

@ -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

View File

@ -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")

View File

@ -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

View File

@ -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