Shop OBEX P1 Docs P2 Docs Learn Events
Getting Propeller to communicate with VB.NET using SEROUT (BS2_Functions OBJ) — Parallax Forums

Getting Propeller to communicate with VB.NET using SEROUT (BS2_Functions OBJ)

ryfitzger227ryfitzger227 Posts: 99
edited 2013-06-05 16:48 in Propeller 1
Hey guys.

I'm working on trying to get a Propeller to communicate to a VB.NET program using the BS2_Functions Object from the Object Exchange. I'm fairly new to the Propeller, so I want to make sure that I'm doing everything correctly. Every time I run the program the red light on the QuickStart flashes, so I assume that data gets sent out, but right after that the TextBox on the VB Program stays the same value. Can anyone see what I'm doing wrong??

SPIN
OBJ BS2 : "BS2_Functions"

PUB main
            
  BS2.start(31,30)


  pause(10000)


  BS2.Serout_Str(5,string("Hello World!"),9600,1,8)


PUB pause(ms)
  waitcnt(clkfreq/1000 * ms + cnt)

VB.NET
Public Class Form1

    Private WithEvents COMPort As New IO.Ports.SerialPort


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        If COMPort.IsOpen Then
            COMPort.Close()
        End If
        Try
            With COMPort
                .PortName = "COM7"
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Handshake = IO.Ports.Handshake.None
            End With
            COMPort.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


    End Sub


    Private Sub COMPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles COMPort.DataReceived


        TextBox1.Text = COMPort.ReadLine


    End Sub

End Class

I don't know if I'm doing the Serial Port part on VB the correct way. It's been a while since I've dealt with Serial Ports in VB.NET.

EDIT:
I found the Serial Port code for VB that I used a while back and it works fine with the BS2 using
SEROUT 16, 16468, ["Hello World!"]
but when I use the Propeller code (above) it doesn't do anything but send a '?' to the computer. Am I using the Prop SEROUT correctly?

Comments

  • HughHugh Posts: 362
    edited 2013-06-05 03:48
    Do you have to use SEROUT? If you can cope with different objects I can probably find some VB.net code that I know works...
  • BeanBean Posts: 8,129
    edited 2013-06-05 04:16
    If you run serial terminal does that receive correctly ? If it does, then the problem in in the VB.NET code.

    I've not used the BS2 functions, so I'm wondering what the "5" parameter is for ?

    BS2.Serout_Str(5,string("Hello World!"),9600,1,8)

    Also, you might need to send a carriage return after the string.

    Bean
  • Mike GMike G Posts: 2,702
    edited 2013-06-05 07:00
    Try adding a CON block with the appropriate settings. The following is the "standard" setup.
      CON
      _clkmode = xtal1 + pll16x     
      _xinfreq = 5_000_000
      
    OBJ BS2 : "BS2_Functions"
    
    PUB main
                
      BS2.start(31,30)
    
    
      pause(1000)
    
    
      BS2.Serout_Str(30,string("Hello World!"),9600,1,8)
    
    
    PUB pause(ms)
      waitcnt(clkfreq/1000 * ms + cnt)
    

    Also, this is bad in a Win Form...
        Private Sub COMPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles COMPort.DataReceived
            TextBox1.Text = COMPort.ReadLine
        End Sub
    

    You need a method like the on below to write to the UI from a secondary thread. Your method will work for the most part but it can cause unexpected results from time to time.
            private void SetText(String message)
            {
                try
                {
                    if (txtStatus.InvokeRequired)
                    {
                        SetTextCallback del = new SetTextCallback(SetText);
                        this.Invoke(del, message);
                    }
                    else
                    {
                        txtStatus.AppendText(message);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    
  • ryfitzger227ryfitzger227 Posts: 99
    edited 2013-06-05 13:48
    Thanks guys for your responses. I ended up using the Full Duplex Serial object instead and it worked fine. I also changed up my vb.net code (I found an example that I used a while back with the BS2). Everything is working as it should.
  • groggorygroggory Posts: 205
    edited 2013-06-05 16:15
    Thanks guys for your responses. I ended up using the Full Duplex Serial object instead and it worked fine. I also changed up my vb.net code (I found an example that I used a while back with the BS2). Everything is working as it should.

    Serial comms in vb.net are a bit funky in my experience. Would you mind sharing your vb.net code?
  • ryfitzger227ryfitzger227 Posts: 99
    edited 2013-06-05 16:48
    Not at all. This will work with BS2 and Propeller.
        Private readBuffer As String = String.Empty
        Private CharToRead As String = String.Empty
        Private Bytenumber As Integer
        Private ByteToRead As Integer
        Private byteEnd(2) As Char
        Private comOpen As Boolean
        Private counter As Integer = 0
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
            With comPort
    
    
                .ParityReplace = &H3B
                .PortName = "COM7"
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Handshake = IO.Ports.Handshake.None
                .RtsEnable = False
                .ReceivedBytesThreshold = 1
                .NewLine = vbCr
                .ReadTimeout = 10000
    
    
            End With
    
    
            Try
    
    
                comPort.Open()
                comOpen = comPort.IsOpen
    
    
            Catch ex As Exception
    
    
                comOpen = False
                MsgBox(ex.Message)
    
    
            End Try
    
    
        End Sub
    
    
        Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
    
    
                TextBox1.Text = readBuffer
    
    
        End Sub
    
    
        Private Sub comPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comPort.DataReceived
    
    
            If comOpen Then
    
    
                Try
                    Threading.Thread.Sleep(250) 'This pause can be removed. Just wanted to make sure that everything in the string was being read.
                    byteEnd = comPort.NewLine.ToCharArray
                    Bytenumber = comPort.BytesToRead
                    readBuffer = comPort.ReadExisting
                    Me.Invoke(New EventHandler(AddressOf DoUpdate))
    
    
                Catch ex As Exception
    
    
                    MsgBox(ex.Message)
    
    
                End Try
    
    
            End If
    
    
        End Sub
    
Sign In or Register to comment.