Go Down

Topic: Sending Data to Serial Port through VB (Read 11119 times) previous topic - next topic

Robert Lee

Andy,

Glad to hear that you got it working!

And no problem with the help.

Rob

andyopayne

Hi Rob,
I had one follow up question I was hoping you might be able to help me with.  I am trying to bring in the accelerometer data from a wii remote into Grasshopper.  I've got 2 lines in my Arduino code that prints 2 different lines, each with a number for the two directions of the accelerometer.
Code: [Select]
Serial.println((byte)accx,DEC);
Serial.println((byte)accy,DEC);

So, when I turn on the Serial Monitor... I get two numbers, each on their own line (which makes sense, because I'm using the println command)... However, when I read this data into Grasshopper... it combines both numbers into 1 larger number... so if my accx number = 172 and my accy number = 105, then the number that comes into Grasshopper is 172105.  If they came into Grasshopper on 2 consecutive lines then I can split the list into 2 and isolate each number... but it's more difficult when the number is combined into one 6 digit number... Is there anything in the code that would be combining the 2 numbers into 1 line?  Here is the code that we came up with.
Code: [Select]
 Sub RunScript(ByVal StepNumber As Integer)
 
   Select Case StepNumber
     Case 0
       OpenPort()
       Print("Port has been opened")
     Case 1
       'Print(StoredValue)
       Print(CInt(Val(StoredValue)))
     'A = StoredValue
     Case 2
       ClosePort()
       Print("Port has been closed")
   End Select
 End Sub

#Region "Additional methods and Type declarations"

 ' I wrapped this in a class, but you would need to work it into the grasshopper class

 Private WithEvents CPort As New IO.Ports.SerialPort        'port with events
 Private TempBuffer As String = String.Empty            'holds the input between events until we get a new line
 Private Delegate Sub HandleComPortDelegate()  'allows com port to run in a background thread without blocking
 Private StoredValue As String = String.Empty  'holds value from port until you call for it

 Sub OpenPort()              'called early in the app, before the port is ever needed. This attaches the port, and opens it to listen.
   Try
     If CPort.IsOpen = False Then
       CPort = My.Computer.Ports.OpenSerialPort("COM3", 9600)
     End If
   Catch ex As Exception
     'MessageBox.Show("no port")
     MessageBox.show(ex.tostring)
   End Try
 End Sub

 'this would be better as a property, but I don't know how Grasshopper handles properties
 Function GetValue() As String
   Return StoredValue
 End Function

 'this is fired by the port when it has data
 Private Sub CPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles CPort.DataReceived
   Dim HndlComPort As New HandleComPortDelegate(AddressOf HandleComPort)             'declare thread
   HndlComPort.Invoke() ' invoke a new delegate to digest the events

 End Sub

 Private Sub HandleComPort()        'background handler for com port

   TempBuffer &= CPort.ReadExisting  'append into tempbuffer
   If (Strings.Right(TempBuffer, 2) = vbCrLf) Then        'if end of item            'this may need to be changed, since Arduino's println may use cr or lf only. I can't remember at this point.
     StoredValue = TempBuffer             'offload
     TempBuffer = String.Empty             'reset
   End If

 End Sub

 'should be called when the port is no longer needed.
 Private Sub ClosePort()
   If CPort IsNot Nothing Then
     If CPort.IsOpen = True Then
       CPort.Close()
     End If
     CPort.Dispose()
   End If
 End Sub
#End Region

Robert Lee

I hadn't considered that, but the point where we convert the string to an integer, it is removing all of the formatting elements of the string, and merging the numbers together.

If you remove the "cint(val(" and the "))" from the print statement you should get a string back.


andyopayne

The problem is that I need the numbers to be numbers and not strings... is there any way to keep then still numbers but keep their formatting?

Robert Lee

Not the way we are doing it now. If I had an accelerometer I'm sure I could read the byte array of the serial buffer and do a much better job of dealing with the incoming data directly. This way you could split the x and y readings when they come in at the port, and put them into two separate variables. Then you could print them both in runScript.

I honestly think that you are at the point where it will be much easier to get that part of the code working in VB, and then copy and paste it back into the script.

I would be happy to provide some more support via email if you wanted to take that direction.

Rob

Go Up