• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Receiving ASKEY through the Serial port

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?
  • 2
1 Solution
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
...sorry, you'll need to close it on form exit:

 comWatch.PortOpen = False  
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now