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

SerialPort on Go! socket


  • Please log in to reply
5 replies to this topic

#1 Gorf

Gorf

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 16 July 2012 - 03:12 PM

Are there any examples of the VB code for reading serial data from a simple expansion board breaking out the pins from ND Go socket 1? I've just tried opening a serial port, and get 42 zero bytes, then a 250, but then nothing - I've not found out how to specify a particular Go socket. Pins 4 and 5 of the Go (socket 1) are connected to pins 0 and 1 of a ND Plus - which is sending out ten bytes every four seconds.

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 16 July 2012 - 11:41 PM

Are there any examples of the VB code for reading serial data from a simple expansion board breaking out the pins from ND Go socket 1?

I've just tried opening a serial port, and get 42 zero bytes, then a 250, but then nothing - I've not found out how to specify a particular Go socket.

Pins 4 and 5 of the Go (socket 1) are connected to pins 0 and 1 of a ND Plus - which is sending out ten bytes every four seconds.

Hi Gorf,

Did you connect the GND pin (pin 10) from the socket to your Netduino Plus as well?

Chris

#3 Gorf

Gorf

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 17 July 2012 - 09:35 AM

Thanks for the reply Chris - I did connect GND but it might have been a flaky jumper cable. When I've reconnected everything this morning to try again, I started receiving data - albeit not recognisable.

I then found Phillip Morris's thread and code here. It's been a big help. I'm now receiving the data from the ND+

Here's the code so far...
ND+ Sender - emulating a device which will send ten bytes of data every four seconds:
Sub Main()
    Dim Length As Integer = 10
    Dim SerOut(Length) As Byte
    Dim ctr1 As Integer = 0, ctr2 As Integer = 10000
    Dim ghout As String
    Dim temp As Integer
    Dim encoding As New System.Text.UTF8Encoding()

    Dim led As New OutputPort(Pins.ONBOARD_LED, False)
    Dim sp1 As SerialPort
    sp1 = New SerialPort("COM1", 19200, Parity.None, 8, StopBits.One)
    sp1.Open()

    Do
        led.Write(True)
        sp1.Flush()
        ctr1 += 1 : ctr2 -= 1
        If ctr1 = 10000 Then ctr1 = 1 : ctr2 = 9999
        ghout = "0000" & ctr2.ToString
        temp = ghout.Length - 4
        ghout = "0000" & ctr1.ToString & "," & ghout.Substring(temp, 4)
        temp = ghout.Length - 9
        ghout = ghout.Substring(temp, 9) & vbCr
        SerOut = encoding.GetBytes(ghout)
        sp1.Write(SerOut, 0, 10)
        Thread.Sleep(250)
        led.Write(False)
        Thread.Sleep(3750)
    Loop
End Sub


ND Go! receiver. At the moment, this is just picking up the data. The code to actuate relays based on the received data will be integrated into this (Original code by Phillip Morris):
Module Module1
    Dim IncomingText As String = ""

    Dim reading As Boolean = False
    Dim count As Integer
    Dim readLength As Integer
    Dim InTray As Byte()
    Public WithEvents SerialPort As New SerialPort("COM1", 19200, Parity.None, 8, StopBits.One)


    Sub Main()

        SerialPort.Open()
        AddHandler SerialPort.DataReceived, New SerialDataReceivedEventHandler(AddressOf serialPort_Datareceived)
        AddHandler SerialPort.ErrorReceived, New SerialErrorReceivedEventHandler(AddressOf serialPort_ErrorReceived)
        Thread.Sleep(Timeout.Infinite)

    End Sub

    Sub serialPort_ErrorReceived(ByVal sender As Object, ByVal e As SerialErrorReceivedEventArgs)
        Debug.Print(e.ToString)
    End Sub

    Sub serialPort_Datareceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        Debug.Print("Data Received")
        ' Incoming Bytes
        Thread.Sleep(10)
        Dim IncomingBytes As Integer = SerialPort.BytesToRead
        ' Create a new temporary store
        Dim DataStore(IncomingBytes) As Byte
        ' Read in the bytes
        SerialPort.Read(DataStore, 0, IncomingBytes)
        ' Convert Bytes to String
        IncomingText = System.Text.Encoding.UTF8.GetChars(DataStore)
        ' Check for CarrageReturn and trim
        If IncomingText = vbNullString Then
            Debug.Print("Null received")
        Else

            IncomingText = IncomingText.Trim

            ' Do something with the data
            CheckRecievedData(IncomingText)
            IncomingText = ""
        End If



    End Sub

    ' Do something with the data
    Sub CheckRecievedData(ByVal strData As String)
        Debug.Print(strData & " Length=" & strData.Length)
    End Sub

End Module

I am still getting spurious triggers of the data received event, but I can deal with those in code unless anyone can see what I'm doing wrong. Is it the carriage return that's being dealt with as a separate input?

Data Received
Null received
Data Received
0803,9197 Length=9
Data Received
Null received
Data Received
0804,9196 Length=9
Data Received
Null received
Data Received
0805,9195 Length=9
Data Received
Null received
Data Received
0806,9194 Length=9
Data Received
Null received


#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 17 July 2012 - 11:30 AM

Hi Gorf,

Yay, it's working better for you :) Good news.

I am still getting spurious triggers of the data received event, but I can deal with those in code unless anyone can see what I'm doing wrong. Is it the carriage return that's being dealt with as a separate input?

Also, DataReceived events are queued. So if one is queued before you read all the data while in the last event call...the event may be called again.

If there are zero bytes waiting to be read, you can safely skip on out of the event.

Chris

#5 Gorf

Gorf

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 17 July 2012 - 01:38 PM

I shouldn't really have that problem - the serial device I will eventually connect to is going to be sending data so infrequently that it has plenty of time to process a packet when received. If data arrives while it's still working, there's a bigger problem causing it, such as an endless loop. The spurious null being sensed arrives immediately after the valid data - maybe the sleep(10) is allowing all the data to arrive and the first queued read is picking it all up. The second queued read then has nothing to work with?

#6 Gorf

Gorf

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 23 July 2012 - 09:27 AM

Another video - showing the relays reacting to inputs from the ND+ The details of serial data being sent that are shown in the closed captions highlighted that the (almost) binary firing sequence of the relays was happening too early. The observant among you may notice my spoken commentary about what should be happening with regards to the power doesn't match what's actually being sent from the ND+. If anyone's struggling as much as YouTube did to understand my northern accent, let me know and I'll take the time to edit the other CCs.




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.