Semi debugged sms.
This commit is contained in:
parent
1b741c7dab
commit
d05e2205d6
@ -33,6 +33,7 @@ type Port interface {
|
|||||||
Disconnect() error
|
Disconnect() error
|
||||||
IsConnected() bool
|
IsConnected() bool
|
||||||
|
|
||||||
|
RawSend(msg string) (string, error)
|
||||||
Send(cmd string) (Resp, error)
|
Send(cmd string) (Resp, error)
|
||||||
|
|
||||||
io.Closer
|
io.Closer
|
||||||
@ -89,7 +90,7 @@ 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) RawSend(msg string) (string, error) {
|
||||||
// Write
|
// Write
|
||||||
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)
|
||||||
@ -107,12 +108,12 @@ func (p *atPort) makeReq(msg string) (string, error) {
|
|||||||
|
|
||||||
func (p *atPort) Send(cmd string) (Resp, error) {
|
func (p *atPort) Send(cmd string) (Resp, error) {
|
||||||
cmd += "\r\n"
|
cmd += "\r\n"
|
||||||
rawResp, err := p.makeReq(cmd)
|
rawResp, err := p.RawSend(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return RespNil, fmt.Errorf("make request: %w", err)
|
return RespNil, fmt.Errorf("make request: %w", err)
|
||||||
}
|
}
|
||||||
if len(rawResp) <= 4 {
|
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
|
resp := rawResp[2 : len(rawResp)-2] // Cut \r\n
|
||||||
|
|
||||||
|
@ -291,6 +291,7 @@ func (m *modem) checkPort(portName string) (outErr error) {
|
|||||||
return fmt.Errorf("reset output buffer: %w", err)
|
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("ATE0") // This shit sometimes enables echo mode... why... when... but it can
|
||||||
|
// m.port.Send("\r\n")
|
||||||
|
|
||||||
// Ping
|
// Ping
|
||||||
m.logger.Println("Ping...")
|
m.logger.Println("Ping...")
|
||||||
@ -314,7 +315,7 @@ func (m *modem) checkPort(portName string) (outErr error) {
|
|||||||
}
|
}
|
||||||
rightModel := "SIMCOM_SIM7600E-H"
|
rightModel := "SIMCOM_SIM7600E-H"
|
||||||
// m.logger.Printf("[% x]\n [% x]", []byte("SIMCOM_SIM7600E-H"), []byte(model))
|
// 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)
|
return fmt.Errorf("invalid modem model: %s", model)
|
||||||
}
|
}
|
||||||
m.logger.Println("\x1b[38;2;0;255;0mOK\x1b[38;2;255;255;255m")
|
m.logger.Println("\x1b[38;2;0;255;0mOK\x1b[38;2;255;255;255m")
|
||||||
|
@ -44,17 +44,19 @@ func (d *dialer) Init() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *dialer) Send(number, msg string) error {
|
func (d *dialer) Send(number, msg string) error {
|
||||||
cmd := fmt.Sprintf("AT+CMGS=\"%s\"\r%s%c", number, msg, 26)
|
d.port.Send(fmt.Sprintf("AT+CMGS=\"%s\"", number)) // Because it will throw error
|
||||||
if resp, err := d.port.Send(cmd); err != nil || !resp.Check() {
|
resp, err := d.port.RawSend(fmt.Sprintf("%s\x1A", msg)); // Add additional \r\n because there is not supposed to be
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("AT+CGMS= request: %w", err)
|
return fmt.Errorf("message 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")
|
|
||||||
}
|
}
|
||||||
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
|
// Reads all new messages
|
||||||
|
2
main.go
2
main.go
@ -29,7 +29,7 @@ func mainE() error {
|
|||||||
log.Println(m.GetInfo())
|
log.Println(m.GetInfo())
|
||||||
|
|
||||||
log.Println("||||||||||||||||| SEND SMS |||||||||||||||||")
|
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 err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user