Shop OBEX P1 Docs P2 Docs Learn Events
Communicating via Serial Port with Visual Basic.NET Program — Parallax Forums

Communicating via Serial Port with Visual Basic.NET Program

ryfitzger227ryfitzger227 Posts: 99
edited 2012-11-27 12:37 in BASIC Stamp
Hello everyone!

I've been working with serial port communication and I have came to a point to where I'm completely stuck.

For some reason I can send data to the BS2, and the BS2 will receive it. However, the VB.NET Program isn't receiving it. On my BOE Board both the blue and pink LEDs are lighting up, telling me the information has sent, but I'm just not convinced. Please look over my code so you can see what I'm doing wrong.

The VB.NET Program will "Write" a string of "1". When the BS2 receives that, it needs to respond with "Hello". The VB.NET program will receive it and put that message into a textbox.

BS2 Code:
' {$STAMP BS2}' {$PBASIC 2.5}


SerialInput VAR Byte


Main:


SERIN 16, 16468, [STR SerialInput\1]


IF SerialInput = "1" THEN
SEROUT 16, 16468, ["HELLO"]


GOTO Done


ENDIF


GOTO Main


Done:


END

If you have any knowledge of VB.NET, I also posted that code.
Imports System.IO.Ports

Public Class Form1


    Private WithEvents comPort As New IO.Ports.SerialPort
    Private dataIn As String
    Private readBuffer As String = String.Empty
    Private Bytenumber As Integer
    Private ByteToRead As Integer
    Private byteEnd(2) As Char
    Private comOpen As Boolean


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


        With comPort


            .ParityReplace = &H3B                    ' replace ";" when parity error occurs 
            .PortName = "COM5"
            .BaudRate = 9600
            .Parity = IO.Ports.Parity.None
            .DataBits = 8
            .StopBits = IO.Ports.StopBits.One
            .Handshake = IO.Ports.Handshake.None
            .RtsEnable = False
            .ReceivedBytesThreshold = 1             'threshold: one byte in buffer > event is fired 
            .NewLine = vbCr         ' CR must be the last char in frame. This terminates the SerialPort.readLine 
            .ReadTimeout = 10000


        End With


        Try
            comPort.Open()
            comOpen = comPort.IsOpen
        Catch ex As Exception
            comOpen = False
            MsgBox("Error Open: " & ex.Message)
        End Try


    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        comPort.Write("1")


    End Sub


    Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)


        If comOpen Then
            Try
                byteEnd = comPort.NewLine.ToCharArray
                Bytenumber = comPort.BytesToRead
                readBuffer = comPort.ReadLine
                Me.Invoke(New EventHandler(AddressOf DoUpdate))
            Catch ex As Exception
                MsgBox("Read " & ex.Message)
            End Try
        End If


    End Sub


    Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBox1.Text = readBuffer
    End Sub
End Class

Please help! What am I doing wrong?!

Thanks!
- Ryan

Comments

  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-11-21 18:01
    If I am not mistaken your BS2 program needs a wait command, I will dig up old posts pertaining to this and post a link as soon as I find it.

    Edit: Browse through this thread. It has pretty much everything you need for doing what you want to do,

    http://forums.parallax.com/showthread.php?96973-VB-Express-to-Stamp-Template
  • ryfitzger227ryfitzger227 Posts: 99
    edited 2012-11-21 18:18
    NWCCTV wrote: »
    If I am not mistaken your BS2 program needs a wait command, I will dig up old posts pertaining to this and post a link as soon as I find it.

    Edit: Browse through this thread. It has pretty much everything you need for doing what you want to do,

    http://forums.parallax.com/showthread.php?96973-VB-Express-to-Stamp-Template

    While I was waiting, I actually did some searching on google and found out my problem. I was using the serialport.readline in vb.net. This reads up to the next line. So what I need to add was CR after the SEROUT message. Example: SEROUT 16, 16468, ["HELLO WORLD!", CR]. This worked.

    Here's my only problem that I need help with. Every time I write to the COM Port and send it, when it returns to the VB Program, it has the original string at the beginning of the line. Sounds confusing. Let me solve that with some code.
    ' {$STAMP BS2}' {$PBASIC 2.5}
    
    
    SerialInput VAR Byte
    
    
    Main:
    
    
    SERIN 16, 16468, [SerialInput]
    
    
    IF SerialInput = "1" THEN
    SEROUT 16, 16468, ["11.111", CR]
    ELSEIF SerialInput = "2" THEN
    SEROUT 16, 16468, ["22.222", CR]
    ELSEIF SerialInput = "3" THEN
    SEROUT 16, 16468, ["33.333", CR]
    ENDIF
    
    
    GOTO Main
    

    Basically, I use the SerialPort.Write in VB.NET. This writes to the BS2 a number. In this case, "1", "2", or "3". Next it will send that number multiple times with a decimal after the first two digits. When I send "1", for example, the BS2 receives that, and then sends "11.111". But when it shows up on the vb.net program, it reads 111.111. It's keeping that first string I sent from the program. How do I clear the SerialPort on the BS2? That way the serial port is clear when I send it, therefore it only shows what I wanted it to show in the first place..
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-11-21 18:21
    I am pretty sure that is where the RDY and WAIT commands come in to play. I modified the code from that thread and was able to get both Servos and Steppers to work using it.
    [SIZE=2][FONT=Times New Roman][COLOR=#000000]SEROUT 16, 16572,["RDY",10][/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Times New Roman][COLOR=#000000]SERIN 16,16572,15,timeout,[WAIT("!"),DEC4 x,DEC4y][/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Times New Roman][COLOR=#000000]timeout:[/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Times New Roman][COLOR=#000000]PAUSE 15[/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Times New Roman][COLOR=#000000]RETURN[/COLOR][/FONT][/SIZE]
    

    VB Code also needs the RDY command:
    [FONT=Courier New][COLOR=blue]Do[/COLOR][/FONT]
    [FONT=Courier New][COLOR=#000000]            in_data = _port.ReadLine()[/COLOR][/FONT]
    [FONT=Courier New][COLOR=blue]Loop[/COLOR][COLOR=blue]Until[/COLOR][COLOR=#000000] in_data = [/COLOR][COLOR=maroon]"RDY"[/COLOR][/FONT]
    
    [FONT=Courier New][COLOR=#000000]        _port.WriteLine([/COLOR][COLOR=maroon]"!"[/COLOR][COLOR=#000000] & vbLf & servo1 & vbLf & servo2)[/COLOR][/FONT]
    
    [FONT=Courier New][COLOR=blue]End[/COLOR][COLOR=blue]Sub[/COLOR][/FONT]
    


    This is just examples from the .doc file in that thread. You will need to modify your code accordingly. The stamp can not send and receive data simultaneously and therefor your VB program needs to tell the stamp it is ready to send/rcv and vice versa. At least, that is the way I understood it when I had similar issues.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-11-21 18:42
    The original VB.net program was written by Jeff, AKA unsouncode. If you can not get it figured out maybe PM him and I am sure he would be more than happy to help you out. He has helped me dramatically in the past with BS2/VB projects.
  • SapphireSapphire Posts: 496
    edited 2012-11-21 19:41
    The reason you are getting what you send from the PC to the BS2 is because you are using the programming port on the BS2. The way this port is built causes it to echo everything it receives. So in reality, you are getting what you sent and the BS2's response. The only way around this would be to use two I/O pins for Tx and Rx instead of the programming port (pseudo pin 16).
  • UnsoundcodeUnsoundcode Posts: 1,530
    edited 2012-11-22 11:33
    I don't have time right now for a complete answer but in the mean time think about using headers to seperate your data.

    Everything with a header is read as data everthing else is discarded, including the echo

    For example
    IF SerialInput = "1" THEN
    SEROUT 16, 16468, ['mydata",CR,"11.111", CR]
    ELSEIF SerialInput = "2" THEN
    SEROUT 16, 16468, ['mydata",CR,"22.222", CR]
    ELSEIF SerialInput = "3" THEN
    SEROUT 16, 16468, ['mydata",CR,"33.333", CR]
    ENDIF
    

    VB looks for the "mydata" header with ReadLine and knows the following string is valid data.


    In VB Readline looks for a string terminated with a Newline character which has a default value of 10, Carriage return has a value of 13. The default NewLine value can be modified in VB

    eg SerialPort1.NewLine=10 or SerialPort.NewLine=13

    This means the default would be SEROUT 16, 16468,

    Changing to SerialPort.NewLine=13 would mean SEROUT 16, 16468,

    Jeff T.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2012-11-27 12:37
    When you say that what you're sending to the BASIC Stamp module seems to be in your input buffer that makes complete sense. The BASIC Stamp modules have a 4.7K resistor between their SIN/SOUT lines, which causes echo. So from the VB application perspective what you send you also receive. From the BASIC Stamp module perspective this never happens because the BASIC Stamps module does not have a serial buffer and since it can perform only one task at a time, all data sent is sent before you can SERIN and see it.
Sign In or Register to comment.