I was using this with an ESP8266 AT command set driver (since abandonded in favor of the Lua firmware). Just instantiate the port without an event handler and then receive serial in a wait loop.
Public Sub New(ByVal ComPort As String, _
Optional ByVal baud As Integer = 9600)
Port = New SerialPort(ComPort, baud, Parity.None, 8, StopBits.One)
With Port 'note no event handler
.Handshake = Handshake.None
.WriteTimeout = 200
.ReadTimeout = 200
.Open()
End With
InputString = String.Empty
'need to change if not 9600
If (baud <> _DefaultBaud) Then '9600 is default baud rate of ESP8266 firmware (0.9.2.2)
_Baud = baud
End If
End Sub
---------------
Public Function GetFromESP(Eot As String) As String
'this is the main reader for ESP responses. It will wait for the expected Eot or
'a socket exception will be thrown if the response contains, ERROR or Unlink
Dim n As Integer = 0
Dim response As String = String.Empty
InputString = String.Empty
While (True)
If (Port.BytesToRead > 0) Then
Dim buff = New Byte(Port.BytesToRead - 1) {}
n = Port.Read(buff, 0, Port.BytesToRead)
Thread.Sleep(200)
InputString &= New String(Encoding.UTF8.GetChars(buff))
'Debug.Print("Inputstring: " & InputString)
If (Contains(InputString, Eot)) Then 'expected return
Exit While
'Trap for socket connect to inactive server
ElseIf (Contains(InputString, "CIPSTART") And _
(Contains(InputString, "ERROR") _
Or Contains(InputString, "Unlink"))) Then
response = InputString
InputString = String.Empty
Throw (New ESPexception("Socket can't connect: GetFomESP returned ERROR Unlink"))
End If
Thread.Sleep(50) 'need this delay (maybe more)
End If
End While
response = InputString
InputString = String.Empty
Return response
End Function