Receiving ASKEY through the Serial port

Posted on 2003-03-04
Medium Priority
Last Modified: 2010-05-01
I have new covered receiving information through the RS232 port so this is probably really simple but I need some help.

I have a device that sends an askey value (Time taken from a stopwatch) to the Rs232 port. I want to load this information into a VB program so that I can calculate the speed the object is going.

So how do you capture this information from a RS232 port, and place in a variable?
Question by:neeveser
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

PNJ earned 200 total points
ID: 8065550
Use MSCOMM32.OCX and place a control onto the form. Set the properties to the correct baud rate and start/stop bit etc. either in code or at design time.

Here's a few code fragments to get you going. This is taken from our crane control system:

in form load:

Private Sub comWatch_OnComm()
   Dim Temp As String

   Select Case comWatch.CommEvent
   Case comEvReceive ' Received RThreshold # of chars.
      ' Grab the string/character
      Temp = GetInputBuffer(vbNullString, comA)
   Case Else

      Temp = "Unknown Event/Error"
      Log Me, LOG_MESSAGE, Temp

   End Select

End Sub

Function GetInputBuffer(Routine As String, ComPort As MSComm) As String

   Dim Temp As Variant, i As Integer, Message As String
   Dim AChar As Integer
   ' Read the Comm Port input buffer, flush it, and log the data received
   ' Return the data as a string
   GetInputBuffer = vbNullString
   If ComPort.InBufferCount = 0 Then Exit Function ' Nothing in input buffer
   Temp = vbNullString
   Do While ComPort.InBufferCount > 0
      Temp = Temp & ComPort.Input
      Message = vbNullString
      For i = 1 To Len(Temp)
         AChar = Asc(Mid$(Temp, i, i))
         Message = Message & IIf(AChar >= 32, Chr$(AChar), "[&H" & Hex(AChar) & "]")
      Next i
      If Routine <> vbNullString Then
         Log Me, LOG_MESSAGE, "From " & Routine & ". Data: " & Message
      End If

   GetInputBuffer = Temp
End Function
Sub SetUpPort()

   On Error GoTo SetUpErr
   With comWatch
      .Handshaking = comNone
      .InputMode = comInputModeText
      ' Use COM3 as default
      .CommPort = 3
      ' Use 1200 baud, even parity, 8 data, and 1 stop bit as default.
      .Settings = "1200,E,8,1"
      .InputLen = 0            ' Tell the control to read entire buffer when Input
                               ' is used.
      .PortOpen = True         ' Open the port.
      .InputLen = 0            ' Read all of the text
      .RThreshold = 1          ' Interrupt every character
   End With
   Exit Sub
   If Err = 8005 Then Resume Next ' Port aleady open
   FatalADOError Err.Number, Err.Description, "SetUpPort", Me.Caption
End Sub

Expert Comment

ID: 8065559
...sorry, you'll need to close it on form exit:

 comWatch.PortOpen = False  
LVL 49

Expert Comment

ID: 9024176
Hi neeveser,
It appears that you have forgotten to close this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept PNJ's comment(s) as an answer.

neeveser, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept THIS comment as an answer.
DanRollins -- EE database cleanup volunteer

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question