Hello everyone;
I'm trying to read CAN information from a device, all data comes in at once without any problems. But I want to assign each data to a variable separately, but the memcpy command gives it in one go. I use mcp2515.h library
float k ;
if (canMsg1.can_id == 0x405)
{ uint8_t weight[4] = {(canMsg1.data[1]), (canMsg1.data[2]),(canMsg1.data[3]),(canMsg1.data[4])};
memcpy (&k, weight, 4);
Serial.println (k);
}
I moved your topic to an appropriate forum category @brave33.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
If you learn more about how data is stored in RAM you would know that you have no problem with buffered bytes when you can address them relative to the buffer they are in.
The name of every array compiles the address of the start of the array. Base + 0 is base[0], etc, but once you can point you don't need the brackets.
Any 4 bytes in RAM can be read as a float, when you point to the right 4 bytes you get the right value meaning that every float saved in that buffer can be read directly without copying elsewhere. Just point and there's the right 4 bytes.
This requires learning the first few things about pointers which is not a beginner subject but your code needs just the start, address and offset.
When you break through the veil of syntax and see the simple underside, code gets easier to understand.
Add: I just need to get past the C++ hall Monitor.
It depends on whether there is actually a float object at that location. If not (even if the bit pattern present can be reinterpreted as a float), the strict aliasing rule is violated, which leads to undefined behaviour.
Thank you all. I get the same output, if I need to write more clearly I found the memcpy command by accident, I'm a bit of a beginner. My exact goal is this:
Assigning the numbers appearing on the serial screen to a separate variable and using them in my code.
for example:
2.5 ......> a
3 .......> b
4.6 .......> c
3.4 .......> d
No, that's probably not a good idea, leading to unnecessarily long, repetitive code. An array might be better. Tell us more about how these values arrive and how you want to use them in your code.
These values come from a device as CAN data, there are 4 different weight data, and I want to assign these 4 different weight data to separate variables and use them, I hope it was explanatory.
Serial screen output comes as 4 separate values at the same time as seen,
I for example float the value 2.5 a; I want to assign it to a variable, so how can I do it as an array?
Do all 4 come in the same message or in different messages?
No, it wasn't. You haven't said why you want to use separate variables or how you will use them. These details are important for figuring out the best way to deal with them.
All 4 come from the same message, just like I shared in the code. I am trying to develop my code, the reason why I want to use 4 different weight data separately is because I want to save them separately in the continuation of the code and perform mathematical operations with them. For example, plus the variables a-b-c-d, or minus the variable a from the variable d...
Ok, thanks for sharing that. It was not clear from your previous explanations.
No, in the code you shared, the message contained only one value. Now you are saying it contains 4 values. Please share more details of how the other 3 values can be extracted from the message.
If you are talking about my 11th message, I only gave the first value 2.5 as an example, but as you can see at the top, there are 4 values underneath. Again, the lines with // in the code I shared in my 11th message are the lines I wrote before, but the other active lines are the lines you gave me. So I get the same output from both.
I tried the value of canMsg1.data[1] , [1] here as 2-3-4 and my outputs are always
0.0
0.0
0.0
0.0
it happened.
I guess it's just data from data[1], for example
2.5
3
4.6
3.4