Serial1.write disconnect USB console

Posted on
  • Hello !
    I'm trying to control a SerialMP3 board but each time I send a command my espruino WiFi is disconnected from the USB console

    var id = 1;
    Serial1.setup(9600);
    Serial1.write([0x7E,0xFF,0x06,0x03,0x00,­0x00,id,0xEF]);
    

    Any idea ?

  • Can you try Serial1.setup(9600,{tx: tx pin, rx: rx pin});? When you don't specify any pins it defaults to using a standard set of pins of each UART, but on the WiFi those might happen to overlap with USB.

  • Amazing :)
    That totaly solved the issue, thanks !

    var id = 1;
    Serial1.setup(9600, { tx: B6, rx: B7});
    Serial1.write([0x7E,0xFF,0x06,0x03,0x00,­0x00,id,0xEF]);
    
  • Thanks! I'll try and change it so the next firmware doesn't automatically default to those :)

    Just so you know, if you use Serial1 then when you disconnect USB, Espruino's console will move over to Serial1 automatically and may end up sending extra characters that you didn't intend. It's an idea to stick USB.setConsole(1) in onInit to force the console to stay on USB so you can avoid that and use Serial1 properly regardless of whether the WiFi is connected to USB or not.

  • Thanks again.
    In fact I Added a"SoftwareSerial" TX function so that I can control several devices with one espruino . And it works amazingly well !

      function softwareSerialTX(pin, arr) { // 9600, 8N1
        var baud = 1000/9600;
        let i, j, len;
        let binArr = [];
    
        len = arr.length;
        for (i = 0; i < len; i++) {
          binArr.push(0);
    
          for(j = 1; j < 256; j *= 2) {
            binArr.push((arr[i] & j) ? 1 : 0);
          }
    
          binArr.push(1);
        }
        binArr.push(0);
    
        let pulseArr = [];
        let start = binArr[0];
        let v = start;
        let cnt = 1;
    
        len = binArr.length;
        for (i = 1; i < len; i++) {
          if (binArr[i] == v) {
            cnt++;
          } else {
            v = binArr[i];
            pulseArr.push(cnt*baud);
            cnt = 1;
          }
        }
        if (cnt) {
          pulseArr.push(cnt*baud);
        }
    
    //console.log(arr);
    //console.log(binArr);
    //console.log(pin);
    //console.log(start);
    //console.log(pulseArr);
    
        digitalPulse(pin, start, pulseArr);
      }
    
  • Nice - thanks for posting that up! It's something I'd really like to get included in the firmware at some point - it should even be possible to receive data as well (it's more tricky doing it in JS because of execution speed, but if it were in the firmware it should work).

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Serial1.write disconnect USB console

Posted by Avatar for SamaelS. @SamaelS.

Actions