Hello everyone,
I'm new in programming arduino and I'm trying to get some information from a motor with integrated electronic that uses CANBus for sharing informations.
The message that I'm sending is the following one and I'm already getting some answer from the electronic.
WHAT I NEED TO RECEIVE AND OBTAIN AT THE END
Send: 606 8 40 C2 20 00 00 00 00 00
Receive: 586 8 40 C2 20 00 05 7A 09 00
DATA RECEIVED: 05 7A 09 00
This value represent the data received from the motor electronic, it has to manipulated in order to get the following values:
-00 09 7A 05
Than it has to be converted from HEX value to Int Value (I know that are the same thing). I need to convert it from char[] to int value.
WHAT I RECEIVED
Send: 606 8 40 C2 20 00 00 00 00 00
Receive: 586 8 40 C2 20 00 05 7A 09
The last byte seems to be disappered, I don't know if I'm doing something wrong, but I got the answer to the rquest.
Can you please help me to find the solution and get the int value at the end?
Here below you can see the code for sending and receiving.
Thank you very much for your help,
SENDER + RECEIVER
#include <CAN.h>
//CAN
char CANBuffer[8];
char CANReadBuffer[8];
char StepsBuffer[4];
long idIn=0;
long steps;
char prova;
int StrToHex(char str[])
{
return (int) strtol(str, 0, 16);
}
void CANsend(long id, char *message, char msgLength) {
CAN.beginPacket(id);
for (char i = 0; i < msgLength; i ++) {
CAN.write(message[i]);
}
CAN.endPacket();
}
void setBuffer(char *inBuffer, char d0, char d1, char d2, char d3, char d4, char d5, char d6, char d7) {
inBuffer[0] = d0;
inBuffer[1] = d1;
inBuffer[2] = d2;
inBuffer[3] = d3;
inBuffer[4] = d4;
inBuffer[5] = d5;
inBuffer[6] = d6;
inBuffer[7] = d7;
}
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("CAN Sender");
// start the CAN bus at 500 kbps
if (!CAN.begin(125000)) {
Serial.println("Starting CAN failed!");
while (1);
}
}
void loop() {
// send packet: id is 11 bits, packet can contain up to 8 bytes of data
Serial.print("Sending packet ... ");
setBuffer(CANBuffer, 0x40, 0xC2, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00);
CANsend(0x606, CANBuffer, 8);
Serial.println("done");
delay(1000);
// try to parse packet
int packetSize = CAN.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received ");
// if (CAN.packetRtr()) {
// // Remote transmission request, packet contains no data
// Serial.print("RTR ");
// }
//
// Serial.print("packet with id 0x");
// Serial.print(CAN.packetId(), HEX);
//
// if (CAN.packetRtr()) {
// Serial.print(" and requested length ");
// Serial.println(CAN.packetDlc());
// } else {
// Serial.print(" and length ");
// Serial.println(packetSize);
//
// // only print packet data for non-RTR packets
while (CAN.available()) {
//*CANReadBuffer=(char)CAN.read();
//CANReadBuffer++;
//Serial.print((char)CAN.read(),HEX);
//prova=(char)CAN.read();
for (char i = 0; i < 10; i ++) {
CANReadBuffer[i] = CAN.read();
}
Serial.print(CANReadBuffer[0],HEX);
Serial.print(CANReadBuffer[1],HEX);
Serial.print(CANReadBuffer[2],HEX);
Serial.print(CANReadBuffer[3],HEX);
Serial.print(CANReadBuffer[4],HEX);
Serial.print(CANReadBuffer[5],HEX);
Serial.print(CANReadBuffer[6],HEX);
Serial.println(CANReadBuffer[7],HEX);
StepsBuffer[0] = CANReadBuffer[7];
StepsBuffer[1] = CANReadBuffer[6];
StepsBuffer[2] = CANReadBuffer[5];
StepsBuffer[3] = CANReadBuffer[4];
//StepsBuffer[4] = CANReadBuffer[3];
//StepsBuffer[5] = CANReadBuffer[2];
//StepsBuffer[6] = CANReadBuffer[1];
//StepsBuffer[7] = CANReadBuffer[0];
steps = StrToHex(StepsBuffer);
Serial.print(steps);
}
}
}