SerialPort on Go! socket
#1
Posted 16 July 2012 - 03:12 PM
#2
Posted 16 July 2012 - 11:41 PM
Hi Gorf,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.
Did you connect the GND pin (pin 10) from the socket to your Netduino Plus as well?
Chris
#3
Posted 17 July 2012 - 09:35 AM
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
Posted 17 July 2012 - 11:30 AM
Yay, it's working better for you Good news.
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.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?
If there are zero bytes waiting to be read, you can safely skip on out of the event.
Chris
#5
Posted 17 July 2012 - 01:38 PM
#6
Posted 23 July 2012 - 09:27 AM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users