8 years ago.

can bus

/media/uploads/kelesekrem/15423675_10202342597682311_1681136491_n.png hi guys; I am trying to read the value of the potentiometer from the CAN bus line, I can read the values, but how can I read 16 bits in 10 system?

f7==247(decimal) ı want to decimal data?

posted by EKREM KELES 12 Dec 2016

1 Answer

8 years ago.

You can send various data types over CAN bus for example as follows:

Arduino code

...
const unsigned int  ID_SENSOR_VALUE = 0x201;
const unsigned int  ID_OUTPUT_VALUE = 0x202;
const unsigned int  ID_SENSOR_VOLTAGE = 0x203;
...
union CanDataType {
    byte  			data[8];		// 8 bytes (max length)
    unsigned int	sensorValue;	// 2 bytes
    char  			outputValue;	// 1 byte
    float 			sensorVoltage;	// 4 bytes
};
...
CanDataType  canData;
unsigned int sensorValue;           // 2 bytes
byte         outputValue;           // 1 byte
float        sensorVoltage;         // 4 bytes
...
void loop() {
    ...
    sensorValue = analogRead(analogInPin);
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    sensorVoltage = (sensorValue * 5) / 1023.0f;
    ...
    canData.sensorValue = sensorValue;
    CAN.sendMsgBuf(ID_SENSOR_VALUE , 0, sizeof(canData.sensorValue), &canData);
    delay(1000);
    canData.outputValue = outputValue;
    CAN.sendMsgBuf(ID_OUTPUT_VALUE, 0, sizeof(canData.outputValue), &canData);
    delay(1000);
    canData.sensorVoltage = sensorVoltage;
    CAN.sendMsgBuf(ID_SENSOR_VOLTAGE, 0, sizeof(canData.sensorVoltage), &canData);
    delay(1000);
}


LPC1768 code

#include "mbed.h"
 
const uint16_t ID_SENSOR_VALUE = 0x201;
const uint16_t ID_OUTPUT_VALUE = 0x202;
const uint16_t ID_SENSOR_VOLTAGE = 0x203;
 
Serial       pc(USBTX, USBRX);
DigitalOut   led1(LED1);
DigitalOut   led2(LED2);
CAN          can(p30, p29);
CANMessage   msg;
uint16_t     sensorValue;	// 2 bytes		
uint8_t      outputValue;	// 1 byte
float        sensorVoltage;	// 4 bytes
       
int main()
{
    pc.baud(9600);
    can.frequency(250000);

    while(1) {
        if(can.read(msg)) {
            led1 = !led1;
            pc.printf("CAN message received\r\n");
            pc.printf("  ID      = 0x%.3x\r\n", msg.id);
            pc.printf("  Type    = %d\r\n", msg.type);
            pc.printf("  Format  = %d\r\n", msg.format);
            pc.printf("  Length  = %d\r\n", msg.len);
            pc.printf("  Data    =");
			            
            for(int i = 0; i < msg.len; i++){
                pc.printf(" %.2x", msg.data[i]);
            }
            
            pc.printf("\r\n");
            
			switch(msg.id) {
	        case ID_SENSOR_VALUE:
				sensorValue = *((uint16_t*)msg.data);
				pc.printf("sensorValue = %d\r\n", sensorValue);
		        break;
	        case ID_OUTPUT_VALUE:
				outputValue = *((uint8_t*)msg.data);
				pc.printf("outputValue = %d\r\n", outputValue);
		        break;
	        case ID_SENSOR_VOLTAGE:
				sensorVoltage = *((float*)msg.data);
				pc.printf("sensorVoltage = %5.3fV\r\n", sensorVoltage);
		        break;
	        }
		}		
    }
}

Accepted Answer