Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Serial Port read data


  • Please log in to reply
1 reply to this topic

#1 kalio20

kalio20

    Member

  • Members
  • PipPip
  • 22 posts

Posted 14 April 2015 - 10:52 AM

Hi,

 

my problem is that i have to read 1 single byte from the serial port, is this possible without using the s.port as event or interrupt? Cause I have to decide on my input wich task i want to run, for example input = 1, do blabla, input = 2, do different blabla. Or would u recommend me a better way for my input needings? Sadly there is no input console.

 

Greets 

 

Zoro



#2 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 14 April 2015 - 05:57 PM

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.