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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

serial communication program runs too slow 1

Status
Not open for further replies.

javaguy1

Programmer
May 14, 2003
136
US
i changed my serial port program because it occasionally produced bad data. the problem is the new version is too slow.

the molding machine tells the program when it starts a new cycle by sending the shot number. then the program asks for the parameters one after the other and builds the replies into a string and adds a newline at the end. it occasionally gets the whole line and once it had two good lines consecutively, however, even with a 27 second cycle time, the new cycle usually starts before the line gets written to the textbox so two cycles get merged into one line. here is the code any suggestions appreciated.

Private Sub MSComm1_OnComm()
Static strLastCharRecieved As String
Static strRawData As String
Dim strData As String * 15

If MSComm1.CommEvent = comEvReceive Then
If strLastCharRecieved = Chr$(3) Then
strLastCharRecieved = MSComm1.Input
'format and record data
strData = Mid(strRawData, 7) 'should be 6 not 7
strRawData = ""
txtData.Text = strData 'triggers txtData_Change()
Else
strLastCharRecieved = MSComm1.Input
If strLastCharRecieved <> Chr$(3) Then
strRawData = strRawData & strLastCharRecieved
End If
End If
End If
'g_blnRecieveEvent = True
'If MSComm1.CommEvent = comEventOverrun Then
'MsgBox &quot;Port Overrun Error&quot;
'End If
'If MSComm1.CommEvent = comEventFrame Then
'MsgBox &quot;Framing Error&quot;
'End If
End Sub

Private Sub txtData_Change()
Dim strDateTime As String * 16

If m_intCount = 0 Then
strDateTime = Date
m_strRecord = strDateTime
strDateTime = Format(Time, &quot;hh:MM:ss&quot;)
m_strRecord = m_strRecord & strDateTime
End If
m_strRecord = m_strRecord & txtData.Text
m_intCount = m_intCount + 1
Call Request(m_intCount)
End Sub

Private Sub Request(intNumber As Integer)
Select Case intNumber
Case 1
MSComm1.Output = Chr$(4) & &quot;T300&quot; & Chr$(5) 'Request Cycle Time
Case 2
MSComm1.Output = Chr$(4) & &quot;T301&quot; & Chr$(5) 'Request Filling Time
Case 3
MSComm1.Output = Chr$(4) & &quot;T302&quot; & Chr$(5) 'Request Plast Time
Case 4
MSComm1.Output = Chr$(4) & &quot;T307&quot; & Chr$(5) 'Request Screw Time
Case 5
MSComm1.Output = Chr$(4) & &quot;T306&quot; & Chr$(5) 'Request Filling1 Time
Case 6
MSComm1.Output = Chr$(4) & &quot;S303&quot; & Chr$(5) 'Request Cushion Position
Case 7
MSComm1.Output = Chr$(4) & &quot;S304&quot; & Chr$(5) 'Request Hold End Position
Case 8
MSComm1.Output = Chr$(4) & &quot;S302&quot; & Chr$(5) 'Request V-P Change Position
Case 9
MSComm1.Output = Chr$(4) & &quot;S301&quot; & Chr$(5) 'Request Filling Start Position
Case 10
MSComm1.Output = Chr$(4) & &quot;P300&quot; & Chr$(5) 'Request Filling Peak Pressure
Case 11
m_strRecord = m_strRecord & vbNewLine
txtLog.Text = txtLog.Text & m_strRecord
intNumber = 0
m_strRecord = &quot;&quot;
End Select
End Sub
 
i notice one thing myself that might help. since m_intCount is a module level variable, i dont need to pass it to to the Request sub. any other suggestions for improving performance?
 
perhaps making all the variables module level will help. then they would only be created once. or i could declare strData and strDateTime inside the if statement that uses them (bad style but who cares when performance is an issue)
 

javaguy1,

VB 6.0 does not really support conditional declarations (.NET does).

Here are some simple things that may help to speed things up.

1. If you can, declare your variables only once.
2. If you can avoid resetting any variables, do so.
3. Updating controls, especially the visable elements of the controls take the longest (text1.text=text1.text & variable)

And there are a few others but at the moment they do not seem to apply.

Good Luck


 
thanks, that helps. the last one i found out about. thats why i created a record string instead of adding each bit of data to txtLog each time. you first suggestions tells me that i should make all the variables module level.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top