mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-22 07:29:01 +00:00
espat: improve error handling for key TCP functions
Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
+18
-5
@@ -1,7 +1,9 @@
|
|||||||
package espat
|
package espat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,9 +21,12 @@ func (d *Device) ConnectTCPSocket(addr, port string) error {
|
|||||||
protocol := "TCP"
|
protocol := "TCP"
|
||||||
val := "\"" + protocol + "\",\"" + addr + "\"," + port
|
val := "\"" + protocol + "\",\"" + addr + "\"," + port
|
||||||
d.Set(TCPConnect, val)
|
d.Set(TCPConnect, val)
|
||||||
time.Sleep(pause * time.Millisecond)
|
time.Sleep(1000 * time.Millisecond)
|
||||||
d.Response()
|
r := d.Response()
|
||||||
|
if strings.Contains(string(r), "OK") {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("ConnectTCPSocket error:" + string(r))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConnectUDPSocket creates a new UDP connection for the ESP8266/ESP32.
|
// 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"
|
val := "\"" + protocol + "\",\"" + addr + "\"," + sendport + "," + listenport + ",2"
|
||||||
d.Set(TCPConnect, val)
|
d.Set(TCPConnect, val)
|
||||||
time.Sleep(pause * time.Millisecond)
|
time.Sleep(pause * time.Millisecond)
|
||||||
d.Response()
|
r := d.Response()
|
||||||
|
if strings.Contains(string(r), "OK") {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("ConnectUDPSocket error:" + string(r))
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisconnectSocket disconnects the ESP8266/ESP32 from the current TCP/UDP connection.
|
// 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 {
|
func (d *Device) StartSocketSend(size int) error {
|
||||||
val := strconv.Itoa(size)
|
val := strconv.Itoa(size)
|
||||||
d.Set(TCPSend, val)
|
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
|
// ready to receive data
|
||||||
d.Response()
|
r := d.Response()
|
||||||
|
if strings.Contains(string(r), ">") {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("StartSocketSend error:" + string(r))
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndSocketSend tell the ESP8266/ESP32 the TCP/UDP socket data sending is complete,
|
// 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.
|
// and to return to command mode. This is only used in "unvarnished" raw mode.
|
||||||
func (d *Device) EndSocketSend() error {
|
func (d *Device) EndSocketSend() error {
|
||||||
d.Write([]byte("+++"))
|
d.Write([]byte("+++"))
|
||||||
|
time.Sleep(pause * time.Millisecond)
|
||||||
|
|
||||||
// TODO: wait until ">" is received, which indicates
|
// TODO: wait until ">" is received, which indicates
|
||||||
// ready to receive data
|
// ready to receive data
|
||||||
|
|||||||
Reference in New Issue
Block a user