B
<p>...I have checked previous posts and haven't found anything that matches this problem. It is fairly complicated so will try to split it up.
<p>I am programing a Modbus master using VB to communicte with 1 slave. The com port is constantly connected (defined by me) to the slave and I send and receive data using lots of functions.
<p>Although the data stream seems correct (checked using a sniffer), VB seems to be lagging at times and processing a reply to the previous request.
<p>E.g I send "01 04 02 46 00 01 D1 A7", the stream shows the correct reply "01 04 02 00 10 B8 FC", but VB processes the reply to the previous request "01 10 02 00 00 01 00 71".
<p>The input and output buffers are defined local to each function (there are several functions for sending data) so I don't understand how this is possible as theoretically the buffer is 'deleted; as it goes out of scope.
<p>1) Do the buffers need to be global<br>
2) Is data coming in faster than VB can process it and how do I prevent this<br>
3) Do I need to connect and disconnect at the begining and end of each send/receive or is it ok to have the com port constantly connected?<br>
4) Any other ideas??
<p>I do not want to buy a activeX control but would appreciate any other help.
<p>As an example here is a typical function:
<p>Thanks...
<pre>
Private Sub ScanStatus()
MSComm1.Output = Out_Buffer
'TimeOut = 0.05 ' Set wait duration.
Start = Timer ' Set start time.
Do While (Timer < Start + TimeOut ) And _
(MSComm1.InBufferCount < 2 * _
NumberOfDetectorRegisters + 5)
DoEvents
Loop
in_Buffer = MSComm1.Input
DoEvents
Do While Len(In_Buffer) = 0
'there was no reply so try again
For ByteCount = 0 To UBound(Out_Buffer)
txtOutBuffer.Text = txtOutBuffer.Text
& " " & Hex$(Out_Buffer(ByteCount))
Next ByteCount
MSComm1.Output = Out_Buffer
Start = Timer ' Set start time.
Do While (Timer < Start + TimeOut ) And
(MSComm1.InBufferCount < 2 *
NumberOfDetectorRegisters + 5)
DoEvents
Loop
In_Buffer = MSComm1.Input
Loop
'txtInputBuffer.Caption = "
For ByteCount = 1 To Len(In_Buffer)
txtInputBuffer.Text = txtInputBuffer.Text
& " " & Hex$(Asc(Mid$(In_Buffer,
ByteCount, 1)))
If ByteCount > 3 And ByteCount <= (3 +
(NumberOfDetectorRegisters * 2)) Then
'Then process the data here...
next ByteCount
Finish = Timer ' Set end time.
end sub
</pre>
<p>I am programing a Modbus master using VB to communicte with 1 slave. The com port is constantly connected (defined by me) to the slave and I send and receive data using lots of functions.
<p>Although the data stream seems correct (checked using a sniffer), VB seems to be lagging at times and processing a reply to the previous request.
<p>E.g I send "01 04 02 46 00 01 D1 A7", the stream shows the correct reply "01 04 02 00 10 B8 FC", but VB processes the reply to the previous request "01 10 02 00 00 01 00 71".
<p>The input and output buffers are defined local to each function (there are several functions for sending data) so I don't understand how this is possible as theoretically the buffer is 'deleted; as it goes out of scope.
<p>1) Do the buffers need to be global<br>
2) Is data coming in faster than VB can process it and how do I prevent this<br>
3) Do I need to connect and disconnect at the begining and end of each send/receive or is it ok to have the com port constantly connected?<br>
4) Any other ideas??
<p>I do not want to buy a activeX control but would appreciate any other help.
<p>As an example here is a typical function:
<p>Thanks...
<pre>
Private Sub ScanStatus()
MSComm1.Output = Out_Buffer
'TimeOut = 0.05 ' Set wait duration.
Start = Timer ' Set start time.
Do While (Timer < Start + TimeOut ) And _
(MSComm1.InBufferCount < 2 * _
NumberOfDetectorRegisters + 5)
DoEvents
Loop
in_Buffer = MSComm1.Input
DoEvents
Do While Len(In_Buffer) = 0
'there was no reply so try again
For ByteCount = 0 To UBound(Out_Buffer)
txtOutBuffer.Text = txtOutBuffer.Text
& " " & Hex$(Out_Buffer(ByteCount))
Next ByteCount
MSComm1.Output = Out_Buffer
Start = Timer ' Set start time.
Do While (Timer < Start + TimeOut ) And
(MSComm1.InBufferCount < 2 *
NumberOfDetectorRegisters + 5)
DoEvents
Loop
In_Buffer = MSComm1.Input
Loop
'txtInputBuffer.Caption = "
For ByteCount = 1 To Len(In_Buffer)
txtInputBuffer.Text = txtInputBuffer.Text
& " " & Hex$(Asc(Mid$(In_Buffer,
ByteCount, 1)))
If ByteCount > 3 And ByteCount <= (3 +
(NumberOfDetectorRegisters * 2)) Then
'Then process the data here...
next ByteCount
Finish = Timer ' Set end time.
end sub
</pre>