salve a tutti,
da una connessione can, con questo aggeggio(max232, AOZ1282CI)
e con questo codice:
/*
Description:
Use ATOM CAN to send and receive information,
delete the macro definition `CAN_SENDER` to compile the receiving part
of the program,
and enable the macro definition `CAN_SENDER` to compile the sending part of
the program.
You need to use two ATOM CANs for testing
*/
#include "M5Atom.h"
#include "ESP32CAN.h"
#include "CAN_config.h"
boolean newData = false;
const byte numBytes = 8;
byte receivedBytes[numBytes];
byte numReceived = 0;
bool unit;
int inc;
int ft;
int cm;
int distanza;
//int mask = 268435710; // hex 0x100000FE
int mask = 282067198; // hex 0x10D000FE dovrebbe essere la misura...
#define CAN_UNIT
//#define CAN_SENDER
#define CAN_MSG_ID 0x001
CAN_device_t CAN_cfg; // CAN Config
unsigned long previousMillis = 0; // will store last time a CAN Message was send
const int interval = 80; // interval at which send CAN Messages (milliseconds)
const int rx_queue_size = 10; // Receive Queue size
int LedNumberCount = 0;
int waitCANData = 0;
uint8_t msgcnt = 0;
void setup()
{
M5.begin(true, true, true);
Serial.println("Atom Install OK....");
M5.dis.fillpix(CRGB(0, 100, 0));
CAN_cfg.speed = CAN_SPEED_1000KBPS;
//CAN_cfg.tx_pin_id = GPIO_NUM_26;
//CAN_cfg.rx_pin_id = GPIO_NUM_32;
CAN_cfg.tx_pin_id = GPIO_NUM_22;
CAN_cfg.rx_pin_id = GPIO_NUM_19;
CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t));
ESP32Can.CANInit();
Serial.println("CAN Install");
M5.dis.fillpix(CRGB(100, 0, 0));
}
void loop()
{
unsigned long currentMillis = millis();
#ifndef CAN_SENDER
// Send CAN Message
CAN_frame_t rx_frame;
if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE)
{
if (rx_frame.FIR.B.FF == CAN_frame_std)
{
Serial.printf("New standard frame");
}
else
{
Serial.printf("New extended frame");
}
if (rx_frame.FIR.B.RTR == CAN_RTR)
{
Serial.printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC);
}
else
{
Serial.printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC);
for (int i = 0; i < rx_frame.FIR.B.DLC; i++)
{
Serial.printf("0x%02X ", rx_frame.data.u8[i]);
// if ( rx_frame.MsgID == mask) {
// Serial.printf("0x%02X ", rx_frame.data.u8[i]);
// }
}
// //////////////////////////////////////////////////////////
Serial.printf("\n");
if ( rx_frame.data.u8[0] == 0x01 )
{
CAN_frame_t tx_frame;
tx_frame.FIR.B.FF = CAN_frame_std;
tx_frame.MsgID = CAN_MSG_ID;
tx_frame.FIR.B.DLC = 8;
tx_frame.data.u8[0] = 0x02;
tx_frame.data.u8[1] = rx_frame.data.u8[1];
tx_frame.data.u8[2] = 0x00;
tx_frame.data.u8[3] = 0x00;
tx_frame.data.u8[4] = 0x00;
tx_frame.data.u8[5] = 0x00;
tx_frame.data.u8[6] = 0x00;
tx_frame.data.u8[7] = 0x00;
ESP32Can.CANWriteFrame(&tx_frame);
}
}
}
#else
if (currentMillis - previousMillis >= interval)
{
//ESP32Can.CANInit();
previousMillis = currentMillis;
CAN_frame_t tx_frame;
tx_frame.FIR.B.FF = CAN_frame_std;
tx_frame.MsgID = CAN_MSG_ID;
tx_frame.FIR.B.DLC = 8;
tx_frame.data.u8[0] = 0x01;
tx_frame.data.u8[1] = msgcnt;
tx_frame.data.u8[2] = 0x00;
tx_frame.data.u8[3] = 0x00;
tx_frame.data.u8[4] = 0x00;
tx_frame.data.u8[5] = 0x00;
tx_frame.data.u8[6] = 0x00;
tx_frame.data.u8[7] = 0x00;
ESP32Can.CANWriteFrame(&tx_frame);
//ESP32Can.CANStop();
msgcnt ++;
if ( msgcnt > 29 )
{
msgcnt = 0;
}
}
CAN_frame_t rx_frame;
if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE)
{
if (rx_frame.FIR.B.FF == CAN_frame_std)
{
Serial.printf("New standard frame");
}
else
{
Serial.printf("New extended frame");
}
if (rx_frame.FIR.B.RTR == CAN_RTR)
{
Serial.printf(" RTR from 0x%08X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC);
}
else
{
Serial.printf(" from 0x%08X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC);
for (int i = 0; i < rx_frame.FIR.B.DLC; i++)
{
Serial.printf("0x%02X ", rx_frame.data.u8[i]);
}
Serial.printf("\n");
if ( rx_frame.data.u8[0] == 0x02)
{
if ( rx_frame.data.u8[1] < 25 )
{
M5.dis.drawpix( rx_frame.data.u8[1], CRGB(100, 0, 0));
}
else if ( rx_frame.data.u8[1] > 27 )
{
M5.dis.fillpix(CRGB(0, 0, 20));
}
waitCANData = 0;
}
}
}
#endif
//waitCANData
M5.update();
if (M5.Btn.isPressed())
{
M5.dis.clear();
esp_restart();
}
delay(10);
}
ottengo un output del genere:
New extended frame from 0x10D000FE, DLC 4, Data 0x12 0x83 0xB8 0x40
New extended frame from 0x100000FE, DLC 8, Data 0x00 0x02 0x00 0x80 0x00 0x8F 0x21 0x0C
New extended frame from 0x10D000FE, DLC 4, Data 0x12 0x83 0xB8 0x40
New extended frame from 0x10C600FE, DLC 8, Data 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
New extended frame from 0x10D000FE, DLC 4, Data 0x12 0x83 0xB8 0x40
New extended frame from 0x10C100FE, DLC 8, Data 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
New extended frame from 0x10D000FE, DLC 4, Data 0x12 0x83 0xB8 0x40
New extended frame from 0x10C500FE, DLC 1, Data 0x00
ora sto cercando di capire a cosa corrispondono quegli "header"
sono riuscito a filtrarne uno che credo mi interessi (quello con il dato da 4 byte) ed ora sto cercando di ricalcolare il valore della stringa.
ho provato a sommarli a due e due, i primi tre, gli ultimi tre etc etc ma non riesco a comprendere....
ho associato l'output CAN a quello del display dell'apparecchio ed ho stilato una tabella:
00 00 80 3f -------- 100
ae 47 81 3f -------- 101
5c 8f 82 3f -------- 102
0a d7 83 3f -------- 103
d8 1e 85 3f -------- 104
66 66 86 3f -------- 105
ed ancora
81 95 63 3f -------- 2 11
1b 2f 5d 3f -------- 2 10
48 e1 86 40 -------18 9
12 83 b8 40 -------1811
non riesco a trovare una relazione ......
qualche idea?