Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling data received from Mscomm

Status
Not open for further replies.

lexer

Programmer
Jun 13, 2006
432
VE
Hi

I'm trying to make a VB program for accounting calls comming from a Nortel Meridian PBX through serial port, This is the program:


Dim Acu As String
Dim Coun As Integer
Dim Buffer As String
Dim Varix As String
Private Sub Form_Load()
MSComm1.CommPort = 1 'Open COM1
MSComm1.PortOpen = True
End Sub
'************Read Data From the PBX
Private Sub MSComm1_OnComm()
Dim VariData As String
Select Case MSComm1.CommEvent
Case comEvReceive
Buffer = MSComm1.Input
VariData = Buffer
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelText = VariData
Call ProcessData(VariData)
End Select
End Sub
'*********Process Received Data
Private Sub ProcessData(VariData As String)
Varix = Varix + VariData 'add data
If InStr(Varix, vbCrLf) Then 'If receive VbCrLf process
'Print "Found VbCrLf"
Print Varix
Varix = ""
Buffer = ""
End If
End Sub


A pbx call has this format:

N 011 00 226 T000002 04/16 16:55 00:00:00 A 92541123
& 0000 0000

The 2 lines from a call ends with VbCrLf, So I would had to receive the information as:

N 011 00 226 T000002 04/16 16:55 00:00:00 A 92541123
& 0000 0000

But I receive:
N 008 00 226
T000005 04/16 16:54 00:00:04 A 92541123
& 0

and in The next call I receive:

000 0000
N 011 00 226
T000002 04/16 16:55 00:00:00 A 92541123
& 0000 0000

I want I join all the data coming from one call in a variable for processing the call information:

Any suggestion? Thanks
 
How about

Case comEvReceive
Buffer = ""
While MSComm1.InBufferCount > 0
Buffer = Buffer & MSComm1.Input
Wend
 
lexer, you still don't seem to have completely taken on board some of the comments from your earlier thread, thread222-1539320. tedsmith, for example, provided code that showed how to accumulate all the data, and even explained the logic behind why he did it that particualr way.

 
Be aware of this potential problem.

I wrote a programme some years ago to do exactly the same thing but discovered that the Norstar PBX was notorious for not logging every call. Things may have improved with their more recent products but if you find that you are dropping calls, don't blame it on your code.

This problem was documented in the Norstar manuals.

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Thanks for your answers, I already found the solution, This is the program:

Dim Acu As String
Dim Coun As Integer
Dim Buffer As String
Dim Varix As String
Dim Temp() As String
Dim VariTemp As String
Private Sub Form_Load()
MSComm1.CommPort = 1 'Open COM1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim VariData As String
Select Case MSComm1.CommEvent
Case comEvReceive
Buffer = MSComm1.Input
VariData = Buffer
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelText = VariData
Call ProcessData(VariData)
End Select
End Sub
Private Sub ProcessData(VariData As String)
Varix = Varix & VariData
If InStr(Varix, vbCrLf) Then
Coun = Coun + 1
Acu = Acu & Varix
If Coun = 2 Then
Temp = Split(Acu, vbCrLf)
VariTemp = Temp(1)
Print VariTemp
Acu = ""
Coun = 0
End If
Varix = ""
Buffer = ""
End If
End Sub

Because Every Call from the PBX has to lines(Every line ends with VbCrLf):

N 067 00 215 T000004 04/17 12:55 00:00:40 A 95839591
& 0000 0000

I store the data from one call until I receive the two VbCrLf (For the every Call), Then I split the data according VbCrLf and use the above line for call accounting:

N 067 00 215 T000004 04/17 12:55 00:00:40 A 95839251

I'm showing the data coming from the PBX on a RichTextBox, How Can I do to empty the RichTextBox after receiving 20 calls?
Can I use?: RichTextBox= ""



 
Richtextbox.Text=""

It would appear that Lexer did follow my example only there happened to be two VBCRLFs in each "packet" that he wanted.

By the way if you just have what you've got there , there is no advantage in having the separate processdata sub - in fact it is slower. You may as well put it all in the OnCom sub.

In general make the On com as small as possible as this fires many times and the processdata should only fire twice.

Dim Acu As String
Dim Coun As Integer
Dim Buffer As String
Dim Temp() As String

Private Sub MSComm1_OnComm()
on error goto DataError
Select Case MSComm1.CommEvent
Case comEvReceive
Buffer = Buffer & MSComm1.Input
If InStr(Buffer, vbCrLf) Then
Call ProcessData(Buffer)
if Len(Buffer)>200 then '(or max length of a complete packet)
Coun = 0
Buffer = ""
End if
End Select
EndOnCom:
On error goto 0
Exit sub
Dataerror:
Coun = 0
Buffer = ""
resume EndOnCom
End Sub

Private Sub ProcessData()
With RichTextBox1
.SelStart = Len(.Text)
.SelText = Buffer
Coun = Coun + 1
Acu=Acu & Buffer
If Coun >1 Then
Temp = Split(Acu, vbCrLf)
VariTemp = Temp(1)
if Coun>1 then
Temp = Split(Acu, vbCrLf)
Print Temp(1)
Coun = 0
Buffer = ""
If Len(.Text)>1000 then 'or whatever
'cut off first 2 lines about 40 lines ago
.Textrtf=mid(.textrtf,Instr(.textrtf,VbCrLf)+3)
.Textrtf=mid(.textrtf,Instr(.textrtf,VbCrLf)+3)
End if

End if
End With
End If
End If
End Sub

If you don't have an error trap you're going to have a lockout on the first data corruption because there will be nothing to clear the buffer or counter if a vbcrlf is never received.
Have fun!
 
Actually you dont need Acu at all, use Buffer instead and don't accumulate it in ProcessData as it is already accumulated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top