Page 1 of 1

Generating CRC from VBScript

Posted: Tue 8. Nov 2016, 15:46
by PCANUser
Hello,
This is a follow on from a question I asked some time ago relating to the creation of a sequence and CRC in a dynamically changing message. The original post was here: http://www.peak-system.com/forum/viewto ... =50&t=1237

The code you suggested is shown below:

Code: Select all

Sub SendMessage()
  Dim sequence, sig1, sig2, crc

  Set sequence = Signals("SequenceNumber")
  Set sig1 = Signals("Sig1")
  Set sig2 = Signals("Sig2")
  Set crc = Signals("CRC")
  ' Disable AutoTransmit option of variables
  sequence.Source.AutoTransmit = False
  sig1.Source.AutoTransmit = False
  sig2.Source.AutoTransmit = False

  sequence.Value = 0
  While True
    sequence.Value = sequence.Value + 1
    crc.Value = 123  ' calculate and assign CRC value here
    Wait 100
  Wend
  
End Sub
The technique works well for a sequence number where each value is just one more than the previous one. However, for calculating a CRC (which is an SAE J1850 CRC), I need access to the contents of the message as bytes and that is where I am struggling.

If I have a signal (for example the CRC), how can I navigate to the message it is in and access the other bytes of the message? I don't really want to have to mask off bits and bytes of signals because this is very unmaintainable although if there is no alternative I suppose it is possible.

I think I may be able to get at the contents via Connections.TransmitMessages().DataVariant but I can't work out out how to get this back as an array.

Any help would be gratefully appreciated.

Thanks,
Richard

P.S. Is there any better way of debugging VBScript in PCAN Explorer other than by using PrintToOutputWindow()?

Re: Generating CRC from VBScript

Posted: Tue 8. Nov 2016, 16:26
by PEAK-Support
It is not possible to go "backwards" from the Signal to the raw CAN Databytes.
You could define 8 Signals in the Symbol file for this ID - all based as 8 Bit unsigned - each for one Byte.
After that work only with this signals - but for me this make no sence, because then you could work directly with raw CAN Data.
..Is there any better way of debugging VBScript in PCAN Explorer other than by using PrintToOutputWindow()?
No, it´s VBS - you could not use any kind of Debugger when use VBS Code. Maybe it´s more efficient to code directly in C/C++/C#/VB.Net etc.

Re: Generating CRC from VBScript

Posted: Tue 8. Nov 2016, 16:46
by PCANUser
Thank you for your prompt response.

I guess there's no way of creating an alias to access the data as bytes?

If the answer is no, that leaves two options as I see it:
1. Calculate the bytes from the signals (ugly but relatively straightforward in VBS).
2. Change to a plug-in and work in a proper ( :D ) language such as C#.

Thanks,
Richard

Re: Generating CRC from VBScript

Posted: Tue 8. Nov 2016, 17:01
by PEAK-Support
We could read all settings of a signal like used CAN-ID, Bit Start, Bit Lenght , DataType etc.
But with this information you have to calculate the signal back to the raw Bytes - hard work!

But our developers told me that we have a "hidden" Property for a Signal Object called "raw data". Maybe this could be used. You have to define a new Signal with exact the lenght of the Bits you need to calculate the CRC. You convert from the Signal back to the RAW Data (result is a string with HEX Value). Then you calculate the CRC and use it as the CRC Signal. If this is the solution you could use, please write us a e-mail, so that we could send you a sample code. This Propertie is not documented in the original Online Help.

A Plug In is also possible and you have a lot of more functions available, but the first steps in such a project need some time.

Re: Generating CRC from VBScript

Posted: Tue 8. Nov 2016, 17:32
by PCANUser
Thanks. The source code would be very useful. I will send an email to your support address.

I have previously developed a C# plug-in for PCAN Explorer so I know what I'm doing but I agree there is a bit more work to set it up.

Could you remind me if there is an event I could intercept if I chose this solution to insert the CRC and sequence number.

Thanks,
Richard

Re: Generating CRC from VBScript

Posted: Wed 9. Nov 2016, 10:34
by PEAK-Support
Here how to use the Property RawData of a Signal:

Code: Select all

Sub RawValueSample()
  Dim sigData, sigCRC
  Set sigData = Signals("Data")
  Set sigCRC = Signals("CRC")
  
  Dim rawdata, i, b, crc
  rawdata = sigData.RawValue
'  MsgBox rawdata,,"Raw"
  
  For i = 1 To Len(rawdata) Step 2
	' Access the individual data bytes (0-6)
	b = CByte("&H" & Mid(rawdata, i, 2))
    
    ' Calculate CRC
    'crc = ...
	
  Next
  ' Set the CRC field in the message and send it once
  sigCRC.Value = crc
End Sub
here the used Symbol File for this small script:

Code: Select all

FormatVersion=5.0 // Do not edit this line!

{SENDRECEIVE}

[Symbol]
ID=123h
DLC=8
Var=Var1 unsigned 0,8  /d:11h
Var=Var2 unsigned 8,16 /d:3322h
Var=Var3 unsigned 24,16 /d:5544h
Var=Var4 unsigned 40,16 /d:7766h
Var=Data raw 0,56
Var=CRC unsigned 56,8
The "Trick" ist to define a extra Signal that use the complete Bits from all other Signals (except the CRC)
In the sample is called DATA, and start from Bit 0 with a length of 56 Bit (7 Bytes - the Var1 to Var4).
In the script we read the RawData from this Signal. (See optional Output via MessageBox)
Then we loop to all Bytes and calculate the CRC. The Result is stored inside the CRC Signal again.

Re: Generating CRC from VBScript

Posted: Wed 9. Nov 2016, 11:10
by PCANUser
Thanks for the response. This looks like a good solution.

We use the PCAN symbol editor to import a DBC file and create the symbol file. Is there any way to specify the raw data field in the DBC so that it is automatically included in the symbol file?

If I decide to go down the C# and add-in route, what would you suggest as the best way to implement this functionality? For my previous add-in, I created a callback on a signal change but this was used for presenting data in received messages.

Thanks once again for your help.
Richard

Re: Generating CRC from VBScript

Posted: Thu 10. Nov 2016, 10:11
by PCANUser
Just to let you know that this is now working well.

Thanks for the help.
Richard

Re: Generating CRC from VBScript

Posted: Thu 10. Nov 2016, 18:42
by PCANUser
In case anybody has been following this discussion, there is one minor tweak required to the example.

We encountered the problem that if the slider controlling the message content was moved, occasionally the checksum was wrong.

The fix for this was to change the code as follows:

Code: Select all

Sub RawValueSample()
  Dim sigData, sigCRC
  Set sigData = Signals("Data")
  Set sigCRC = Signals("CRC")
  
  Dim rawdata, i, b, crc
  rawdata = sigData.RawValue
'  MsgBox rawdata,,"Raw"
  
  sigCRC.Source.AutoTransmit = false
  sigData.Source.AutoTransmit = true ' Trigger transmission on write to Data
  

  For i = 1 To Len(rawdata) Step 2
   ' Access the individual data bytes (0-6)
   b = CByte("&H" & Mid(rawdata, i, 2))
    
    ' Calculate CRC
    'crc = ...
   
  Next
  ' Set the CRC field in the message
  sigCRC.Value = crc

  ' Send the message
  sigData.RawValue = rawdata
End Sub

Re: Generating CRC from VBScript

Posted: Fri 11. Nov 2016, 17:11
by PEAK-Support
Yes, thats is a good objection...
Whenever you change a signal, the corespondending CAN ID, where the signal is part of, will be send by default.
By setting the autotransmit to off, you prevent this.