Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.
Your code must have bugs, I think.
My own code runs much faster when I rewrote it event driven, and it's the only way to go when there is a delay between enquiry and reply by the serial device.
Otherwise your code waste billion of CPU cycles.
Did you set BOTH InputLen property and RTreshold to 1 ?
Below is all my code with the port initialization, it is very short. Please, check it.
The setting UserForm1.MSComm1.RThreshold = 0
I use now to disable the event-driven procedure.
Public ADCCode As Integer
Public Evens As Integer
Public Ch As Byte
Public Buffer As Variant
Sub DataAcquisition()
Sheets("Sheet1").Select
Dim i As Integer
'UserForm1.InBufferSize = 8192
UserForm1.MSComm1.CommPort = 4
UserForm1.MSComm1.PortOpen = True
UserForm1.MSComm1.Handshaking = comNone
UserForm1.MSComm1.Settings = "115200,N,8,1"
'set for 115200 baud, no parity, 8 bits, 1 stop bit
UserForm1.MSComm1.InputMode = comInputModeBinary
'binary data returned in variant array
UserForm1.MSComm1.InputLen = 1
'set to one character when read by Input
UserForm1.MSComm1.RThreshold = 0
'number of characters to receive before
'generating on comm event
UserForm1.MSComm1.DTREnable = True
UserForm1.MSComm1.RTSEnable = False
UserForm1.MSComm1.InputLen = 1
'transfer all received bytes when read
UserForm1.MSComm1.RThreshold = 0
'interrupt after 1 byte
ADCCode = 0
While bEndFlag = False
DoEvents
If UserForm1.MSComm1.InBufferCount > 0 Then
Buffer = UserForm1.MSComm1.Input
Ch = CByte(Buffer(0))
If (Ch >= 48) And (Ch < 58) Then
ADCCode = (10 * ADCCode) + Ch - 48
ElseIf Ch = 13 Then
ADCCode = ADCCode + 2
Events = Events + 1
Cells(1, 1) = Events
Cells(ADCCode, 1) = Cells(ADCCode, 1) + 1
ADCCode = 0
End If
I was talking about the event driven version. You should be sure that your character processing routine gets only one char strings.
You consume only the first one, and ignore if Buffer has more inside.
ANYWAY
1) You should use Option Explicit
You declare "Evens" but in your code use "Events"
2) The use of
With Userform1.MSComm1
.....
End With
would make your code more readable.
3) These comments are wrong
UserForm1.MSComm1.InputLen = 1 'transfer all received bytes when read
UserForm1.MSComm1.RThreshold = 0 'interrupt after 1 byte
I corrected the definition Events instead of Evens, but nothing changed, when
I write
UserForm1.MSComm1.InputLen = 0 or 1 the same
UserForm1.MSComm1.RThreshold = 1
again the program hangs
then I comment out the readout loop in the while loop,
and leaved only the event driven code, but it gives an overflow error in the line
ADCCode = (10 * ADCCode) + Ch - 48
As my input line can be from 1 to 3 digit + '\n' character I cannot fix the
input string length. That is why I read the input character by character.
What else can I do?
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.