Add: power on with context, SIM808 support
This commit is contained in:
@ -37,7 +37,8 @@ type Port interface {
|
||||
Disconnect() error
|
||||
IsConnected() bool
|
||||
|
||||
RawSend(msg string, timeout time.Duration) (string, error)
|
||||
RawSend(msg string) error
|
||||
RawRead(timeout time.Duration) (string, error)
|
||||
Send(cmd string) (Resp, error)
|
||||
SendWithTimeout(cmd string, timeout time.Duration) (Resp, error)
|
||||
|
||||
@ -106,42 +107,53 @@ func (p *atPort) IsConnected() bool {
|
||||
return p.port != nil
|
||||
}
|
||||
|
||||
// Low level write/read function
|
||||
func (p *atPort) RawSend(msg string, timeout time.Duration) (string, error) {
|
||||
func (p *atPort) RawRead(timeout time.Duration) (string, error) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
// Write
|
||||
if _, err := p.port.Write([]byte(msg)); err != nil {
|
||||
return "", fmt.Errorf("serial port write: %w", err)
|
||||
}
|
||||
time.Sleep(timeout)
|
||||
// Read
|
||||
deadline := time.Now().Add(timeout)
|
||||
outBuf := make([]byte, 0)
|
||||
|
||||
readLoop:
|
||||
for {
|
||||
readLen, err := p.port.Read(p.inputBuf)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("port read: %w", err)
|
||||
}
|
||||
if readLen == 0 {
|
||||
if readLen == 0 && time.Now().After(deadline) {
|
||||
break readLoop
|
||||
}
|
||||
outBuf = append(outBuf, p.inputBuf[:readLen]...)
|
||||
if readLen < len(p.inputBuf) {
|
||||
break readLoop
|
||||
}
|
||||
// if readLen < len(p.inputBuf) {
|
||||
// break readLoop
|
||||
// }
|
||||
}
|
||||
// p.logger.Println(msg, "\x1b[38;2;150;150;150mRAWREAD:", string(outBuf), "\x1b[38;2;255;255;255m")
|
||||
|
||||
return string(outBuf), nil
|
||||
}
|
||||
|
||||
// Low level write/read function
|
||||
func (p *atPort) RawSend(msg string) error {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
// Write
|
||||
if _, err := p.port.Write([]byte(msg)); err != nil {
|
||||
return fmt.Errorf("serial port write: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *atPort) Send(cmd string) (Resp, error) {
|
||||
rawResp, err := p.RawSend(cmd+"\r\n", time.Microsecond)
|
||||
err := p.RawSend(cmd + "\r\n")
|
||||
if err != nil {
|
||||
return RespNil, fmt.Errorf("%s request: %w", cmd, err)
|
||||
}
|
||||
rawResp, err := p.RawRead(ReadTimeout)
|
||||
if err != nil {
|
||||
return RespNil, fmt.Errorf("%s request: %w", cmd, err)
|
||||
}
|
||||
|
||||
if len(rawResp) <= 4 {
|
||||
return RespNil, fmt.Errorf("%s request: read too small msg: %d byte - %s", cmd, len(rawResp), string(rawResp))
|
||||
}
|
||||
@ -151,10 +163,15 @@ func (p *atPort) Send(cmd string) (Resp, error) {
|
||||
}
|
||||
|
||||
func (p *atPort) SendWithTimeout(cmd string, timeout time.Duration) (Resp, error) {
|
||||
rawResp, err := p.RawSend(cmd+"\r\n", timeout)
|
||||
err := p.RawSend(cmd + "\r\n")
|
||||
if err != nil {
|
||||
return RespNil, fmt.Errorf("%s request: %w", cmd, err)
|
||||
}
|
||||
rawResp, err := p.RawRead(timeout)
|
||||
if err != nil {
|
||||
return RespNil, fmt.Errorf("%s request: %w", cmd, err)
|
||||
}
|
||||
|
||||
if len(rawResp) <= 4 {
|
||||
return RespNil, fmt.Errorf("%s request: read too small msg: %d byte - %s", cmd, len(rawResp), string(rawResp))
|
||||
}
|
||||
|
Reference in New Issue
Block a user