Added signal check.

This commit is contained in:
Andrey Egorov
2024-08-10 15:21:27 +03:00
parent 75d05f197c
commit d21ad0ce00
5 changed files with 169 additions and 28 deletions

View File

@ -37,8 +37,9 @@ type Port interface {
Disconnect() error
IsConnected() bool
RawSend(msg string) (string, error)
RawSend(msg string, timeout time.Duration) (string, error)
Send(cmd string) (Resp, error)
SendWithTimeout(cmd string, timeout time.Duration) (Resp, error)
io.Closer
}
@ -106,7 +107,7 @@ func (p *atPort) IsConnected() bool {
}
// Low level write/read function
func (p *atPort) RawSend(msg string) (string, error) {
func (p *atPort) RawSend(msg string, timeout time.Duration) (string, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
@ -114,7 +115,7 @@ func (p *atPort) RawSend(msg string) (string, error) {
if _, err := p.port.Write([]byte(msg)); err != nil {
return "", fmt.Errorf("serial port write: %w", err)
}
// time.Sleep(time.Millisecond)
time.Sleep(timeout)
// Read
outBuf := make([]byte, 0)
readLoop:
@ -131,13 +132,26 @@ readLoop:
break readLoop
}
}
// p.logger.Println(msg, "\x1b[38;2;150;150;150mRAWREAD:", string(p.inputBuf[:readLen]), "\x1b[38;2;255;255;255m")
// p.logger.Println(msg, "\x1b[38;2;150;150;150mRAWREAD:", string(outBuf), "\x1b[38;2;255;255;255m")
return string(outBuf), nil
}
func (p *atPort) Send(cmd string) (Resp, error) {
rawResp, err := p.RawSend(cmd + "\r\n")
rawResp, err := p.RawSend(cmd+"\r\n", time.Microsecond)
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))
}
resp := rawResp[2 : len(rawResp)-2] // Cut \r\n
return Resp(resp), nil
}
func (p *atPort) SendWithTimeout(cmd string, timeout time.Duration) (Resp, error) {
rawResp, err := p.RawSend(cmd+"\r\n", timeout)
if err != nil {
return RespNil, fmt.Errorf("%s request: %w", cmd, err)
}