Added read.

This commit is contained in:
Andrey Egorov
2024-07-29 20:03:22 +03:00
parent d05e2205d6
commit fd9e999b5a
4 changed files with 89 additions and 48 deletions

View File

@ -44,8 +44,8 @@ func (d *dialer) Init() error {
}
func (d *dialer) Send(number, msg string) error {
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
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)
}
@ -66,7 +66,18 @@ func (d *dialer) ReadNew() ([]string, error) {
return nil, fmt.Errorf("AT+CMGL request: %w", err)
}
msgs := strings.Split(strings.Replace(string(resp), "\r", "", -1), "\n")
return msgs, nil // TODO
outMsgs := make([]string, 0)
for _, s := range msgs {
if len(s) >= len("+CMGL:") && s[:len("+CMGL:")] == "+CMGL:" {
params := strings.Split(s[len("+CMGL:"):], ",")
d.logger.Println("GET MSG:", params)
} else {
outMsgs = append(outMsgs, s)
}
}
return outMsgs, nil // TODO
}
func (d *dialer) Close() error {