MScomm1/splitdata problem
MScomm1/splitdata problem
MScomm1/splitdata problem
MScomm1/splitdata problem
MScomm1/splitdata problem
MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem
MScomm1/splitdata problem MScomm1/splitdata problem
MScomm1/splitdata problem
Go Back  Xtreme Visual Basic Talk > > > MScomm1/splitdata problem


Reply
 
Thread Tools Display Modes
  #1  
Old 08-30-2007, 01:01 PM
teamtwf8 teamtwf8 is offline
Centurion
 
Join Date: Feb 2007
Posts: 107
Default MScomm1/splitdata problem


Hi,

Maybe someone has more knowledge as me.
I scammed the net for many samples and I know I am close but now stuck. On the Dutch forum they cant help me any more so I have to seek help somewhere else.

For my Modlecar engine dyno project I already tried USB and parallel data logging but they failed for accuraccy or had terrible jitter. So I had to go to serial communication by RS232.

What is the problem?
I communicate with a serial port to an IC called UFDC-1.
http://www.sensorsportal.com/DOWNLOADS/UFDC_1.pdf

This IC can easily record prequencies for RPM collection.
I can set up the IC with the Mscomm1 commands.
I also get data returned with Mscomm1.input call.
However to work on the data that is received in the buffer is my bottle neck. I do not know how to handle.

Setting:
Code:
MSComm1.InputLen = 0 'number of characters on which oncomm event fires     No idea if this is correct
    MSComm1.RThreshold = 20 'number of characters to receive                   No idea if this is correct
    MSComm1.SThreshold = 1 'number of characters to?
I do not know if these are set correct or how to set.

The code I send to the IC is like this:
Code:
Private Sub cmdSingleClick_Click() 'manual collection data by single click

MSComm1.Output = "M0" & Chr(13) 'sets UFDC-1 to read Freq on channel-1
MSComm1.Output = "S" & Chr(13) 'starts RPM measurement at UFDC-1
MSComm1.Output = "R" & Chr(13) 'starts RPM collecting as BCD ASCII by UFDC-1

End Sub
First problem is I get the send codes M0 S and R returned into the mscomm1.input a textbox shows something like this: >M0 >S >R 2.833455171905
The spaces are in the textbox thick black vertical stripes and are chr(13).
Can this be prevented?

What I need is only the numerical data from the IC.
I already made a routine to rip all charaters and chr(13) from the string and by splitting on the "."
The data in the bufffer is no fixed length as long as I get the send codes returned.

According to the UFDC-1 manual the data put in the buffer is BCD- ASCII but I do not know if the value 2.833455171905 is a BCD ASCII value.
What this value should represent is a frequency which I need to be muliply by 60 to get RPM values.
But as I do not know if this 2.833455171905 BCD ASCII or whatever this is an other problem.

here is the full code I have made for what is important for the UFDC-1 to colelct the frequencies.
Code:
Private Sub CmdComConnect_click()
    
    If MSComm1.PortOpen = True Then
    MSComm1.PortOpen = False 'Close selected comport if in use already
    End If
    
    '2400 bps initial setting
    MSComm1.Settings = "2400,N,8,1" 'set portsettings for initial communication with UFDC-1
    MSComm1.CommPort = 1 'cmbComPorts.Text
    MSComm1.InputLen = 0 'number of characters on which oncomm event fires     No idea if this is correct
    MSComm1.RThreshold = 20 'number of characters to receive                   No idea if this is correct
    MSComm1.SThreshold = 1 'number of characters t                             No idea if this is correct
    MSComm1.InputMode = comInputModeText
    MSComm1.PortOpen = True 'Open selected comport
    MSComm1.Output = "A6" & Chr(13) 'sets accuracy to 0.01%
    MSComm1.Output = "R" & Chr(13) 'sets data to BCD ASCII
    
End Sub

Private Sub MSComm1_OnComm()
Dim i, sBuffer As String
Dim sTemp1 As Variant

Select Case MSComm1.CommEvent
  Case comEvReceive
    
    'For i = 1 To Len(sBuffer)
    
    sBuffer = MSComm1.Input
    Text1.Text = sBuffer 'display receive buffer content
    sTemp1 = Split(sBuffer, ".") 'split the data on "."
    Text2.Text = Replace(sTemp1(0), Chr(13), "") 'left hand side data
    Text2.Text = Replace(Text2.Text, "A6", "")
    Text2.Text = Replace(Text2.Text, ">", "")
    Text2.Text = Replace(Text2.Text, "R", "")
    Text2.Text = Replace(Text2.Text, "R0", "")
    Text2.Text = Replace(Text2.Text, "M", "")
    Text2.Text = Replace(Text2.Text, "M0", "")
    Text2.Text = Replace(Text2.Text, "S", "")
    Text2.Text = Replace(Text2.Text, "A", "")
        
    Text3.Text = Replace(sTemp1(1), Chr(13), "") 'right hand side data
    Text3.Text = Replace(Text3.Text, "A6", "")
    Text3.Text = Replace(Text3.Text, ">", "")
    Text3.Text = Replace(Text3.Text, "R", "")
    Text3.Text = Replace(Text3.Text, "R0", "")
    Text3.Text = Replace(Text3.Text, "M", "")
    Text3.Text = Replace(Text3.Text, "M0", "")
    Text3.Text = Replace(Text3.Text, "S", "")
    Text3.Text = Replace(Text3.Text, "A", "")
    
    
    Text4.Text = Text2.Text & "." & Text3.Text 'Merge slitted numbers data left and right from . DOT
    'Text5.Text = Format(Text4.Text * 60, "00000.00"'show the rpm data in the textbox.
    'ARProgressBarEngine.Value = Format(Text5.Text, "00000") 'show the rpm data in a bargraph.
    
    'Next
    
    Case comEventRxOver
    Text5.Text = " receive buffer overflow"

  End Select
End Sub

Private Sub cmdSingleClick_Click() 'manual collection data by single click

MSComm1.Output = "M0" & Chr(13) 'sets UFDC-1 to read Freq on channel-1
MSComm1.Output = "S" & Chr(13) 'starts RPM measurement at UFDC-1
MSComm1.Output = "R" & Chr(13) 'starts RPM collecting as BCD ASCII by UFDC-1

End Sub

Private Sub cmdTimerStart_Click() 'auto collection data
timSample.Interval = 500
timSample = True
End Sub

Private Sub timSample_Timer()

        MSComm1.Output = "M0" & Chr(13) 'sets UFDC-1 to read Freq on channel-1
        MSComm1.Output = "S" & Chr(13) 'starts RPM measurement at UFDC-1
        MSComm1.Output = "R" & Chr(13) 'starts RPM collecting as BCD ASCII by UFDC-1

End Sub

Private Sub cmdComDisconnect_Click()
MSComm1.PortOpen = False
timSample = False

End Sub
Any help in the right direction would be appriciated.

Paco
Reply With Quote
  #2  
Old 08-31-2007, 04:43 PM
RoofRabbit's Avatar
RoofRabbit RoofRabbit is offline
Contributor
 
Join Date: Sep 2003
Location: Lenoir, NC - USA
Posts: 731
Default

I think the data you are getting is in binary format with text inside it (the dark vertical lines are non-displayable characters). If you can determine for format of it's data packet, you should be able to define a custom user data type to handle it. It probably has numerical data (Integer, Long or even Double) mixed with String data.

If you can't find info on it's data structure and can't figure it out on your own, see if you can get the complete block of data into a byte array and post back here again. I might be able to figure out it's structure from the data in that array (Don't post a very large block of data, just inform us you have it).
__________________
Website http://roofrabbit.com/
Reply With Quote
  #3  
Old 09-01-2007, 05:17 AM
syurish syurish is offline
Newcomer
 
Join Date: Sep 2007
Posts: 4
Default

1) Fist of all use an appropriate command to select the relavive error (A1, A2, A3, etc.). By default it is only 1 %.

2) In the Rn command description instead of '0 - BCD ASCII' must be '0- Decimal ASCII'.

Good luck.

- End -
Reply With Quote
  #4  
Old 09-01-2007, 09:20 AM
teamtwf8 teamtwf8 is offline
Centurion
 
Join Date: Feb 2007
Posts: 107
Default

Hi Roofrabbit,

According the UFDC-1 manual the ic can be set up to send 4 types of data.

Rn Get measurement result in format number 'n' (0 ≤ n ≤3)
0 - BCD ASCII
1 - BCD HEX
2 - BIN HEX
3 - BIN

R The same as R0

As you can see in my current code I use the option to just send the "R" as this should result in BCD ASCII.

The unknown charaters are certainly VbCr's.
As currently I get all the send commands echod back I know that for sure as they all return in the same format as the sended code.
The ">" character returned is the signal from the UFDC-1 that it is ready to receive other to be send commands.
When I use a terminal program I get the same commands, but the result as digits only. So the guy who made the terminal program has done in code what I need to do too. But I only have the .exe file no sourcecode.

My first concern is to get rit of the echod commands or am I mistaken and will does always be returned?
If the commands are not returned then splitting data or working on it would be easier what ever format they are as you no not have to get rid of charaters and numbers and can concentrate on the real data needed.

So therefor I placed my problem in serial communication section.
There must be someone with common knowledge of Mscomm1

I do have some VB knowledge/skills but if you are talking about arrays, bits and bytes I mostly only understand if I see a sample code.
All my knowledge is try and error so far with this UFDC-1.
The code above is purely made with samples I took from the net.

Paco
Reply With Quote
  #5  
Old 09-01-2007, 10:04 AM
teamtwf8 teamtwf8 is offline
Centurion
 
Join Date: Feb 2007
Posts: 107
Default

Syurish,

Thanks for dropping in here.
You have more knowledge of this UFDC-1 I think then anyone else?

So the UFDC-1 manual is not correct for the R setting!
So what exits the UFDC-1 with option R0 = DECIMAL ASCII

If you look at my code when I connect the UFDC-1 I set the accuracy at with A6 to 0.01% once only.
Or are you telling me I have to send all the codes everytime to the UFDC-1 like this in the command click or timer?

Code:
Private Sub cmdSingleClick_Click() 'manual collection data by single click

MSComm1.Output = "MA" & Chr(13) 'sets UFDC-1 to read RPM on channel-1
MSComm1.Output = "A6" & Chr(13) 'sets UFDC-1 accuracy on channel-1 (added)
MSComm1.Output = "Z0" & Chr(13) 'sets number of black/white parts of sensor object (added)
MSComm1.Output = "S" & Chr(13) 'starts RPM measurement at UFDC-1
MSComm1.Output = "R" & Chr(13) 'starts RPM collecting as DECIMAL ASCII by UFDC-1

End Sub
Please confirm.

The simple sample in the UFDC-1 manual is very basic and the TERMINAL.exe is not available as sourcecode so I can break it down to what I only need.
If you are programmer for profession all might look so simple!

I will try to make a screen shot of the code in the text box. (done / editted)
Top textbox has the returned code with M0>........
The second textbox has the code returned as Decimal ASCII?

http:\\www.twf8.ws\sc1.jpg

I will have a look again on the code if I have the time.
I am on holiday from 3 -9 till 19-9

Paco

Last edited by teamtwf8; 09-02-2007 at 12:32 AM.
Reply With Quote
  #6  
Old 09-02-2007, 01:08 PM
teamtwf8 teamtwf8 is offline
Centurion
 
Join Date: Feb 2007
Posts: 107
Default

http:\\www.twf8.ws\sc2.jpg
screenshot of the termial.exe
top receive screen/ bottom send screen

Paco
Reply With Quote
  #7  
Old 09-03-2007, 06:20 AM
syurish syurish is offline
Newcomer
 
Join Date: Sep 2007
Posts: 4
Default

Yes, there was an error in the manual.

I did miss your A6 code. Sorry. All is correct with the accuracy. A6 only once is OK.

The link to terminal software is
http://www.smartprogramming.de/dms/downloads/?id=25

- End -
Reply With Quote
  #8  
Old 09-18-2007, 04:06 AM
teamtwf8 teamtwf8 is offline
Centurion
 
Join Date: Feb 2007
Posts: 107
Default

syurish,

I just returned from holiday and started again with the project.

I changed some codes and I know I am near the end.
Stopped using oncomm command

I now only once set code for Accuracy (A6) and Start of the measurements (S) in the Serial connect private sub
Code:
Private Sub CmdComConnect_click()
    
    If MSComm1.PortOpen = True Then
    MSComm1.PortOpen = False 'Close selected comport if in use already
    End If
    
    '2400 bps initial setting for time being
    MSComm1.Settings = "2400,N,8,1" 'set portsettings for initial communication with UFDC-1
    MSComm1.CommPort = 1 'cmbComPorts.Text 'we currently use fixed COM1
    MSComm1.InputLen = 8 'number of characters on which oncomm event fires     No idea if this number is correct
    MSComm1.RThreshold = 1 'number of characters to receive                   No idea if this number is correct
    MSComm1.SThreshold = 1 'number of characters t                             No idea if this number is correct
    MSComm1.InputMode = comInputModeText
    MSComm1.PortOpen = True 'Open selected comport COM1
    MSComm1.Output = "A6" & Chr(13) 'sets accuracy to 0.01%
    MSComm1.Output = "S" & Chr(13) 'starts RPM measurement at UFDC-1
    
End Sub
When I use a command button this is the current code.
Code:
Private Sub cmdSingleClick_Click()

Dim i, sBuffer As String
Dim sTemp1 As String
Dim CtrOld, TimeDifference As Currency

    sBuffer = ""

    MSComm1.Output = "MA" & Chr(13) 'sets UFDC-1 to read RPM on channel-1
    MSComm1.Output = "R" & Chr(13) 'starts RPM collecting as Decimal ASCII by UFDC-1
    
    sBuffer = MSComm1.Input
    Text1.Text = sBuffer 'display receive buffer content
    
    sTemp1 = Replace(sBuffer, Chr(13), "")
    Text2.Text = sTemp1
                     
    QueryPerformanceCounter Ctr1 'Start of timestamp taken from high res Freq API
    'we need this time stamp to calculate elapsed time between RPM values for accelaration
    TimeDifference = Ctr1 - CtrOld 'substract new with old timestamp.
    CtrOld = Ctr1

End Sub
I now do not get echoed back the characters anymore only VbCr but this one can be filtered out easily.
But I need the line of digits with numbers only always in the correct format.
I still struggle with the
MSComm1.InputLen, MSComm1.RThreshold, MSComm1.SThreshold, I think.

I have the terminal.exe running but I need the source code!
Any one more knowledge to get this fixed I like to test engines before the winter time comes and it is to cold.

Thanks, Paco

Last edited by teamtwf8; 09-18-2007 at 04:14 AM.
Reply With Quote
  #9  
Old 09-20-2007, 07:46 PM
Cerian Knight's Avatar
Cerian KnightMScomm1/splitdata problem Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,191
Default

InputLen = 0 will read all waiting characters...this is usually a good thing.
RThreshold = 1 will trigger OnComm event when 1 or more characters are waiting in the buffer...this is usually also a good thing. You must build and parse a master buffer when reading the data, because all of the data may not be in yet for a given response.
RThreshold = 0 will disable OnComm event...which I generally do not recommend except for the simplest communication at the lowest baud rates.
SThreshold = 0 is fine for sending simple commands where you don't need to continuously send lots of data out.

BTW, in this example: 'Dim I , J as Long', only J is Long. I is a Variant. Do this instead: 'Dim I as Long, J as Long'.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #10  
Old 09-21-2007, 12:05 AM
teamtwf8 teamtwf8 is offline
Centurion
 
Join Date: Feb 2007
Posts: 107
Default

Cerian thanks for the explanation.
This helps

I am now a little further and can read one channel with out problems.
Will post code later as it is on an other PC.

Paco
Reply With Quote
  #11  
Old 09-21-2007, 02:29 AM
DougT's Avatar
DougT DougT is offline
Ultimate Antique

Administrator
* Expert *
 
Join Date: Sep 2005
Location: Maldon,Essex, UK
Posts: 3,939
Default

I'll just stick my oar in if nobody minds....

There seem to be a few unanswered questions which may hold the 'key'

1. The Application Protocol: Looking at your first post it seems that the device is signalling "End of Message" with a vbCr > pair. This is important to note as that's what you need to look for in your Buffer management routine.
If the AP is as described, then I would expect your original sequence of commands to result in:
"vbCr>M0vbCr>SvbCr>R2.833455171905vbCr>"

(It also looks like that the device issues a "vbCr>" when it's first powered on to indicate it's ready)

2. Echo of Transmitted Data: I don't understand how, just by polling the device rather than usig the OnComm event, the echoing of characters has stopped. Are you sure you didn't change any other settings (the external switches, perhaps?)

It looks to me as if there are two types of Commands; "Configuration Commands" and "Data Fetching Commands" The Configuration Commands do not return any data, they just echo the command and can be ignored, whilst the Data Fetching Commands actually produce the information you want to process.

As Cerian has pointed out, you have to collect the information received from the device and make sure it's all there before trying to process it. Then when you process the Buffer (by looking for vbCr> pairs) you can discard any Configuration Command echos and pull out the numerical responses from the Data Fetching Commands.
__________________
semel insanivimus omnes
S Data in context = Information, S Information in context = Knowledge, S Knowledge in context = Experience
S Experience in context = Wisdom= Data
Reply With Quote
  #12  
Old 09-25-2007, 11:33 AM
teamtwf8 teamtwf8 is offline
Centurion
 
Join Date: Feb 2007
Posts: 107
Cool

Ok thanks for all the help and suggestions sofar.

Correct about the commands not echoed back suddenly any more.
The UFDC-1 was by accident started up in autonomous mode when that happend.

I now have the commands back but they were not the issue.
It was correct to the CR to be a deciding point of interest! :-)

The UFDC-1 can read two channels in sequence but for testing purpose I now will only use a single channel
untill all works at highest speed possible.

This is the current code with help from a friend that works with baudrates upto 4800 bps.
We now again are orking with the oncomm.

As soon as I try higher bps settings there is no communication any more.
With the terminal.exe it works fine so it must be something inside the current code I use.
Will have to look for the cause later.

Code:
Option Explicit
Dim sReceiveString As String
Dim sBuffer1, sBuffer2 As String
Dim sTemp1, sTemp2 As String
Dim sTemp3, sTemp4 As Variant
Dim Ctr1 As Currency
Dim CtrOld, TimeDifference As Currency
Dim Channel1Request As Boolean
Dim EndTime As Date
Dim filename1 As String

Private Sub cmdRecStart_Click()
cmdRecStart.Visible = False

cmdRecStop.Enabled = True
cmdRecStop.Visible = True

Kill App.Path & "\data\dyno\dyno.xls"
Open App.Path & "\data\dyno\dyno.xls" For Output As #1
  Print #1, "Timestamp1" & vbTab; "RPM Engine" 'print header in file
Close #1

End Sub

Private Sub cmdRecStop_Click()

cmdRecStart.Enabled = True
cmdRecStart.Visible = True
cmdRecStart = False

cmdRecStop.Visible = False
cmdRecStop.Enabled = False

'### Rename the recorded dyno.xls file to date/time and save it to filename1
FileCopy App.Path & "\data\dyno\dyno.xls", App.Path & "\data\dyno\dyno1.xls"

EndTime = DateAdd("s", 1, Now)
Do Until Now > EndTime
  DoEvents
Loop

Name App.Path & "\data\dyno\dyno1.xls" As App.Path & "\data\dyno\" & Format(Date, "yyyy-mm-dd") & "_" & Format(Time, "hh-mm-ss") & ".xls"
filename1 = App.Path & "\data\dyno\" & Format(Date, "yyyy-mm-dd") & "_" & Format(Time, "hh-mm-ss") & ".xls"
'### End
End Sub

Private Sub form_load()
cmdRecStart.Visible = True
cmdRecStart.Enabled = True
cmdRecStop.Visible = False
cmdRecStop.Enabled = False

    If MSComm1.PortOpen = True Then
    MSComm1.PortOpen = False 'Close selected comport if in use already
    End If
    
    '2400 bps initial communication setting
    MSComm1.Settings = "2400,N,8,1" 'set portsettings for initial communication with UFDC-1
    MSComm1.CommPort = 1 'cmbComPorts.Text 'we currently use fixed COM1
    MSComm1.InputLen = 1 ' Read only one character at the time
    MSComm1.RThreshold = 1 'number of characters to receive
    MSComm1.SThreshold = 1
    MSComm1.InputMode = comInputModeText
    MSComm1.PortOpen = True 'Open selected comport COM1
    MSComm1.Output = "B4" & Chr(13) 'set baudrate at 9600 bps
    MSComm1.PortOpen = False 'Close selected comport COM1
    
    MSComm1.Settings = "4800,N,8,1"
    MSComm1.CommPort = 1 'cmbComPorts.Text 'we currently use fixed COM1
    MSComm1.InputLen = 1 ' Read only one character at the time
    MSComm1.RThreshold = 1 'number of characters to receive
    MSComm1.SThreshold = 1
    MSComm1.InputMode = comInputModeText
    MSComm1.PortOpen = True 'Open selected comport COM1
    MSComm1.Output = "A7" & Chr(13) 'sets accuracy to 0.01%
    MSComm1.Output = "Z08" & Chr(13) 'sets the rotor for 16 B/W spots
    MSComm1.Output = "MA" & Chr(13) 'sets UFDC-1 to read RPM on channel-1
    MSComm1.Output = "S" & Chr(13) 'start UFDC-1
    MSComm1.Output = "R" & Chr(13) 'starts RPM collecting value as Decimal ASCII by UFDC-1
    Channel1Request = True
    sReceiveString = ""
    
End Sub

Private Function DecodeString(Data As String, ByRef Result As Double) As Boolean
    Dim spstr As Variant
    Dim valstr As String
    
      QueryPerformanceCounter Ctr1 'Start of timestamp taken from high res Freq API
      'we need this time stamp to calculate elapsed time between RPM values for accelaration
      TimeDifference = Ctr1 - CtrOld 'substract new with old timestamp.
      lblTimedif = Ctr1
      CtrOld = Ctr1
                   
    On Error GoTo DecodeError
    ' check if the string starts with a digit, a space or a -
    
    ' replace . with , to have a proper formed double value string
    If InStr(1, Data, ".", vbTextCompare) Then
        spstr = Split(Data, ".")
        valstr = spstr(0) '+ "," + spstr(1) 'we only like all values left from the ","
    Else
        valstr = Data
    End If
    Result = CDbl(valstr)
    DecodeString = True
    Exit Function
DecodeError:
    DecodeString = False
End Function
Private Sub MSComm1_OnComm()
    Dim recv As String
    Dim i As Integer
    
    Do While True
        recv = MSComm1.Input 'put the data in the string
        If recv = "" Then 'if the datastring is empty then
            Exit Do 'exit the loop
        End If
         If Left(recv, 1) = CStr(Chr(13)) Then 'if the input data contains a RETURN then
            ' Found command terminator
            Dim Val As Double
            If DecodeString(sReceiveString, Val) Then
                  ' This value is from channel 1 (engine) so we issue a channel 2 request now
                    lblRpmEngine.Caption = CStr(Val)
                    ARProgressBarEngine.Value = CStr(Val)
                                            
                      'this "if" part does not work 'Autostart/stop of recording to file
                      If cmdRecStart = True And CStr(Val) >= 1000 Then
                        Open App.Path & "\data\dyno\dyno.xls" For Append As #1
                        Print #1, Ctr1 & vbTab; CStr(Val)
                        Close #1
                      End If
                      DoEvents
                      
                      If cmdRecStop.Enabled = True And CStr(Val) >= 9000 Then
                      cmdRecStop = True
                      End If
                      
                      'end part
                    
                    MSComm1.Output = "MA" & Chr(13) 'sets UFDC-1 to read Freq on channel-1
                    MSComm1.Output = "S" & Chr(13) 'start UFDC-1
                    MSComm1.Output = "R" & Chr(13) 'starts RPM collecting value as Decimal ASCII by UFDC-1
            End If
            
            ' reset the receive string
            sReceiveString = ""
         Else
            sReceiveString = sReceiveString + recv
         End If
    Loop
End Sub

Private Sub form_unload(Cancel As Integer)
If MSComm1.PortOpen = True Then
    MSComm1.PortOpen = False 'Close selected comport if in use already
End If
    
MSComm1.PortOpen = True 'Open selected comport COM1
MSComm1.Output = "B3" & Chr(13)
MSComm1.PortOpen = False

End Sub
I am getting there.

Paco
Reply With Quote
  #13  
Old 09-25-2007, 12:55 PM
Cerian Knight's Avatar
Cerian KnightMScomm1/splitdata problem Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,191
Default

At higher baud rates you are guaranteed to be conditionally throwing away parts of 'recv' with the code:
Code:
If Left(recv, 1) = CStr(Chr(13)) Then
The vbCR may (at any baud rate) be in the middle of 'recv'. The code should probably read something like this:
Code:
If InStr(1, recv, vbCr) > 0 Then
but you will have to change your parsing to handle this properly.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #14  
Old 09-26-2007, 08:46 AM
syurish syurish is offline
Newcomer
 
Join Date: Sep 2007
Posts: 4
Exclamation

teamtwf8 ,

Be careful (just in case) with the higher bps. If you try to establish a communication speed higher than the necessary conversion time for an appropriate relative error you will not receive any result.

Please refer to the following article where described how to calculate the measurement time for the UFDC-1M-16 and UFDC-1 and relation between communication speed and conversion time:

http://www.sensorsportal.com/HTML/DIGEST/P_150.htm



Good luck.

- End -
Reply With Quote
  #15  
Old 10-01-2007, 10:06 AM
teamtwf8 teamtwf8 is offline
Centurion
 
Join Date: Feb 2007
Posts: 107
Default

Cerian,

Thanks for the code.

Serge,

I cant fully understand all written in the article.
I can now use 4800 bps as maximum as next step to 9660 doe snot work at all.
Do not know where the conversion time need to be set.
I am a new bie to this all and when I read these articles the world is spinning fastly in my head.

With current code I can record RPM.
But the values are always stepped if I accellerate a flywheel where I am certain this never cant be correct with my knowledge.

Code:
Option Explicit
Dim sReceiveString As String
Dim sBuffer1, sBuffer2 As String
Dim sTemp1, sTemp2 As String
Dim sTemp3, sTemp4 As Variant
Dim Ctr1 As Currency
Dim CtrOld, TimeDifference As Currency
Dim Channel1Request As Boolean
Dim EndTime As Date
Dim filename1 As String
Dim Validation As Boolean
Dim DataStart As Long
Dim DataStop As Long

Private Sub form_load()
cmdStartRec.Visible = True
cmdStartRec.Enabled = True
cmdStopRec.Visible = False
cmdStopRec.Enabled = False
cmdStartRec.BackColor = &HC000&


'###sets combobox value for DataStart
For DataStart = 0 To 15000 Step 1000
cmbDataStart.AddItem DataStart
Next DataStart

'###sets combobox value for DataStop
For DataStop = 6000 To 40000 Step 1000
cmbDataStop.AddItem DataStop
Next DataStop

Validation = False
Timer1 = True

    If MSComm1.PortOpen = True Then
    MSComm1.PortOpen = False 'Close selected comport if in use already
    End If
    
    '2400 bps initial communication setting
    MSComm1.Settings = "2400,N,8,1" 'set portsettings for initial communication with UFDC-1
    MSComm1.CommPort = 1 'cmbComPorts.Text 'we currently use fixed COM1
    MSComm1.InputLen = 1 ' Read only one character at the time
    MSComm1.RThreshold = 1 'number of characters to receive
    MSComm1.SThreshold = 1
    MSComm1.InputMode = comInputModeText
    MSComm1.PortOpen = True 'Open selected comport COM1
    MSComm1.Output = "B4" & Chr(13) 'set baudrate at 9600 bps
    MSComm1.PortOpen = False 'Close selected comport COM1
    
    MSComm1.Settings = "4800,N,8,1"
    MSComm1.CommPort = 1 'cmbComPorts.Text 'we currently use fixed COM1
    MSComm1.InputLen = 1 ' Read only one character at the time
    MSComm1.RThreshold = 1 'number of characters to receive
    MSComm1.SThreshold = 1
    MSComm1.InputMode = comInputModeText
    MSComm1.PortOpen = True 'Open selected comport COM1
    MSComm1.Output = "A7" & Chr(13) 'sets accuracy to 0.01%
    MSComm1.Output = "Z08" & Chr(13) 'sets the rotor for 16 B/W spots
    MSComm1.Output = "MA" & Chr(13) 'sets UFDC-1 to read RPM on channel-1
    MSComm1.Output = "S" & Chr(13) 'start UFDC-1
    MSComm1.Output = "R" & Chr(13) 'starts RPM collecting value as Decimal ASCII by UFDC-1
    Channel1Request = True
    sReceiveString = ""
    
End Sub

Private Function DecodeString(Data As String, ByRef Result As Double) As Boolean
    Dim spstr As Variant
    Dim valstr As String
    
      QueryPerformanceCounter Ctr1 'Start of timestamp taken from high res Freq API
      'we need this time stamp to calculate elapsed time between RPM values for accelaration
      TimeDifference = Ctr1 - CtrOld 'substract new with old timestamp.
      lblTimedif = Ctr1
      CtrOld = Ctr1
                       
    On Error GoTo DecodeError
    ' check if the string starts with a digit, a space or a -
    
    ' replace . with , to have a proper formed double value string
    If InStr(1, Data, ".", vbTextCompare) Then
        spstr = Split(Data, ".")
        valstr = spstr(0) '+ "," + spstr(1) 'we only like all values left from the ","
    Else
        valstr = Data
    End If
    Result = CDbl(valstr)
    DecodeString = True
    Exit Function
DecodeError:
    DecodeString = False
    
    End Function

Private Sub MSComm1_OnComm()
    Dim recv As String
    Dim i As Integer
    
    Do While True
        recv = MSComm1.Input 'put the data in the string
        If recv = "" Then 'if the datastring is empty then
            Exit Do 'exit the loop
        End If
         If InStr(1, recv, vbCr) > 0 Then
         'If Left(recv, 1) = CStr(Chr(13)) Then 'if the input data contains a RETURN then
            ' Found command terminator
            Dim val As Double
            If DecodeString(sReceiveString, val) Then
                  ' This value is from channel 1 (engine) so we issue a channel 2 request now
                    lblRpmEngine.Caption = CStr(val)
                    ARProgressBarEngine.Value = CStr(val)
                                                                
                    MSComm1.Output = "MA" & Chr(13) 'sets UFDC-1 to read Freq on channel-1
                    MSComm1.Output = "S" & Chr(13) 'start UFDC-1
                    MSComm1.Output = "R" & Chr(13) 'starts RPM collecting value as Decimal ASCII by UFDC-1
            End If
            
            ' reset the receive string
            sReceiveString = ""
         Else
            sReceiveString = sReceiveString + recv
         End If
    Loop
End Sub

Private Sub Timer1_Timer()
'section to make sure the recorded data is put in excel file for later analyzing
'timer set to 1 ms
If Validation = True And lblRpmEngine.Caption >= cmbDataStart Then
Open App.Path & "\data\dyno\dyno.xls" For Append As #1
  Print #1, Ctr1 & vbTab; lblRpmEngine.Caption
  Close #1
End If

If Validation = True And lblRpmEngine >= cmbDataStop Then
Close #1
cmdStopRec = True
Validation = False
End If

End Sub

Private Sub cmdStartRec_Click()
'activate recodring
cmdStartRec.Visible = False
cmdStopRec.Enabled = True
cmdStopRec.Visible = True

On Error Resume Next
Kill App.Path & "\data\dyno\dyno.xls"
Open App.Path & "\data\dyno\dyno.xls" For Output As #1
  Print #1, "Timestamp1" & vbTab; "RPM Engine" 'print header in file
Close #1
Validation = True

End Sub

Private Sub cmdStopRec_Click()
'stop recording
cmdStartRec.Enabled = True
cmdStartRec.Visible = True
cmdStartRec = False

cmdStopRec.Visible = False
cmdStopRec.Enabled = False

'### Rename the recorded dyno.xls file to date/time and save it to filename1
FileCopy App.Path & "\data\dyno\dyno.xls", App.Path & "\data\dyno\dyno1.xls"

EndTime = DateAdd("s", 1, Now)
Do Until Now > EndTime
  DoEvents
Loop

Name App.Path & "\data\dyno\dyno1.xls" As App.Path & "\data\dyno\" & Format(Date, "yyyy-mm-dd") & "_" & Format(Time, "hh-mm-ss") & ".xls"
filename1 = App.Path & "\data\dyno\" & Format(Date, "yyyy-mm-dd") & "_" & Format(Time, "hh-mm-ss") & ".xls"
'### End

Validation = False

End Sub


Private Sub form_unload(Cancel As Integer)
If MSComm1.PortOpen = True Then
  MSComm1.PortOpen = False 'Close selected comport if in use already
End If
    
MSComm1.PortOpen = True 'Open selected comport COM1
MSComm1.Output = "B3" & Chr(13)
MSComm1.PortOpen = False

End Sub
I attached the .xls file of a accelaration for visualitation.
Attached Files
File Type: zip test.zip (3.4 KB, 7 views)
Reply With Quote
  #16  
Old 10-01-2007, 11:38 AM
Cerian Knight's Avatar
Cerian KnightMScomm1/splitdata problem Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,191
Default

I think you still need to append 'recv' to 'sReceiveString' before sending the latter to 'DecodeString'. Also, regarding high baud rates, I'm not sure if it is possible for more than one response (each containing a vbCr) to come through in one event. Maybe not, if this is a single command single response controller. If it is multiple repetitive responses (seperated by vbCr), then the unused part of 'sReceiveString' needs to be put back into 'sReceiveString' when you are done decoding.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #17  
Old 10-01-2007, 01:01 PM
teamtwf8 teamtwf8 is offline
Centurion
 
Join Date: Feb 2007
Posts: 107
Default

Cerian,

Thanks,

The oncomm was not written by me and I half know what goes on in there. lets say 60%
I have to study your response and see if I can make something of it.

The UFDC-1 can handle only one set of commands at one time if that was what you said in the second part of your answer.

Paco
Reply With Quote
  #18  
Old 10-01-2007, 02:09 PM
Cerian Knight's Avatar
Cerian KnightMScomm1/splitdata problem Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,191
Default

For the first part I was suggesting you do this instead:
Code:
If InStr(1, recv, vbCr) > 0 Then 'insert the following line to make sure that any characters before the vbCr in recv are parsed sReceiveString = sReceiveString + recv
I hope that is crystal clear.

For the second part I was hoping vbCr will never be immediately followed by another received character, at least until after the next command is sent.

In other words if the communication is like this:
[Send Command][Receive response][Receive response]...etc.
then we might have a parsing problem. But if it is like this:
[Send Command][Receive response][Send Command][Receive response]
then we are OK.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #19  
Old 10-02-2007, 01:04 AM
teamtwf8 teamtwf8 is offline
Centurion
 
Join Date: Feb 2007
Posts: 107
Default

Cerian,

I did not understand you thinking as there was already this code
Code:
sReceiveString = sReceiveString + recv
below in the oncomm.

So if I understnad right this must become the full oncomm?
Code:
Private Sub MSComm1_OnComm()
    Dim recv As String
    
    Do While True
        recv = MSComm1.Input 'put the data in the string
        If recv = "" Then 'if the datastring is empty then
            Exit Do 'exit the loop
        End If
         
         If InStr(1, recv, vbCr) > 0 Then 'search string inside string if larger as 0
         sReceiveString = sReceiveString + recv 'added code line on advise of Cerian
         'new value is previous value added with value of MSComm1.input
         
         ' Found command terminator
            Dim val As Double
            If DecodeString(sReceiveString, val) Then
                  ' This value is from channel 1 (engine) so we issue a channel 2 request now
                    lblRpmEngine.Caption = CStr(val)
                    ARProgressBarEngine.Value = CStr(val)
                                                                
                    MSComm1.Output = "MA" & Chr(13) 'sets UFDC-1 to read Freq on channel-1
                    MSComm1.Output = "S" & Chr(13) 'start UFDC-1
                    MSComm1.Output = "R" & Chr(13) 'starts RPM collecting value as Decimal ASCII by UFDC-1
            End If
            
            ' reset the receive string
            sReceiveString = ""
         Else
            sReceiveString = sReceiveString + recv 'Do not know if this code line may stay.
            'new value is previous value added with value of MSComm1.input
         End If
    Loop
End Sub
I added the comment for my own piece of mind.
If they are not correct would you be so kind to ammend them.

For the second part.

If I understand correct.
When oncomm receives anything and puts it in the buffer it sends the code to the UFDC-1
MSComm1.Output = "MA" & Chr(13) 'sets UFDC-1 to read Freq on channel-1
MSComm1.Output = "S" & Chr(13) 'start UFDC-1
MSComm1.Output = "R" & Chr(13) 'starts RPM collecting value as Decimal ASCII by UFDC-1.

I put a screen shot of one of the first versions I wrote to get any reading out of the UFDC-1.
The textbox below ithe F3 - reset button is unworked result from the MSComm1.Input.
The black vertical lines are VbCr's
As you can see it echoes back the commands and also the result.
The setting of the MSComm1.RThreshold = 20 makes the box show 20 characters.

I hope this makes things more clear.

I whish there was a forum of the UFDC-1 but it seems I am pionering with my low level technical knowledge in a situation of high level of enginering knowledge. :-(

Cerian, for your information I have tried USB datalogger to get high accurate RPM values but this failed.

Then did parallel port RPM reading but failed cause if unknown jitter.

Then I discovered the UFDC-1 and asked if I could use it for my project.
http://www.twf8.ws/new/tech/dtss/dow...tss_manual.htm
I was told I could.
So now I am busy to get this thing rolling.
I thought it wa smuch easier to get a value for the reat of the codes I already have. But I think I overlooked this heavily.

What I need in the end is a cheap and accurate way of collecting RPM to produce inertia values of a flywheel by calculation the speed up in RPM.
I need a highest sample rate as posisble to get nice graphs.

Paco
Attached Images
File Type: jpg screenshot_01 Oct. 02 08.25.jpg (284.6 KB, 13 views)
Reply With Quote
  #20  
Old 10-02-2007, 03:04 PM
Cerian Knight's Avatar
Cerian KnightMScomm1/splitdata problem Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,191
Default

The code looks correct with the change I suggested. Just before calling DecodeString you were abandoning the contents of 'recv', which may have contained more than just vbCr. Remember that we set up OnComm to be called as soon as a single character enters the buffer, but Windows is NOT a realtime OS! I've seen cases with serial data streaming I've done where HUNDREDS of characters are returned by MSComm.Input by the time Windows gets around to executing the OnComm code after an (low priority IRQ) event triggers .

I've read the doc on the UFDC-1 and I'm not convinced you need to resend "MA" and "S" in the OnComm event. I would expect "R" to be sufficient.

Also, I'm not convinced that you need "A7", I would expect A2 or A3 to be fine. 0.25% relative error is pretty good for most measurements. I expect that the lower the value the slower the response.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').

Last edited by Cerian Knight; 10-02-2007 at 03:10 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
MScomm1/splitdata problem
MScomm1/splitdata problem
MScomm1/splitdata problem MScomm1/splitdata problem
MScomm1/splitdata problem
MScomm1/splitdata problem
MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem MScomm1/splitdata problem
MScomm1/splitdata problem
MScomm1/splitdata problem
 
MScomm1/splitdata problem
MScomm1/splitdata problem
 
-->