firtel

ESP32-SLCAN

May 23rd, 2022 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 15.82 KB | None | 0 0
  1. #include <WiFi.h>
  2. //#include <ESP32CAN.h>           // v1.0.0     from https://github.com/nhatuan84/arduino-esp32-can-demo
  3. //#include <CAN_config.h>         // as above
  4. #include <CAN.h>
  5.  
  6. #define DEBUG
  7. #define RGB_PIN 27
  8.  
  9. #ifdef RGB_PIN
  10. #define FASTLED_ESP32_I2S true
  11. #include <FastLED.h>
  12. #define NUM_LEDS 1
  13. CRGB leds[NUM_LEDS];
  14. #endif
  15.  
  16. // wifi config
  17. const char* ssid     = "";
  18. const char* password = "";
  19.  
  20. // rs-server config
  21. const int serverPort = 1234;
  22.  
  23. // reading buffor config
  24. #define BUFFER_SIZE 1024
  25.  
  26. // global objects
  27. WiFiServer server;
  28. WiFiClient client;
  29. byte buff[BUFFER_SIZE];
  30.  
  31. boolean working           = false;
  32. boolean bluetooth         = false;
  33. boolean timestamp         = false;
  34. boolean cr                = false;
  35. int can_speed             = 500;
  36. int ser_speed             = 500000;
  37.  
  38. static uint8_t hexval[17] = "0123456789ABCDEF";
  39.  
  40. void slcan_ack()
  41. {
  42.   //if (bluetooth) SerialBT.write('\r');
  43.   //else Serial.write('\r');
  44.   client.write('\r');
  45. } // slcan_ack()
  46.  
  47. //----------------------------------------------------------------
  48.  
  49. void slcan_nack()
  50. {
  51.   //if (bluetooth) SerialBT.write('\a');
  52.   //else Serial.write('\a');
  53.   client.write('\a');
  54. } // slcan_nack()
  55.  
  56. void pars_slcancmd(char *buf)
  57. {                           // LAWICEL PROTOCOL
  58.   switch (buf[0]) {
  59.     case 'O':               // OPEN CAN
  60.       working=true;
  61.       //ESP32Can.CANInit();
  62.       CAN.begin(500E3);
  63.       slcan_ack();
  64.       break;
  65.     case 'C':               // CLOSE CAN
  66.       working=false;
  67.       //ESP32Can.CANStop();
  68.       CAN.end();
  69.       slcan_ack();
  70.       break;
  71.     case 't':               // SEND STD FRAME
  72.       send_canmsg(buf,false,false);
  73.       slcan_ack();
  74.       break;
  75.     case 'T':               // SEND EXT FRAME
  76.       send_canmsg(buf,false,true);
  77.       slcan_ack();
  78.       break;
  79.     case 'r':               // SEND STD RTR FRAME
  80.       send_canmsg(buf,true,false);
  81.       slcan_ack();
  82.       break;
  83.     case 'R':               // SEND EXT RTR FRAME
  84.       send_canmsg(buf,true,true);
  85.       slcan_ack();
  86.       break;
  87.     case 'Z':               // ENABLE TIMESTAMPS
  88.       switch (buf[1]) {
  89.         case '0':           // TIMESTAMP OFF  
  90.           timestamp = false;
  91.           slcan_ack();
  92.           break;
  93.         case '1':           // TIMESTAMP ON
  94.           timestamp = true;
  95.           slcan_ack();
  96.           break;
  97.         default:
  98.           break;
  99.       }
  100.       break;
  101.     case 'M':               ///set ACCEPTANCE CODE ACn REG
  102.       slcan_ack();
  103.       break;
  104.     case 'm':               // set ACCEPTANCE CODE AMn REG
  105.       slcan_ack();
  106.       break;
  107.     case 's':               // CUSTOM CAN bit-rate
  108.       slcan_nack();
  109.       break;
  110.     case 'S':               // CAN bit-rate
  111.       switch (buf[1]) {
  112.         case '0':           // 10k  
  113.           slcan_nack();
  114.           break;
  115.         case '1':           // 20k
  116.           slcan_nack();
  117.           break;
  118.         case '2':           // 50k
  119.           slcan_nack();
  120.           break;
  121.         case '3':           // 100k
  122.           //CAN_cfg.speed=CAN_SPEED_100KBPS;
  123.           CAN.end();
  124.           CAN.begin(100E3);
  125.           can_speed = 100;
  126.           slcan_ack();
  127.           break;
  128.         case '4':           // 125k
  129.           //CAN_cfg.speed=CAN_SPEED_125KBPS;
  130.           CAN.end();
  131.           CAN.begin(125E3);
  132.           can_speed = 125;
  133.           slcan_ack();
  134.           break;
  135.         case '5':           // 250k
  136.           //CAN_cfg.speed=CAN_SPEED_250KBPS;
  137.           CAN.end();
  138.           CAN.begin(250E3);
  139.           can_speed = 250;
  140.          slcan_ack();
  141.           break;
  142.         case '6':           // 500k
  143.           //CAN_cfg.speed=CAN_SPEED_500KBPS;
  144.           CAN.end();
  145.           CAN.begin(500E3);
  146.           can_speed = 500;
  147.           slcan_ack();
  148.           break;
  149.         case '7': // 800k
  150.           //CAN_cfg.speed=CAN_SPEED_800KBPS;
  151.           CAN.end();
  152.           CAN.begin(800E3);
  153.           can_speed = 800;
  154.           slcan_ack();
  155.           break;
  156.         case '8':           // 1000k
  157.           //CAN_cfg.speed=CAN_SPEED_1000KBPS;
  158.           CAN.end();
  159.           CAN.begin(1000E3);
  160.           can_speed = 1000;
  161.           slcan_ack();
  162.           break;
  163.         default:
  164.           slcan_nack();
  165.           break;
  166.       }
  167.       break;
  168.     case 'F':               // STATUS FLAGS
  169.       //if (bluetooth) SerialBT.print("F00");
  170.       //else Serial.print("F00");
  171.       client.print("F00");
  172.       slcan_ack();
  173.       break;
  174.     case 'V':               // VERSION NUMBER
  175.       //if (bluetooth) SerialBT.print("V1234");
  176.       //else Serial.print("V1234");
  177.       client.print("V1234");
  178.       slcan_ack();
  179.       break;
  180.     case 'N':               // SERIAL NUMBER
  181.       //if (bluetooth) SerialBT.print("N2208");
  182.       //else Serial.print("N2208");
  183.       client.print("N2208");
  184.       slcan_ack();
  185.       break;
  186.     case 'l':               // (NOT SPEC) TOGGLE LINE FEED ON SERIAL
  187.       cr = !cr;
  188.       slcan_nack();
  189.       break;
  190.     case 'h':               // (NOT SPEC) HELP SERIAL
  191.       Serial.println();
  192.       Serial.println("mintynet.com - slcan esp32");
  193.       Serial.println();
  194.       Serial.println("O\t=\tStart slcan");
  195.       Serial.println("C\t=\tStop slcan");
  196.       Serial.println("t\t=\tSend std frame");
  197.       Serial.println("r\t=\tSend std rtr frame");
  198.       Serial.println("T\t=\tSend ext frame");
  199.       Serial.println("R\t=\tSend ext rtr frame");
  200.       Serial.println("Z0\t=\tTimestamp Off");
  201.       Serial.println("Z1\t=\tTimestamp On");
  202.       Serial.println("snn\t=\tSpeed 0xnnk N/A");
  203.       Serial.println("S0\t=\tSpeed 10k N/A");
  204.       Serial.println("S1\t=\tSpeed 20k N/A");
  205.       Serial.println("S2\t=\tSpeed 50k N/A");
  206.       Serial.println("S3\t=\tSpeed 100k");
  207.       Serial.println("S4\t=\tSpeed 125k");
  208.       Serial.println("S5\t=\tSpeed 250k");
  209.       Serial.println("S6\t=\tSpeed 500k");
  210.       Serial.println("S7\t=\tSpeed 800k");
  211.       Serial.println("S8\t=\tSpeed 1000k");
  212.       Serial.println("F\t=\tFlags N/A");
  213.       Serial.println("N\t=\tSerial No");
  214.       Serial.println("V\t=\tVersion");
  215.       Serial.println("-----NOT SPEC-----");
  216.       Serial.println("h\t=\tHelp");
  217.       Serial.print("l\t=\tToggle CR ");
  218.       if (cr) {
  219.         Serial.println("ON");
  220.       } else {
  221.         Serial.println("OFF");
  222.       }
  223.       Serial.print("CAN_SPEED:\t");
  224.       switch(can_speed) {
  225.         case 100:
  226.           Serial.print("100");
  227.           break;
  228.         case 125:
  229.           Serial.print("125");
  230.           break;
  231.         case 250:
  232.           Serial.print("250");
  233.           break;
  234.         case 500:
  235.           Serial.print("500");
  236.           break;
  237.         case 800:
  238.           Serial.print("800");
  239.           break;
  240.         case 1000:
  241.           Serial.print("1000");
  242.           break;
  243.         default:
  244.           break;
  245.       }
  246.       Serial.print("kbps");
  247.       if (timestamp) {
  248.         Serial.print("\tT");
  249.       }
  250.       if (working) {
  251.         Serial.print("\tON");
  252.       } else {
  253.         Serial.print("\tOFF");
  254.       }
  255.       Serial.println();
  256.       slcan_nack();
  257.       break;
  258.     default:
  259.       slcan_nack();
  260.       break;
  261.   }
  262. } // pars_slcancmd()
  263.  
  264. void transfer_tty2can()
  265. {
  266.   int ser_length;
  267.   static char cmdbuf[32];
  268.   static int cmdidx = 0;
  269.   /*
  270.   if (bluetooth) {
  271.     if ((ser_length = SerialBT.available()) > 0) {
  272.       for (int i = 0; i < ser_length; i++) {
  273.         char val = SerialBT.read();
  274.         cmdbuf[cmdidx++] = val;
  275.         if (cmdidx == 32)
  276.         {
  277.           slcan_nack();
  278.           cmdidx = 0;
  279.         } else if (val == '\r')
  280.         {
  281.           cmdbuf[cmdidx] = '\0';
  282.           pars_slcancmd(cmdbuf);
  283.           cmdidx = 0;
  284.         }
  285.       }
  286.     }
  287.   } else {
  288.     if ((ser_length = Serial.available()) > 0) {
  289.       for (int i = 0; i < ser_length; i++) {
  290.         char val = Serial.read();
  291.         cmdbuf[cmdidx++] = val;
  292.         if (cmdidx == 32)
  293.         {
  294.           slcan_nack();
  295.           cmdidx = 0;
  296.         } else if (val == '\r')
  297.         {
  298.           cmdbuf[cmdidx] = '\0';
  299.           pars_slcancmd(cmdbuf);
  300.           cmdidx = 0;
  301.         }
  302.       }
  303.     }
  304.   }
  305.   */
  306.     if ((ser_length = client.available()) > 0) {
  307.       for (int i = 0; i < ser_length; i++) {
  308.         char val = client.read();
  309.         cmdbuf[cmdidx++] = val;
  310.         if (cmdidx == 32)
  311.         {
  312.           slcan_nack();
  313.           cmdidx = 0;
  314.         } else if (val == '\r')
  315.         {
  316.           cmdbuf[cmdidx] = '\0';
  317.           pars_slcancmd(cmdbuf);
  318.           cmdidx = 0;
  319.         }
  320.       }
  321.     }
  322. } // transfer_tty2can()
  323.  
  324. //----------------------------------------------------------------
  325.  
  326.  
  327. void transfer_can2tty()
  328. {
  329.   //CAN_frame_t rx_frame;
  330.   String command = "";
  331.   long time_now = 0;
  332.   //receive next CAN frame from queue
  333.   //if(xQueueReceive(CAN_cfg.rx_queue,&rx_frame, 3*portTICK_PERIOD_MS)==pdTRUE) {
  334.   int packetSize = CAN.parsePacket();
  335.   if (packetSize) {
  336.     //do stuff!
  337.     int DLC = CAN.packetDlc();
  338.     if(working) {
  339.       //if(rx_frame.FIR.B.FF==CAN_frame_ext) {
  340.       if (CAN.packetExtended()) {
  341.         //if (rx_frame.FIR.B.RTR==CAN_RTR) {
  342.         if (CAN.packetRtr()) {
  343.           command = command + "R";
  344.         } else {
  345.           command = command + "T";
  346.         }
  347.         /*
  348.         command = command + char(hexval[ (rx_frame.MsgID>>28)&1]);
  349.         command = command + char(hexval[ (rx_frame.MsgID>>24)&15]);
  350.         command = command + char(hexval[ (rx_frame.MsgID>>20)&15]);
  351.         command = command + char(hexval[ (rx_frame.MsgID>>16)&15]);
  352.         command = command + char(hexval[ (rx_frame.MsgID>>12)&15]);
  353.         command = command + char(hexval[ (rx_frame.MsgID>>8)&15]);
  354.         command = command + char(hexval[ (rx_frame.MsgID>>4)&15]);
  355.         command = command + char(hexval[ rx_frame.MsgID&15]);
  356.         command = command + char(hexval[ rx_frame.FIR.B.DLC ]);
  357.         */
  358.         long MsgID = CAN.packetId();
  359.         command = command + char(hexval[ (MsgID>>28)&1]);
  360.         command = command + char(hexval[ (MsgID>>24)&15]);
  361.         command = command + char(hexval[ (MsgID>>20)&15]);
  362.         command = command + char(hexval[ (MsgID>>16)&15]);
  363.         command = command + char(hexval[ (MsgID>>12)&15]);
  364.         command = command + char(hexval[ (MsgID>>8)&15]);
  365.         command = command + char(hexval[ (MsgID>>4)&15]);
  366.         command = command + char(hexval[ MsgID&15]);
  367.         command = command + char(hexval[ DLC ]);
  368.  
  369.        
  370.       } else {
  371.         //if (rx_frame.FIR.B.RTR==CAN_RTR) {
  372.         if (CAN.packetRtr()) {
  373.           command = command + "r";
  374.         } else {
  375.           command = command + "t";
  376.         }
  377.         /*
  378.         command = command + char(hexval[ (rx_frame.MsgID>>8)&15]);
  379.         command = command + char(hexval[ (rx_frame.MsgID>>4)&15]);
  380.         command = command + char(hexval[ rx_frame.MsgID&15]);
  381.         command = command + char(hexval[ rx_frame.FIR.B.DLC ]);
  382.         */
  383.         long MsgID = CAN.packetId();
  384.         command = command + char(hexval[ (MsgID>>8)&15]);
  385.         command = command + char(hexval[ (MsgID>>4)&15]);
  386.         command = command + char(hexval[ MsgID&15]);
  387.         command = command + char(hexval[ DLC ]);
  388.        
  389.       }
  390.       //for(int i = 0; i < rx_frame.FIR.B.DLC; i++){
  391.       for(int i = 0; i < DLC; i++){
  392.         int b = CAN.read();
  393.         //command = command + char(hexval[ rx_frame.data.u8[i]>>4 ]);
  394.         //command = command + char(hexval[ rx_frame.data.u8[i]&15 ]);
  395.         command = command + char(hexval[ b>>4 ]);
  396.         command = command + char(hexval[ b&15 ]);
  397.         //printf("%c\t", (char)rx_frame.data.u8[i]);
  398.       }
  399.     if (timestamp) {
  400.       time_now = millis() % 60000;
  401.       command = command + char(hexval[ (time_now>>12)&15 ]);
  402.       command = command + char(hexval[ (time_now>>8)&15 ]);
  403.       command = command + char(hexval[ (time_now>>4)&15 ]);
  404.       command = command + char(hexval[ time_now&15 ]);
  405.     }
  406.     command = command + '\r';
  407.     //if (bluetooth) SerialBT.print(command);
  408.     //else Serial.print(command);
  409.     client.print(command);
  410.     if (cr) Serial.println("");
  411.     }
  412.   }
  413. } // transfer_can2tty()
  414.  
  415.  
  416. //----------------------------------------------------------------
  417.  
  418. void send_canmsg(char *buf, boolean rtr, boolean ext) {
  419.   if (!working) {
  420.  
  421.   } else {
  422.     int msg_len = 0;
  423.     if (ext) {
  424.       sscanf(&buf[9], "%01x", &msg_len);
  425.     } else {
  426.       sscanf(&buf[4], "%01x", &msg_len);
  427.     }
  428.     //CAN_frame_t tx_frame;
  429.     int msg_id = 0;
  430.     int msg_ide = 0;
  431.     if (rtr) {
  432.       if (ext) {
  433.         sscanf(&buf[1], "%04x%04x", &msg_ide, &msg_id);
  434.         //tx_frame.FIR.B.RTR = CAN_RTR;
  435.         //tx_frame.FIR.B.FF = CAN_frame_ext;
  436.         CAN.beginExtendedPacket((msg_ide*65536 + msg_id), msg_len, true);
  437.       } else {
  438.         sscanf(&buf[1], "%03x", &msg_id);
  439.         //tx_frame.FIR.B.RTR = CAN_RTR;
  440.         //tx_frame.FIR.B.FF = CAN_frame_std;
  441.         CAN.beginPacket(msg_id, msg_len, true);
  442.       }
  443.     } else {
  444.       if (ext) {
  445.         sscanf(&buf[1], "%04x%04x", &msg_ide, &msg_id);
  446.         //tx_frame.FIR.B.RTR = CAN_no_RTR;
  447.         //tx_frame.FIR.B.FF = CAN_frame_ext;
  448.         CAN.beginExtendedPacket((msg_ide*65536 + msg_id), msg_len);
  449.       } else {
  450.         sscanf(&buf[1], "%03x", &msg_id);
  451.         //tx_frame.FIR.B.RTR = CAN_no_RTR;
  452.         //tx_frame.FIR.B.FF = CAN_frame_std;
  453.         CAN.beginPacket(msg_id, msg_len, true);
  454.       }
  455.     }
  456.     //tx_frame.MsgID = msg_ide*65536 + msg_id;
  457.     /*
  458.     int msg_len = 0;
  459.     if (ext) {
  460.       sscanf(&buf[9], "%01x", &msg_len);
  461.     } else {
  462.       sscanf(&buf[4], "%01x", &msg_len);
  463.     }
  464.     tx_frame.FIR.B.DLC = msg_len;
  465.     */
  466.     int candata = 0;
  467.     if (ext) {
  468.       for (int i = 0; i < msg_len; i++) {
  469.         sscanf(&buf[10 + (i*2)], "%02x", &candata);
  470.         //tx_frame.data.u8[i] = candata;
  471.         CAN.write(candata);
  472.       }
  473.     } else {
  474.       for (int i = 0; i < msg_len; i++) {
  475.         sscanf(&buf[5 + (i*2)], "%02x", &candata);
  476.         //tx_frame.data.u8[i] = candata;
  477.         CAN.write(candata);
  478.       }
  479.     }
  480.     //ESP32Can.CANWriteFrame(&tx_frame);
  481.     CAN.endPacket();
  482.   }
  483. } // send_canmsg()
  484.  
  485.  
  486. void setup() {
  487.   #ifdef DEBUG
  488.   Serial.begin(115200);
  489.   #endif
  490.  
  491.   #ifdef RGB_PIN
  492.   FastLED.addLeds<WS2812B, RGB_PIN, GRB>(leds, NUM_LEDS);
  493.   leds[0].setRGB(10,0,0);
  494.   FastLED.show();
  495.   #endif
  496.  
  497.   WiFi.begin(ssid, password);
  498.   while (WiFi.status() != WL_CONNECTED) {
  499.     delay(500);
  500.   }
  501.   #ifdef DEBUG
  502.   Serial.println("connected to WiFi");
  503.   Serial.println("IP adddr: ");
  504.   Serial.println(WiFi.localIP());
  505.   #endif
  506.  
  507.   server = WiFiServer(serverPort);
  508.   server.begin();
  509.  
  510.   //CAN_cfg.speed=CAN_SPEED_500KBPS;
  511.   //CAN_cfg.tx_pin_id = GPIO_NUM_26;
  512.   //CAN_cfg.rx_pin_id = GPIO_NUM_5;
  513.   //CAN_cfg.rx_queue = xQueueCreate(10,sizeof(CAN_frame_t));
  514.  
  515.   CAN.setPins(26,32);
  516.   CAN.begin(500E3);
  517.  
  518.   #ifdef RGB_PIN
  519.   leds[0].setRGB(0,3,0);
  520.   FastLED.show();
  521.   #endif
  522.  
  523. }
  524.  
  525. void loop() {
  526.   // wait for client
  527.   client = server.available();
  528.   if (!client)
  529.   return;
  530.  
  531.   #ifdef DEBUG
  532.   Serial.println("Client Found");
  533.   #endif
  534.   #ifdef RGB_PIN
  535.   leds[0].setRGB(0,0,10);
  536.   FastLED.show();
  537.   #endif
  538.   while(CAN.available()){
  539.       CAN.read();
  540.   }
  541.   while (client.connected()) {
  542.     transfer_can2tty();
  543.     transfer_tty2can();
  544.     /*
  545.     int size = 0;
  546.     // read data from wifi client and send to serial
  547.     while ((size = client.available())) {
  548.       size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
  549.       client.read(buff, size);
  550.       Serial.write(buff, size);
  551.       Serial.flush();
  552.     }
  553.     // read data from serial and send to wifi client
  554.     while ((size = Serial.available())) {
  555.       size = (size >= BUFFER_SIZE ? BUFFER_SIZE : size);
  556.       Serial.readBytes(buff, size);
  557.       client.write(buff, size);
  558.       client.flush();
  559.     }
  560.     */
  561.   }
  562.   #ifdef DEBUG
  563.   Serial.println("Client Disconnected!");
  564.   #endif
  565.   #ifdef RGB_PIN
  566.   leds[0].setRGB(0,3,0);
  567.   FastLED.show();
  568.   #endif
  569.   client.stop();
  570. }
Add Comment
Please, Sign In to add comment