Welcome to EDAboard.com

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.

How to flush InBuffer in MSComm?

Status
Not open for further replies.

vdaniel

Full Member level 4
Joined
Oct 8, 2004
Messages
204
Helped
8
Reputation
16
Reaction score
0
Trophy points
1,296
Activity points
2,157
mscomm1.inputlen

Is there a possibility to flush the InBuffer of the MSComm in Visual Basic 6.0?

Varuzhan
 

picman2

Member level 2
Joined
May 18, 2001
Messages
51
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,286
Activity points
363
mscomm flush

Yes, simply set MSComm property InBufferCount at zero.
 

    vdaniel

    Points: 2
    Helpful Answer Positive Rating

vdaniel

Full Member level 4
Joined
Oct 8, 2004
Messages
204
Helped
8
Reputation
16
Reaction score
0
Trophy points
1,296
Activity points
2,157
mscomm1.rthreshold

Thanks, it helped. Please, where can I read about it?
Thanks,

Varuzhan
 

picman2

Member level 2
Joined
May 18, 2001
Messages
51
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,286
Activity points
363
mscomm inbuffercount

There are so many resources about MSComm... other the brief docs by Microsoft

Simply search by google and you will find many articles and samples on topic.

I would only remark that you should use MSComm only in an event-driven fashion, e.g. do not wait for chars arrival by repeately reading InBufferCount.

Cheers
PicMan
 

vdaniel

Full Member level 4
Joined
Oct 8, 2004
Messages
204
Helped
8
Reputation
16
Reaction score
0
Trophy points
1,296
Activity points
2,157
mscomm rthreshold

Very strange,

This code works well, despite of your recommendation:

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

End If
Wend

But the event-driven works much slower, and hangs at
much less input characters rate

Private Sub MSComm1_OnComm()

Static ADCCode As Integer
Static Ch As Byte
Static Buffer As Variant

Select Case MSComm1.CommEvent
Case comEvReceive

Buffer = MSComm1.Input

Ch = CByte(Buffer(0))
If (Ch >= 48) And (Ch < 58) Then
ADCCode = (10 * ADCCode) + Ch - 48
ElseIf Ch = 10 Then
Cells(1, 1) = ADCCode
Cells(ADCCode + 2, 1) = Cells(ADCCode + 2, 1) + 1
ADCCode = 0
End If

Case Else
End Select

End Sub

Please, can you explain this?

Thank you,

Varuzhan
 

picman2

Member level 2
Joined
May 18, 2001
Messages
51
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,286
Activity points
363
rthreshold mscomm

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 ?

Input can return more than a single char !

PicMan
 

vdaniel

Full Member level 4
Joined
Oct 8, 2004
Messages
204
Helped
8
Reputation
16
Reaction score
0
Trophy points
1,296
Activity points
2,157
mscomm flush buffer

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

End If
Wend
End Sub

Where is the error?
Thanks

Varuzhan
 

picman2

Member level 2
Joined
May 18, 2001
Messages
51
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,286
Activity points
363
flush buffer mscomm

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

PicMan
 

vdaniel

Full Member level 4
Joined
Oct 8, 2004
Messages
204
Helped
8
Reputation
16
Reaction score
0
Trophy points
1,296
Activity points
2,157
mscomm1 inputlen

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?

Thanks
Varuzhan
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top