Comm32 Logo
Home Button  Buy Button 

Topic:   Problem with receiving data from instrument on Excel VBA application

By: George MetaxasPosted on: Mar 30 2018 at 07:27:42 AM
I am developing an Excel VBA application where I am reading measurement data from a network analyzer.
For the time being, I am using the trial version of SComm32.
The network analyzer is connected to a PC through a USB-to-GPIB interface by Prologix. In this way, the network analyzer is connected to the PC on a serial port.
The PC runs on Windows 7 Pro 64 bit.
The data are series of ASCII characters that are either 3, 60 or 80 bytes long, always ending with a line feed (ASCII 10) character.

This is how I initialize the SComm32 object that is responsible for communication with the network analyzer:

Set SCommNA = CreateObject("SCommLib.SComm")
SCommNA.CommPort = NACOMPort
With SCommNA
.Settings = "115200,8,N,1"
.InputMode = comInputModeText
.RThreshold = 0 ' set to 0, not needing to catch the comEvReceive event
.InputLen = 1
.OverlappedIO = False
.NullDiscard = False
.Handshaking = comNone
.InBufferSize = 80
End With


and this is how I send a command to read data from the analyzer from within a function where the SCommNA object is passed by reference (ByRef):

Private Sub TuneToChannel(rowNo As Integer, ByRef SCommDUTObject As SCommLib.SComm, ByRef SCommNAObject As SCommLib.SComm)
' ...

' Send a command to NA to get measurement data
noRxBytes = 80 ' expect 80 bytes as an answer
SCommObject.InBufferCount = 0 ' clear RX buffer
SCommObject.OutBufferCount = 0 ' clear TX buffer
strCmd = NA_MARKER_BW_MEAS & vbLf ' there is a set of globals with the commands for the analyzer
SCommObject.Output = strCmd

Do
strRxBuffer = strRxBuffer & SCommObject.Read
DoEvents
Loop Until Len(strRxBuffer) = noRxBytes

' H. Read a response from NA (BW, fc, Q, IL & vbLf)
' Data are stored in strRxBuffer
strNAResponse = strRxBuffer
strRxBuffer = vbNullString ' necessary before the next measurement

' Further processing of strNAResponse string follows
' ...
End Sub

My problem is that every now and then code execution gets stuck in the DoEvents function call.
In an earlier version, I had set .RThreshold property to 1 to catch the comEvReceive event and had a handler for the OnComm event like this:

' Handler for the OnComm event for NA.
Private Sub SCommNA_OnComm()

Dim ch As String
Dim eventVal As Integer

Select Case SCommNA.CommEvent
'Case comEvCTS

'Case comEvDSR

Case comEvReceive
Do While SCommNA.InBufferCount > 0
ch = SCommNA.Read
strRxBuffer = strRxBuffer & ch

If ch = chr$(10) Then ' exit when the last character is line-feed
Exit Do
End If
Loop

Case Is > 1000
eventVal = SCommNA.CommEvent

End Select
End Sub

Any ideas or / and suggestions on how to work around this problem?

By: SupportPosted on: Mar 30 2018 at 12:29:57 PM
Personally I prefer your OnComm version.

Assuming strRxBuffer is declared at the top of the form/module ie outside of any sub or function.
Then each time the OnComm event is trigger append the characters into strRxBuffer as you're doing now.
If you don't get the LF just exit the oncomm event (it's fast so you might get buffer empty between characters but don't worry. The OnComm event will trigger again soon asn you can get more data.
Keep responding to the oncomm event till you get LF. When you get LF you can process the data in strRxBuffer.
Do all that without calling DoEvents.


Reply - add a comment to this topic.

You may enter letters, numbers and standard punctuation only. HTML and other scripts/tags will be rejected.

Topic:- Problem with receiving data from instrument on Excel VBA application


Enter the numbers.

Your name here is optional