Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- //#include <ESP32CAN.h> // v1.0.0 from https://github.com/nhatuan84/arduino-esp32-can-demo
- //#include <CAN_config.h> // as above
- #include <CAN.h>
- #define DEBUG
- #define RGB_PIN 27
- #ifdef RGB_PIN
- #define FASTLED_ESP32_I2S true
- #include <FastLED.h>
- #define NUM_LEDS 1
- CRGB leds[NUM_LEDS];
- #endif
- // wifi config
- const char* ssid = "";
- const char* password = "";
- // rs-server config
- const int serverPort = 1234;
- // reading buffor config
- #define BUFFER_SIZE 1024
- // global objects
- WiFiServer server;
- WiFiClient client;
- byte buff[BUFFER_SIZE];
- boolean working = false;
- boolean bluetooth = false;
- boolean timestamp = false;
- boolean cr = false;
- int can_speed = 500;
- int ser_speed = 500000;
- static uint8_t hexval[17] = "0123456789ABCDEF";
- void slcan_ack()
- {
- //if (bluetooth) SerialBT.write('\r');
- //else Serial.write('\r');
- client.write('\r');
- } // slcan_ack()
- //----------------------------------------------------------------
- void slcan_nack()
- {
- //if (bluetooth) SerialBT.write('\a');
- //else Serial.write('\a');
- client.write('\a');
- } // slcan_nack()
- void pars_slcancmd(char *buf)
- { // LAWICEL PROTOCOL
- switch (buf[0]) {
- case 'O': // OPEN CAN
- working=true;
- //ESP32Can.CANInit();
- CAN.begin(500E3);
- slcan_ack();
- break;
- case 'C': // CLOSE CAN
- working=false;
- //ESP32Can.CANStop();
- CAN.end();
- slcan_ack();
- break;
- case 't': // SEND STD FRAME
- send_canmsg(buf,false,false);
- slcan_ack();
- break;
- case 'T': // SEND EXT FRAME
- send_canmsg(buf,false,true);
- slcan_ack();
- break;
- case 'r': // SEND STD RTR FRAME
- send_canmsg(buf,true,false);
- slcan_ack();
- break;
- case 'R': // SEND EXT RTR FRAME
- send_canmsg(buf,true,true);
- slcan_ack();
- break;
- case 'Z': // ENABLE TIMESTAMPS
- switch (buf[1]) {
- case '0': // TIMESTAMP OFF
- timestamp = false;
- slcan_ack();
- break;
- case '1': // TIMESTAMP ON
- timestamp = true;
- slcan_ack();
- break;
- default:
- break;
- }
- break;
- case 'M': ///set ACCEPTANCE CODE ACn REG
- slcan_ack();
- break;
- case 'm': // set ACCEPTANCE CODE AMn REG
- slcan_ack();
- break;
- case 's': // CUSTOM CAN bit-rate
- slcan_nack();
- break;
- case 'S': // CAN bit-rate
- switch (buf[1]) {
- case '0': // 10k
- slcan_nack();
- break;
- case '1': // 20k
- slcan_nack();
- break;
- case '2': // 50k
- slcan_nack();
- break;
- case '3': // 100k
- //CAN_cfg.speed=CAN_SPEED_100KBPS;
- CAN.end();
- CAN.begin(100E3);
- can_speed = 100;
- slcan_ack();
- break;
- case '4': // 125k
- //CAN_cfg.speed=CAN_SPEED_125KBPS;
- CAN.end();
- CAN.begin(125E3);
- can_speed = 125;
- slcan_ack();
- break;
- case '5': // 250k
- //CAN_cfg.speed=CAN_SPEED_250KBPS;
- CAN.end();
- CAN.begin(250E3);
- can_speed = 250;
- slcan_ack();
- break;
- case '6': // 500k
- //CAN_cfg.speed=CAN_SPEED_500KBPS;
- CAN.end();
- CAN.begin(500E3);
- can_speed = 500;
- slcan_ack();
- break;
- case '7': // 800k
- //CAN_cfg.speed=CAN_SPEED_800KBPS;
- CAN.end();
- CAN.begin(800E3);
- can_speed = 800;
- slcan_ack();
- break;
- case '8': // 1000k
- //CAN_cfg.speed=CAN_SPEED_1000KBPS;
- CAN.end();
- CAN.begin(1000E3);
- can_speed = 1000;
- slcan_ack();
- break;
- default:
- slcan_nack();
- break;
- }
- break;
- case 'F': // STATUS FLAGS
- //if (bluetooth) SerialBT.print("F00");
- //else Serial.print("F00");
- client.print("F00");
- slcan_ack();
- break;
- case 'V': // VERSION NUMBER
- //if (bluetooth) SerialBT.print("V1234");
- //else Serial.print("V1234");
- client.print("V1234");
- slcan_ack();
- break;
- case 'N': // SERIAL NUMBER
- //if (bluetooth) SerialBT.print("N2208");
- //else Serial.print("N2208");
- client.print("N2208");
- slcan_ack();
- break;
- case 'l': // (NOT SPEC) TOGGLE LINE FEED ON SERIAL
- cr = !cr;
- slcan_nack();
- break;
- case 'h': // (NOT SPEC) HELP SERIAL
- Serial.println();
- Serial.println("mintynet.com - slcan esp32");
- Serial.println();
- Serial.println("O\t=\tStart slcan");
- Serial.println("C\t=\tStop slcan");
- Serial.println("t\t=\tSend std frame");
- Serial.println("r\t=\tSend std rtr frame");
- Serial.println("T\t=\tSend ext frame");
- Serial.println("R\t=\tSend ext rtr frame");
- Serial.println("Z0\t=\tTimestamp Off");
- Serial.println("Z1\t=\tTimestamp On");
- Serial.println("snn\t=\tSpeed 0xnnk N/A");
- Serial.println("S0\t=\tSpeed 10k N/A");
- Serial.println("S1\t=\tSpeed 20k N/A");
- Serial.println("S2\t=\tSpeed 50k N/A");
- Serial.println("S3\t=\tSpeed 100k");
- Serial.println("S4\t=\tSpeed 125k");
- Serial.println("S5\t=\tSpeed 250k");
- Serial.println("S6\t=\tSpeed 500k");
- Serial.println("S7\t=\tSpeed 800k");
- Serial.println("S8\t=\tSpeed 1000k");
- Serial.println("F\t=\tFlags N/A");
- Serial.println("N\t=\tSerial No");
- Serial.println("V\t=\tVersion");
- Serial.println("-----NOT SPEC-----");
- Serial.println("h\t=\tHelp");
- Serial.print("l\t=\tToggle CR ");
- if (cr) {
- Serial.println("ON");
- } else {
- Serial.println("OFF");
- }
- Serial.print("CAN_SPEED:\t");
- switch(can_speed) {
- case 100:
- Serial.print("100");
- break;
- case 125:
- Serial.print("125");
- break;
- case 250:
- Serial.print("250");
- break;
- case 500:
- Serial.print("500");
- break;
- case 800:
- Serial.print("800");
- break;
- case 1000:
- Serial.print("1000");
- break;
- default:
- break;
- }
- Serial.print("kbps");
- if (timestamp) {
- Serial.print("\tT");
- }
- if (working) {
- Serial.print("\tON");
- } else {
- Serial.print("\tOFF");
- }
- Serial.println();
- slcan_nack();
- break;
- default:
- slcan_nack();
- break;
- }
- } // pars_slcancmd()
- void transfer_tty2can()
- {
- int ser_length;
- static char cmdbuf[32];
- static int cmdidx = 0;
- /*
- if (bluetooth) {
- if ((ser_length = SerialBT.available()) > 0) {
- for (int i = 0; i < ser_length; i++) {
- char val = SerialBT.read();
- cmdbuf[cmdidx++] = val;
- if (cmdidx == 32)
- {
- slcan_nack();
- cmdidx = 0;
- } else if (val == '\r')
- {
- cmdbuf[cmdidx] = '\0';
- pars_slcancmd(cmdbuf);
- cmdidx = 0;
- }
- }
- }
- } else {
- if ((ser_length = Serial.available()) > 0) {
- for (int i = 0; i < ser_length; i++) {
- char val = Serial.read();
- cmdbuf[cmdidx++] = val;
- if (cmdidx == 32)
- {
- slcan_nack();
- cmdidx = 0;
- } else if (val == '\r')
- {
- cmdbuf[cmdidx] = '\0';
- pars_slcancmd(cmdbuf);
- cmdidx = 0;
- }
- }
- }
- }
- */
- if ((ser_length = client.available()) > 0) {
- for (int i = 0; i < ser_length; i++) {
- char val = client.read();
- cmdbuf[cmdidx++] = val;
- if (cmdidx == 32)
- {
- slcan_nack();
- cmdidx = 0;
- } else if (val == '\r')
- {
- cmdbuf[cmdidx] = '\0';
- pars_slcancmd(cmdbuf);
- cmdidx = 0;
- }
- }
- }
- } // transfer_tty2can()
- //----------------------------------------------------------------
- void transfer_can2tty()
- {
- //CAN_frame_t rx_frame;
- String command = "";
- long time_now = 0;
- //receive next CAN frame from queue
- //if(xQueueReceive(CAN_cfg.rx_queue,&rx_frame, 3*portTICK_PERIOD_MS)==pdTRUE) {
- int packetSize = CAN.parsePacket();
- if (packetSize) {
- //do stuff!
- int DLC = CAN.packetDlc();
- if(working) {
- //if(rx_frame.FIR.B.FF==CAN_frame_ext) {
- if (CAN.packetExtended()) {
- //if (rx_frame.FIR.B.RTR==CAN_RTR) {
- if (CAN.packetRtr()) {
- command = command + "R";
- } else {
- command = command + "T";
- }
- /*
- command = command + char(hexval[ (rx_frame.MsgID>>28)&1]);
- command = command + char(hexval[ (rx_frame.MsgID>>24)&15]);
- command = command + char(hexval[ (rx_frame.MsgID>>20)&15]);
- command = command + char(hexval[ (rx_frame.MsgID>>16)&15]);
- command = command + char(hexval[ (rx_frame.MsgID>>12)&15]);
- command = command + char(hexval[ (rx_frame.MsgID>>8)&15]);
- command = command + char(hexval[ (rx_frame.MsgID>>4)&15]);
- command = command + char(hexval[ rx_frame.MsgID&15]);
- command = command + char(hexval[ rx_frame.FIR.B.DLC ]);
- */
- long MsgID = CAN.packetId();
- command = command + char(hexval[ (MsgID>>28)&1]);
- command = command + char(hexval[ (MsgID>>24)&15]);
- command = command + char(hexval[ (MsgID>>20)&15]);
- command = command + char(hexval[ (MsgID>>16)&15]);
- command = command + char(hexval[ (MsgID>>12)&15]);
- command = command + char(hexval[ (MsgID>>8)&15]);
- command = command + char(hexval[ (MsgID>>4)&15]);
- command = command + char(hexval[ MsgID&15]);
- command = command + char(hexval[ DLC ]);
- } else {
- //if (rx_frame.FIR.B.RTR==CAN_RTR) {
- if (CAN.packetRtr()) {
- command = command + "r";
- } else {
- command = command + "t";
- }
- /*
- command = command + char(hexval[ (rx_frame.MsgID>>8)&15]);
- command = command + char(hexval[ (rx_frame.MsgID>>4)&15]);
- command = command + char(hexval[ rx_frame.MsgID&15]);
- command = command + char(hexval[ rx_frame.FIR.B.DLC ]);
- */
- long MsgID = CAN.packetId();
- command = command + char(hexval[ (MsgID>>8)&15]);
- command = command + char(hexval[ (MsgID>>4)&15]);
- command = command + char(hexval[ MsgID&15]);
- command = command + char(hexval[ DLC ]);
- }
- //for(int i = 0; i < rx_frame.FIR.B.DLC; i++){
- for(int i = 0; i < DLC; i++){
- int b = CAN.read();
- //command = command + char(hexval[ rx_frame.data.u8[i]>>4 ]);
- //command = command + char(hexval[ rx_frame.data.u8[i]&15 ]);
- command = command + char(hexval[ b>>4 ]);
- command = command + char(hexval[ b&15 ]);
- //printf("%c\t", (char)rx_frame.data.u8[i]);
- }
- if (timestamp) {
- time_now = millis() % 60000;
- command = command + char(hexval[ (time_now>>12)&15 ]);
- command = command + char(hexval[ (time_now>>8)&15 ]);
- command = command + char(hexval[ (time_now>>4)&15 ]);
- command = command + char(hexval[ time_now&15 ]);
- }
- command = command + '\r';
- //if (bluetooth) SerialBT.print(command);
- //else Serial.print(command);
- client.print(command);
- if (cr) Serial.println("");
- }
- }
- } // transfer_can2tty()
- //----------------------------------------------------------------
- void send_canmsg(char *buf, boolean rtr, boolean ext) {
- if (!working) {
- } else {
- int msg_len = 0;
- if (ext) {
- sscanf(&buf[9], "%01x", &msg_len);
- } else {
- sscanf(&buf[4], "%01x", &msg_len);
- }
- //CAN_frame_t tx_frame;
- int msg_id = 0;
- int msg_ide = 0;
- if (rtr) {
- if (ext) {
- sscanf(&buf[1], "%04x%04x", &msg_ide, &msg_id);
- //tx_frame.FIR.B.RTR = CAN_RTR;
- //tx_frame.FIR.B.FF = CAN_frame_ext;
- CAN.beginExtendedPacket((msg_ide*65536 + msg_id), msg_len, true);
- } else {
- sscanf(&buf[1], "%03x", &msg_id);
- //tx_frame.FIR.B.RTR = CAN_RTR;
- //tx_frame.FIR.B.FF = CAN_frame_std;
- CAN.beginPacket(msg_id, msg_len, true);
- }
- } else {
- if (ext) {
- sscanf(&buf[1], "%04x%04x", &msg_ide, &msg_id);
- //tx_frame.FIR.B.RTR = CAN_no_RTR;
- //tx_frame.FIR.B.FF = CAN_frame_ext;
- CAN.beginExtendedPacket((msg_ide*65536 + msg_id), msg_len);
- } else {
- sscanf(&buf[1], "%03x", &msg_id);
- //tx_frame.FIR.B.RTR = CAN_no_RTR;
- //tx_frame.FIR.B.FF = CAN_frame_std;
- CAN.beginPacket(msg_id, msg_len, true);
- }
- }
- //tx_frame.MsgID = msg_ide*65536 + msg_id;
- /*
- int msg_len = 0;
- if (ext) {
- sscanf(&buf[9], "%01x", &msg_len);
- } else {
- sscanf(&buf[4], "%01x", &msg_len);
- }
- tx_frame.FIR.B.DLC = msg_len;
- */
- int candata = 0;
- if (ext) {
- for (int i = 0; i < msg_len; i++) {
- sscanf(&buf[10 + (i*2)], "%02x", &candata);
- //tx_frame.data.u8[i] = candata;
- CAN.write(candata);
- }
- } else {
- for (int i = 0; i < msg_len; i++) {
- sscanf(&buf[5 + (i*2)], "%02x", &candata);
- //tx_frame.data.u8[i] = candata;
- CAN.write(candata);
- }
- }
- //ESP32Can.CANWriteFrame(&tx_frame);
- CAN.endPacket();
- }
- } // send_canmsg()
- void setup() {
- #ifdef DEBUG
- Serial.begin(115200);
- #endif
- #ifdef RGB_PIN
- FastLED.addLeds<WS2812B, RGB_PIN, GRB>(leds, NUM_LEDS);
- leds[0].setRGB(10,0,0);
- FastLED.show();
- #endif
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- }
- #ifdef DEBUG
- Serial.println("connected to WiFi");
- Serial.println("IP adddr: ");
- Serial.println(WiFi.localIP());
- #endif
- server = WiFiServer(serverPort);
- server.begin();
- //CAN_cfg.speed=CAN_SPEED_500KBPS;
- //CAN_cfg.tx_pin_id = GPIO_NUM_26;
- //CAN_cfg.rx_pin_id = GPIO_NUM_5;
- //CAN_cfg.rx_queue = xQueueCreate(10,sizeof(CAN_frame_t));
- CAN.setPins(26,32);
- CAN.begin(500E3);
- #ifdef RGB_PIN
- leds[0].setRGB(0,3,0);
- FastLED.show();
- #endif
- }
- void loop() {
- // wait for client
- client = server.available();
- if (!client)
- return;
- #ifdef DEBUG
- Serial.println("Client Found");
- #endif
- #ifdef RGB_PIN
- leds[0].setRGB(0,0,10);
- FastLED.show();
- #endif
- while(CAN.available()){
- CAN.read();
- }
- while (client.connected()) {
- transfer_can2tty();
- transfer_tty2can();
- /*
- int size = 0;
- // read data from wifi client and send to serial
- while ((size = client.available())) {
- size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
- client.read(buff, size);
- Serial.write(buff, size);
- Serial.flush();
- }
- // read data from serial and send to wifi client
- while ((size = Serial.available())) {
- size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
- Serial.readBytes(buff, size);
- client.write(buff, size);
- client.flush();
- }
- */
- }
- #ifdef DEBUG
- Serial.println("Client Disconnected!");
- #endif
- #ifdef RGB_PIN
- leds[0].setRGB(0,3,0);
- FastLED.show();
- #endif
- client.stop();
- }
Add Comment
Please, Sign In to add comment