Shop OBEX P1 Docs P2 Docs Learn Events
Reading the serial port in Visual Basic 2008?? — Parallax Forums

Reading the serial port in Visual Basic 2008??

MicrocontrolledMicrocontrolled Posts: 2,461
edited 2010-05-27 03:19 in General Discussion
I have tried many options for this, but can come up with nothing that is compatable with my current setup. I have this code (as a template):

Imports System.IO
Public Class frmMain
    Dim WithEvents SerialPort As New IO.Ports.SerialPort ' serial port declare
    Dim port as string
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            port = InputBox("Select the COM port you wish to open","COM port","COM9")
            SerialPort.ReadTimeout = 100
            SerialPort.PortName = port
            SerialPort.BaudRate = "9600" ' 38400
            SerialPort.Parity = IO.Ports.Parity.None ' no parity
            SerialPort.DataBits = 8 ' 8 bits
            SerialPort.StopBits = IO.Ports.StopBits.One ' one stop bit
            SerialPort.ReadTimeout = 1000 ' milliseconds so times out in 1 second if no response
            SerialPort.Open() ' open the port
            SerialPort.DiscardInBuffer() ' clear the input buffer
            SerialPort.Write("Hello World")
            SerialPort.Close()
End Sub

I need to have a function that waits until a serial signal is received in the USB port and then stores the following string of bytes into a String. Can someone help get this working with my current code?

Thanks,
Micro

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Check out my new website!!

Use the Propeller icon!! Propeller.gif

Follow me on Twitter! Search "Microcontrolled"

Comments

  • bambinobambino Posts: 789
    edited 2010-05-27 00:36
    I am working with some code that may get you started in this post:

    http://forums.parallax.com/showthread.php?p=908941
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-05-27 03:11
    1) for strings, right at the top
    Imports System.IO
    Imports Strings = Microsoft.VisualBasic ' so can use things like left( and right( for strings

    2) for reading bytes, a buffer at the beginning of frmmain

    Dim InPacket(0 To 2000) As Byte

    3) if you get sick of typing in the com port every time you start - put it in a startup form

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

    and you can set the timer interval here too
    Timer1.Interval = 150 ' check for new characters every 150ms

    4) a timer - and you need to disable the timer while the timer does things in case it causes reentry code
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    ' collect bytes from the serial port
    Timer1.Enabled = False
    .... your code ...
    Timer1.Enabled = True
    End Sub

    5) call this subroutine from your timer
    Sub ReadSerialPort()
    Dim BytesToRead As Integer ' may as well get in batches
    Dim i as integer
    Do
    If SerialPort1.BytesToRead = 0 Then Exit Do ' no more bytes
    BytesToRead = SerialPort1.BytesToRead
    If BytesToRead > 2000 Then BytesToRead = 2000
    SerialPort1.Read(InPacket, 0, BytesToRead) ' read in a packet
    For i = 1 To BytesToRead
    .. process the bytes here, turn them into a string etc ...
    Next i
    Loop

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.smarthome.viviti.com/propeller
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2010-05-27 03:19
    Hi Microcontrolled , I have read your posts in the Propeller forum with· interest , I did something similar for the Stamp at this link http://forums.parallax.com/showthread.php?p=671804

    I know how difficult it can be to choose the right examples and to avoid mistakes but its a great self teaching experience.

    There are so many options for reading and writing data to and from the serial port that it really becomes application dependent.

    When you say you want to store the serial data from USB to a string I am assuming the string is coming from the Prop to a VB application. If that is the case it happens to be one of the easier examples of VB receiving data. In the code below what you refer to as a signal I have called the Header , the Header is supposed to be a unique string that tells VB that what immediately follows is the string data to store or in this example display. The instruction used to read both the string data and the Header is the serial port ReadLine , Readline reads from the serial port buffer until it reaches a NewLine character ( 10 ) , the NewLine acts as the string terminator. So say you want to transmit "Hello World" from Prop to PC you would transmit two strings "Header" and "Hello World" each terminated with a NewLine like this "Header",10,"Hello World",10 . To contradict that a little what I would prefer to transmit is 10,"Header",10,"Hello World",10 .

    Here's a basic example thats enclosed in the DataReceived event of your serial port . One thing you must bear in mind is that the DataReceived event runs in a thread separate from the main thread this prevents you from directly accessing Controls created in the main thread from the DataReceived event ( in this example DataReceived can not directly access the RichTextBox ) . You would normally use a Delegate but to keep this example as simple as possible insert the following line into Form1 Load event CheckForIllegalCrossThreadCalls=False , this will prevent exceptions
    [color=#0000ff][color=#0000ff]Private[/color][/color] [color=#0000ff][color=#0000ff]Sub[/color][/color][color=#000000] SerialPort1_DataReceived([/color][color=#0000ff][color=#0000ff]ByVal[/color][/color][color=#000000] sender [/color][color=#0000ff][color=#0000ff]As[/color][/color] [color=#0000ff][color=#0000ff]Object[/color][/color][color=#000000], [/color][color=#0000ff][color=#0000ff]ByVal[/color][/color][color=#000000] e [/color][color=#0000ff][color=#0000ff]As[/color][/color][color=#000000] System.IO.Ports.SerialDataReceivedEventArgs) [/color][color=#0000ff][color=#0000ff]Handles[/color][/color][color=#000000] SerialPort1.DataReceived[/color]
    [color=#0000ff][color=#0000ff]Dim[/color][/color] Header [color=#0000ff][color=#0000ff]As[/color][/color] [color=#0000ff][color=#0000ff]String[/color][/color] = [color=#a31515][color=#a31515]"Header"[/color][/color]
    [color=#0000ff][color=#0000ff]Dim[/color][/color] InString [color=#0000ff][color=#0000ff]As[/color][/color] [color=#0000ff][color=#0000ff]String[/color][/color] = [color=#a31515][color=#a31515]""[/color][/color]
    [color=#0000ff][color=#0000ff]Try[/color][/color]
    SerialPort1.ReadTimeout = 100
    InString = SerialPort1.ReadLine
    [color=#0000ff][color=#0000ff]If[/color][/color] InString = Header [color=#0000ff][color=#0000ff]Then[/color][/color]
    InString = SerialPort1.ReadLine
    RichTextBox1.AppendText(InString)
    [color=#0000ff][color=#0000ff]End[/color][/color] [color=#0000ff][color=#0000ff]If[/color][/color]
    [color=#0000ff][color=#0000ff]Catch[/color][/color] ex [color=#0000ff][color=#0000ff]As[/color][/color] Exception
    [color=#0000ff][color=#0000ff]End[/color][/color] [color=#0000ff][color=#0000ff]Try[/color][/color]
    [color=#0000ff][color=#0000ff]End[/color][/color] [color=#0000ff][color=#0000ff]Sub[/color][/color]
    
    

    ·Create your application as you normally would and add a RichTextBox to display the incoming data.

    Good luck with your Prop / VB documents , I will continue to follow withe interest

    Jeff T.
Sign In or Register to comment.