DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: RS232 - Reading Serial Port

  1. #1
    Join Date
    Sep 2006

    RS232 - Reading Serial Port

    I'm facing problems with reading data from serial port.
    Im sending out a command to the spectrum analyzer, and start reading. characters appears continously but i still cant read as its in ascii.
    this is what i wrote:

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

    SerialPort1.WriteLine(Chr(82) & Chr(51) & Chr(13) & vbCrLf)

    End Sub

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

    TextBox3.Invoke(New _
    myDelegate(AddressOf updateTextBox3), _
    New Object() {})
    End Sub

    Public Delegate Sub myDelegate()

    Public Sub updateTextBox3()
    With TextBox3
    .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
    .AppendText(SerialPort1.ReadExisting & vbCrLf)
    End With
    End Sub

    here're my questions:
    1)how to convert my data read from port which is in ascii into decimal?
    2)the incoming data is running such as in an overflow state,
    it goes on...

    but i want it to appears as a continuously changing figure.
    like XX.XX (keeps changing)

    How to write this command?

    3) How to write the data read from the serial port into a txt file inside certain folder?

  2. #2
    Join Date
    Dec 2006

    Give this a shot

    This sample will grab a hex data steam from com1 that looks something like this:
    ff 30 31 32 33 34 35 36 37 38 39 31 0d
    then turn it into the acsii equivalent:
    where [] is a little rectangle "junk" character

    In this sample the data in the receive event is grabbed in the "grabSerialData"
    subroutine in this line of code:

    comportData &= SerialPort1.ReadExisting()

    I'll bet that your overflow state can be cured with the values in the
    .ReadBufferSize and .ReceivedBytesThreshold parameters

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    With SerialPort1
    .PortName = "Com1"
    .BaudRate = 300
    .Parity = IO.Ports.Parity.None
    .DataBits = 8
    .StopBits = 1
    .ReadBufferSize = 14 'inbound data stream + 1
    .ReadTimeout = 100
    .ReceivedBytesThreshold = 13 'the length of your inbound data stream
    .Handshake = IO.Ports.Handshake.None
    .RtsEnable = True
    .DtrEnable = True
    '.Encoding = System.Text.Encoding.ASCII (when I leave this out the inbound hex stream turns into ascii)
    End With
    Catch ex As Exception
    End Try
    End Sub

    Public Delegate Sub myDelegate()

    Private Sub OnDataReceived(ByVal sender As Object, _
    ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
    Handles SerialPort1.DataReceived
    End Sub

    Public Sub grabSerialData()
    ' declare variables
    Dim comportData
    Dim intBytesAvailable As Integer = SerialPort1.BytesToRead
    If intBytesAvailable > 0 Then
    comportData &= SerialPort1.ReadExisting()
    End If

    The comportData variable now holds "?01234567891[]" where [] is a little rectangle.
    You would use something like Mid$ to parse out what you want and throw away the ?
    and [], i.e. the "junk" characters.

    Dim arg0, arg1
    arg0 = Mid$(comportData, 12, 1) 'strips out 0123456789 and puts it into arg0
    arg1 = Mid$(comportData, 2, 10) 'strips out 1 and puts it in arg1
    'the ? and [] are not parsed so they just get thrown away

    Then you would use code that looks something something like this to write to
    a flat file on your c: drive:

    Dim objFSO, objFolder, objShell, objTextFile, objFile
    Dim strDirectory, strFile
    strDirectory = "c:\dir"
    strFile = "\file.txt"

    ' Create the File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Check that the strDirectory folder exists
    If objFSO.FolderExists(strDirectory) Then
    Set objFolder = objFSO.GetFolder(strDirectory)
    Set objFolder = objFSO.CreateFolder(strDirectory)
    WScript.Echo "Just created " & strDirectory
    End If

    If objFSO.FileExists(strDirectory & strFile) Then
    Set objFolder = objFSO.GetFolder(strDirectory)
    Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
    Wscript.Echo "Just created " & strDirectory & strFile
    End If

    'set objFile = nothing
    'set objFolder = nothing
    ' OpenTextFile Method needs a Const value
    ' ForAppending = 8 ForReading = 1, ForWriting = 2
    Const ForAppending = 8

    Set objTextFile = objFSO.OpenTextFile _
    (strDirectory & strFile, ForAppending, True)

    ' Writes a new line into open file
    objTextFile.WriteLine(arg0 & strComma & arg1)


    It would be best to make sure C:\dir\file.txt already exists. Also make SURE
    that you have permission to write to file.txt

    Good luck,


Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 01:48 PM
  2. Replies: 1
    Last Post: 02-21-2006, 02:23 AM
  3. Replies: 4
    Last Post: 11-14-2005, 07:48 AM
  4. Replies: 1
    Last Post: 10-06-2005, 02:36 PM
  5. Replies: 0
    Last Post: 05-24-2005, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center