Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Here my code...
C++
void rfid_msg(byte opcode, byte* data, int len)
{
    byte* msg = new byte[len + 5];
    byte response[256];

    msg[0] = 0xff;
    msg[1] = len;
    msg[2] = opcode;

    if (len > 0) {
        memcpy(&msg[3], data, len);
    }

    unsigned int tmp = crc(&msg[1], len + 2);

    msg[len + 3] = tmp >> 8;
    msg[len + 4] = tmp & 0xff;

    Serial.write(msg, len + 5);
}

rfid_msg(0x0c, new byte[0], 0);

For this specific 'message' the data to send should be a five byte array with the values 0xff, 0x00, 0x0c, 0x1d, 0x03 (the crc computes that 0x1d, 0x03 thing)...
However the first (and only the first) byte comes as 0x16 - always...

If I change the this line:
C++
byte* msg = new byte[len + 5];

to this:
C++
byte msg[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 

(16 bytes is enough for sure)
than everything works perfectly and the first byte comes as 0xff...

Any explanation?
Posted
Comments
Afzaal Ahmad Zeeshan 16-Nov-15 16:22pm    
5ed; for a very interesting question there.

I would also be interested in looking forward to answer to this problem.
[no name] 16-Nov-15 18:26pm    
What am I missing?
0xff + opcode + len + crc == 6 bytes
byte* msg = new byte[len + 5]; // does not allocate enough

Hard coded constants are dangerous.
https://www.arduino.cc/en/Reference/Int[^]
0xff + opcode + len + crc == 6 bytes

C++
void rfid_msg(byte opcode, byte* data, int len)
{
    byte* msg = new byte[len + 6];
    byte response[256];
 
    msg[0] = 0xff;
    msg[1] = len >> 8;
    msg[2] = len & 0xff;
    msg[3] = opcode;
 
    if (len > 0) {
        memcpy(&msg[4], data, len);
    }
 
    unsigned int tmp = crc(&msg[1], len + 3);
 
    msg[len + 4] = tmp >> 8;
    msg[len + 5] = tmp & 0xff;
 
    Serial.write(msg, len + 6);

    delete [] msg;
}
 
Share this answer
 
v5
Comments
George Jonsson 16-Nov-15 21:05pm    
Will this line really work?
msg[1] = len;
Shouldn't the integer be split up in two bytes.
I haven't programmed C++ in a long time, but I think you will only get the truncated value and the byte msg[2] will be empty.
[no name] 16-Nov-15 21:07pm    
Correct same as crc. Code reviews do work.
George Jonsson 16-Nov-15 21:17pm    
They do. :-)
Kornfeld Eliyahu Peter 17-Nov-15 2:09am    
You missed some points here...
There is a thing called implicit type casting in C/C++, so when you do a 'byte = int' thin you will not get error or corrupted memory, but the compiler will wrap it in a type cast code...
You also missed the point where I told, that if I declare a fixed size array, and not allocating it run-time (using new) it all works perfect...

Even if you would imagine that msg[1] = len would write a 2 byte value into msg, the next line msg[2] = opcode should overwrite the second byte of len and all should go perfectly...(maybe msg[1] will have some problems depending on the endianess of the platform)

You also missed the point that the only part corrupted is msg[0] and it corrupted only if I use new to allocate the array dynamically...

And given the exact case (opcode= 0x0c, no data) all the theory (even if it was right) do not explains the 0x16 value comes on at msg[0]...
[no name] 17-Nov-15 2:30am    
Your response seems a little haughty as I am only trying to help you.
1. Agree with the typecasting - so you want the int truncated. A comment is good.
2. I can't run your code so only visual examination possible.
3. A better test would be to declare an array of 5 bytes and see what happens. If that replicates the error add and extra byte and zero it. If that fixes it then it may be how the data is being read.
In order to make sure of what the code is doing, I would fire the debugger on the code.
I think this code is a good example of when it become useful to run the debugger.

http://playground.arduino.cc/Code/VisualMicro[^]
http://www.visualmicro.com/page/Debugging-for-Arduino.aspx[^]
http://playground.arduino.cc/Main/DevelopmentTools[^]
http://stackoverflow.com/questions/7225693/how-do-you-debug-arduino-code-running-on-arduino-hardware[^]
 
Share this answer
 
v2
Comments
[no name] 16-Nov-15 21:37pm    
No built in debugger in Arduino environment.
http://www.codeproject.com/Articles/1037057/Debugger-for-Arduino
Patrice T 16-Nov-15 21:44pm    
Pitiful :(
But quick search gives results, are you sure of this ?
[no name] 16-Nov-15 21:46pm    
Yes - having used Arduino for many years I've learnt to live with it - Read the intro in the link I gave.
Kornfeld Eliyahu Peter 17-Nov-15 2:00am    
I would like to use a debugger, but even VisualMicro (the best I found and paid for!) not really helps here...
The problem is somewhere around the Serial.write and not with the byte-array...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900