+ Reply to Thread
Results 1 to 8 of 8

Thread: Communicating via Serial Port with Visual Basic.NET Program

  1. #1

    Default Communicating via Serial Port with Visual Basic.NET Program

    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:
    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.

    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

  2. #2

    Default Re: Communicating via Serial Port with Visual Basic.NET Program

    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/showthrea...Stamp-Template

  3. #3

    Default Re: Communicating via Serial Port with Visual Basic.NET Program

    Quote Originally Posted by NWCCTV View Post
    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/showthrea...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.

    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..

  4. #4

    Default Re: Communicating via Serial Port with Visual Basic.NET Program

    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.

    Code:
    SEROUT 16, 16572,["RDY",10]
    SERIN 16,16572,15,timeout,[WAIT("!"),DEC4 x,DEC4y]
    timeout:
    PAUSE 15
    RETURN
    VB Code also needs the RDY command:

    Code:
    Do
                in_data = _port.ReadLine()
    LoopUntil in_data = "RDY"
    
            _port.WriteLine("!" & vbLf & servo1 & vbLf & servo2)
    
    EndSub

    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.

  5. #5

    Default Re: Communicating via Serial Port with Visual Basic.NET Program

    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.

  6. #6

    Default Re: Communicating via Serial Port with Visual Basic.NET Program

    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).
    Sapphire

  7. #7

    Default Re: Communicating via Serial Port with Visual Basic.NET Program

    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

    Code:
    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, ['mydata",10,"11.111", 10]

    Changing to SerialPort.NewLine=13 would mean SEROUT 16, 16468, ['mydata",13,"11.111", 13]

    Jeff T.

  8. #8
    Parallax Engineering

    Chris Savage's Avatar
    Location
    Rocklin, CA
    Posts
    12,432
    Blog Entries
    8

    Lightbulb Re: Communicating via Serial Port with Visual Basic.NET Program

    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.
    Chris Savage
    Engineering Tech, Parallax Inc.
    (916) 624-8333 x3005
    Website / Twitter / Google+

+ Reply to Thread

Similar Threads

  1. Serial port recognition in visual basic
    By cheeeseyfry in forum General Discussion
    Replies: 0
    Last Post: 02-23-2012, 02:01 AM
  2. Reading the serial port in Visual Basic 2008??
    By Microcontrolled in forum General Discussion
    Replies: 3
    Last Post: 05-27-2010, 10:19 AM
  3. Replies: 0
    Last Post: 05-17-2009, 09:09 AM
  4. Replies: 2
    Last Post: 12-17-2008, 12:18 AM
  5. communicating with Visual Basic
    By Archiver in forum BASIC Stamps Archive
    Replies: 3
    Last Post: 02-13-2003, 02:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts