-
March 11th, 2008, 05:41 PM
#1
Help with MsComm
Hi,
I have a new digital compass device as seen here http://www.oceanserver-store.com/osevkitsorus.html. It outputs a string of characters at a rate up to 40hz. I can change the format of the output string, the simplest being a comma delimited string ending with <cr><lf>. The string length will vary. The following is an example of the output from a terminal showing yaw, pitch roll:-
167.5,-4.7,7.4
167.5,-4.7,7.4
167.5,-4.7,7.4
167.5,-4.7,7.4
167.5,-4.7,7.4
167.5,-4.7,7.4
167.4,-4.7,7.4
167.4,-4.7,7.4
167.4,-4.7,7.4
167.4,-4.7,7.4
I am trying to get this data into a VB program and split it up at the commas. I have the MsComm setup:-
Code:
MSComm1.Settings = "115200,N,8,1"
MSComm1.InputLen = 1
MSComm1.RThreshold = 1
and the MSComm1_OnComm() sub is:-
Code:
Private Sub MSComm1_OnComm()
Dim InChar As String * 1
If MSComm1.CommEvent = comEvReceive Then
Do
InChar = MSComm1.Input
If InChar = vbLf Then
' not splitting it up yet, just viewing it in a textbox
Text1.Text = Text1.Text & InString
Else
InString = InString & InChar
End If
Loop While MSComm1.InBufferCount
End If
End Sub
The results I get in the textbox are like this:-
167.3,-4.7,7.2
167.3,-4.7,7.2
167.3,-4.7,7.2
167.3,-4.7,7.2
7.2
7.2
7.2
7.2
7.2
7.2
167.3,-4.7,7.2
7.2
167.3,-4.7,7.2
167.3,-4.7,7.2
167.3,-4.7,7.2
Can anyone see why I am only getting the tail end half of the time? I am hoping someone can help me fix this code so I get all of the string on every line so I can easily split it up.
-
March 11th, 2008, 05:48 PM
#2
Re: Help with MsComm
Why is the buffer only 1 character? You should use a dynamic string, and then use Instr() to check for vbcrlf
-
March 11th, 2008, 06:13 PM
#3
Re: Help with MsComm
Originally Posted by dglienna
Why is the buffer only 1 character? You should use a dynamic string, and then use Instr() to check for vbcrlf
Even though I have been playing with VB6 for quite along time, my understanding is pretty poor. The original code I copied from this forums somewhere, where someone had a similar query.
To be honest, I have no idea how to use a dynamic string as you suggested. I'll do some searching. Thanks, you are always a great help to me.
-
March 11th, 2008, 07:46 PM
#4
Re: Help with MsComm
Look at this: Turns out to be the InBufferSize property of MSComm1, set to 1024, and InputLen set to 0
Code:
Option Explicit
' Select the right COM port in the Properties of the control!
Private Sub Command1_Click()
MSComm1.PortOpen = True
MSComm1.Output = "AT" & Chr$(13)
Do
DoEvents
Loop Until MSComm1.InBufferCount > 1
Text1.Text = MSComm1.Input
End Sub
or from MSDN:
Code:
Private Sub Form_Load ()
' Buffer to hold input string
Dim Instring As String, Buffer as String
' Use COM1.
MSComm1.CommPort = 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.InputLen = 0
' Open the port.
MSComm1.PortOpen = True
' Send the attention command to the modem.
MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that
' the modem responds with "OK".
' Wait for data to come back to the serial port.
Do
DoEvents
Buffer$ = Buffer$ & MSComm1.Input
Loop Until InStr(Buffer$, "OK" & vbCRLF)
' Read the "OK" response data in the serial port.
' Close the serial port.
MSComm1.PortOpen = False
End Sub
Last edited by dglienna; March 11th, 2008 at 08:09 PM.
-
March 11th, 2008, 10:02 PM
#5
Re: Help with MsComm
Thanks, that helped me sort it out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|