diff --git a/espat/tcp.go b/espat/tcp.go index 7cc984e..0007c63 100644 --- a/espat/tcp.go +++ b/espat/tcp.go @@ -1,7 +1,9 @@ package espat import ( + "errors" "strconv" + "strings" "time" ) @@ -19,9 +21,12 @@ func (d *Device) ConnectTCPSocket(addr, port string) error { protocol := "TCP" val := "\"" + protocol + "\",\"" + addr + "\"," + port d.Set(TCPConnect, val) - time.Sleep(pause * time.Millisecond) - d.Response() - return nil + time.Sleep(1000 * time.Millisecond) + r := d.Response() + if strings.Contains(string(r), "OK") { + return nil + } + return errors.New("ConnectTCPSocket error:" + string(r)) } // ConnectUDPSocket creates a new UDP connection for the ESP8266/ESP32. @@ -30,8 +35,11 @@ func (d *Device) ConnectUDPSocket(addr, sendport, listenport string) error { val := "\"" + protocol + "\",\"" + addr + "\"," + sendport + "," + listenport + ",2" d.Set(TCPConnect, val) time.Sleep(pause * time.Millisecond) - d.Response() - return nil + r := d.Response() + if strings.Contains(string(r), "OK") { + return nil + } + return errors.New("ConnectUDPSocket error:" + string(r)) } // DisconnectSocket disconnects the ESP8266/ESP32 from the current TCP/UDP connection. @@ -78,17 +86,22 @@ func (d *Device) GetTCPTransferMode() []byte { func (d *Device) StartSocketSend(size int) error { val := strconv.Itoa(size) d.Set(TCPSend, val) + time.Sleep(pause * time.Millisecond) - // TODO: wait until ">" is received, which indicates + // when ">" is received, it indicates // ready to receive data - d.Response() - return nil + r := d.Response() + if strings.Contains(string(r), ">") { + return nil + } + return errors.New("StartSocketSend error:" + string(r)) } // EndSocketSend tell the ESP8266/ESP32 the TCP/UDP socket data sending is complete, // and to return to command mode. This is only used in "unvarnished" raw mode. func (d *Device) EndSocketSend() error { d.Write([]byte("+++")) + time.Sleep(pause * time.Millisecond) // TODO: wait until ">" is received, which indicates // ready to receive data