1

I'm trying to define some test data using string literals in CAPL. Most of the data is ASCII, but occasionally I need raw bytes, which I encode using hex escape sequences. This worked until I tried to encode values above 127. Here's a test example:

variables 
{
    message 0x123 testmsg;
}
testcase TestHex(char hexdata[])
{
    testStep("Info", "First byte: 0x%X", hexdata[0]);
    testmsg.byte(0) = hexdata[0];
    output(testmsg);
}
void MainTest ()
{ 
    TestHex("\x00");
    TestHex("\x10");
    TestHex("\x20");
    TestHex("\x50");
    TestHex("\x7F");
    TestHex("\x80");
    TestHex("\xFF");
}

The output I get in the test report is:

First byte: 0x0
First byte: 0x10
First byte: 0x20
First byte: 0x50
First byte: 0x7F
First byte: 0x3F
First byte: 0x3F

I can also see the same bytes transmitted on the bus inside the testmsg message.

How could I encode such values in CAPL strings?

Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53
  • You are printing `0xx80`, am I correct? But that's beyond the point: why not just `0%s`? You are passing around strings, no need to convert to hex when printing. – Daemon Painter Dec 21 '20 at 13:33
  • @DaemonPainter I'm not printing anything, I'm trying to define data arrays which are mostly ASCII, but occasionally contain binary data like BCD numbers or bytes with bit flags. – Dmitry Grigoryev Dec 21 '20 at 13:46
  • CAPL is strongly typed. You are storing strings, in this example -> and you have issues printing (`testStep("Info", "First byte: 0x%X", hexdata[0]);`). Please, rework your MVCE to explain better the point I am missing. – Daemon Painter Dec 21 '20 at 13:52
  • @DaemonPainter I have no issues printing bytes in 0x00-0x7F range, so I'm pretty sure the issue is not related to printing. I also observe the same 0x3F bytes inside CAN messages as well. – Dmitry Grigoryev Dec 21 '20 at 14:33

1 Answers1

2

If you want to use values greater than 127, you should use byte instead of char as datatype for your parameter to TestHex. I.e.:

testcase TestHex(byte hexdata[])
{
    testStep("Info", "First byte: 0x%X", hexdata[0]);
    testmsg.byte(0) = hexdata[0];
    output(testmsg);
}
MSpiller
  • 3,500
  • 2
  • 12
  • 24
  • Thanks, I'll test that and report back. Any idea why `char` is essentially limited to 7 bits? – Dmitry Grigoryev Dec 22 '20 at 11:07
  • Replacing `char` with `byte` doesn't work by the way: "Error 1154 at (15,5): types of parameters do not match". – Dmitry Grigoryev Jan 04 '21 at 13:30
  • @DmitryGrigoryev - You need to declare another byte array variable in the MainTest function and add the values inside it and then send this byte array as a paramter. – Shyam Jan 27 '21 at 08:24
  • @Shyam I don't quite understand how I can "add values inside it". Do you suggest something like `byte hexdata = {'m','y',' ','l','o','n','g',' ','s','t','r','i','n','g',' ','w','i','t','h',' ',0xFF}`? That's what I was trying to avoid by using string literals in the first place... – Dmitry Grigoryev Jan 27 '21 at 09:45
  • You can't mix value of two different types in a single variable. What you are trying to do is mix a byte value and a character into the same variable. You need to go either with byte type or avoid sending byte type in char variable. – Shyam Jan 27 '21 at 10:04
  • @Shyam I'm not mixing anything, there is no `byte` type in the question. I'm fine with `char []`, what I don't understand is why `"\xFF"` is not a valid `char []` (or, more precisely, why is it replaced with `"\x3F"`) – Dmitry Grigoryev Jan 27 '21 at 10:06
  • The "char" datatype can only store ASCII values. If you try to store any other value, then it gets converted to a question mark so that it can be easily identified when printed. The hex value of a question mark is 0x3F. That is the reason why you see 3F getting printed for unsupported ASCII value. – Shyam Jan 27 '21 at 10:16
  • One more information is, the char data type is actually a signed 8 bit information. So, it can only store value with 7 bits (which is a maximum of 127). – Shyam Jan 28 '21 at 07:34
  • @Shyam, Is there a CAPL language spec where this is described? – Dmitry Grigoryev Jan 28 '21 at 09:11
  • It can be checked in the CANoe help section under the title "Value Ranges of the Data Types". A char data type has the range -128...+127 and a byte data type has the range 0...255. Also note that a byte is nothing but an unsigned char. – Shyam Feb 01 '21 at 07:34