funny looping and serial data problem
Question posted by: Sonoman
(Guest)
on
July 17th, 2005 10:17 PM
Hello everyone:
I am new to Visual Basic and I have a problem that I cannot put my
finger on. I am working on a school project and I am getting some serial
data from a microprocessor. I have designed my "Do" loop to wait for
each data set coming in at intervals of about 1/2 a second from the
microprocessor.
When I comment the following lines:
bufferVar = ""
myBufferData = ""
myDataString = ""
in my code below I get the correct serial data set the first time around
and after that it repeats the same data in the textbox however many
times determined by the "for" loop with only one serial data delay (1/2
a second). BUT if I uncomment these lines I only get the last data set
once after the correct delay determined by the for loop. So, if I have 6
serial data sets my program will run for 3 seconds and display only the
last data set.
What I would like it to do is to print each data set as it arrives for a
long period of time. Any Ideas? Thanks in advance.
Here is my code:
Private Sub Command1_Click(Index As Integer)
'setup the port
If MSComm1.PortOpen = False Then MSComm1.CommPort = 5
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True
MSComm1.InputMode = comInputModeText
For i = 1 To 3
'reset data to enter clean every time
'endFlag used to find out when buffer has one full data set
endFlag = False
'MSComm1.InBufferCount = 0 to start with empty buffer every time
MSComm1.InBufferCount = 0
'start clean every time to extract serial data
'OK, if I comment these three variables I get only the first serial
data set
'repeated however many times is indicated in the "for" loop
'BUT, if I uncomment these three variables I get only the last data set
'with the correct delay (1/2 a sec per iteration of "for" loop)
bufferVar = ""
myBufferData = ""
myDataString = ""
setEnd = 0
'loops until "last item" marker is found
'and sets location value to setEnd
Do
myBufferData = myBufferData & MSComm1.Input
endFlag = InStr(myBufferData, ",")
Loop Until endFlag = True
'locates last item marker on data set
setEnd = InStr(myBufferData, ",")
'gets location markers for x, y and z
xPos = InStr(myBufferData, "x")
yPos = InStr(myBufferData, "y")
zPos = InStr(myBufferData, "z")
'builds buffer data set string
'used to print the variable locations to the screen, not important
bufferVar = bufferVar & "xPos = " & xPos & vbCrLf
bufferVar = bufferVar & "yPos = " & yPos & vbCrLf
bufferVar = bufferVar & "zPos = " & zPos & vbCrLf
bufferVar = bufferVar & "setEnd = " & setEnd & vbCrLf
myDataString = myDataString & "x = " & Mid(myBufferData, (xPos +
1), (yPos - xPos - 1)) & " "
myDataString = myDataString & "y = " & Mid(myBufferData, (yPos +
1), (zPos - yPos - 1)) & " "
myDataString = myDataString & "z = " & Mid(myBufferData, (zPos +
1), (setEnd - zPos - 1)) & vbCrLf
'write to textBoxes
txtData.Text = myDataString
txtBuffer.Text = bufferVar
Next i
'when done close com port
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
End Sub
2
Answers Posted
On Mon, 25 Oct 2004 20:46:12 -0400, Sonoman <billgates@microsoft.com>
you typed some letters in random order:
What do you expect to receive in to the comport exactly ?
If you know how long the string is you are receiving,then try using
the 'MSC_OnComm()' event.
This event gets triggered when the input buffer reaches a certain
size. The size can be set on the 'Rthreshold' property
perhaps this helps
Groetjenz,
Mickey
--
#### gewoan skrieve su ast ut seist ####
mickey wrote:[color=blue]
> On Mon, 25 Oct 2004 20:46:12 -0400, Sonoman <billgates@microsoft.com>
> you typed some letters in random order:
>
> What do you expect to receive in to the comport exactly ?
>
> If you know how long the string is you are receiving,then try using
> the 'MSC_OnComm()' event.
>
> This event gets triggered when the input buffer reaches a certain
> size. The size can be set on the 'Rthreshold' property
>
> perhaps this helps
>
>
>
>
> Groetjenz,
>
> Mickey[/color]
Thank you for your response. I have the Do loop because the length of
the string varies from 7 to 13 characters randomly. Like I said before
this is my first time using VB, so I will look into that MSC_OnComm()
function to see if I could use it.
Anyway, I don't think that is going to solve my data displaying problem.
I spoke to one of my professors today and he pointed out something
interesting about the way I am building my strings. I did not really
understand him then but now I know where to look.
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 196,803 network members.
Top Community Contributors
|