Serial UART Communication

Posted on
  • Hello Gordon,

    I have a few questions regarding serial communication.

    Serial1.setup is not working with parity 'none'. I got the following error:

    Serial1.setup(9600,{rx:B07,tx:B06,bytesi­ze:8,parity:'none',stopbits:1});
    INTERNAL ERROR: Unsupported serial parity mode.

    'even' works:

    Serial1.setup(9600,{rx:B07,tx:B06,bytesi­ze:8,parity:'even',stopbits:1});
    =undefined

    Is null also 'none' ?

    Second question. I'm a bit confused about the UART/USART pins. What is the difference between UART and USART ?

    On the pinout reference for example "USART1 TX" is labeled on pin B06 and A09.
    Thus this mean, that i can choose which pin i would like to use ?

    Many thanks

    Sacha

  • Ahh - thanks. I'll change that. For now, just do parity:0 (or leave it out). parity:null works for no parity too, as you suggest.

    UART/USART are actually because of the STM32 chip that's being used. A USART has a CK (clock) pin, and a UART doesn't. There's no support for the CK pin in Espruino right now, so you can actually safely assume that both UART and USART are the same.

    And with the pins - yes, on Espruino you've usually got a choice of which pin you put which peripheral on. However you can't put rx on one pin and tx on the other - both rx and tx have to be on pins 'next' to each other - eg . use B6+B7 or A9+A10, not B6+A10

  • Many thanks Gordon.

  • this is probably a basic question:
    how do I read from Serial1 when the data that arrives is not a letter or number but for example has the value 0x08 (BS)?
    I am interfacing to a gadget that sends out bytes not text and need to take action depending on the data received.

  • I would expect it to be returned as an escaped string (ie, "\x08") - does it not do that?

  • 8-)
    even if it did, I would have no clue what to do to capture that escaped string... I am trying to learn

  • Ok, I figured out a way. There must be more elegant ways to do this, but at least now I can see what I am receiving and I can take some actions depending on the messages.

    Serial1.setup(2400/*baud*/);
    Serial1.onData(function (e) { 
      var tr=(e.data);
      p=tr.charCodeAt(0);
      print(p);
      switch (p)
      {
        case 80 :
          var s = "CV,1.6A to 3A"; 
          print(s);
          break;
        case 96 :
          var s2 = "CV, < 1.6A"; 
          print(s2);
          break;
        case 85 :
          break;
        default :
          var s3="*";
          print(s3);
      }
    });
    
  • Using charCodeAt is the right way of doing it if you need a number...

  • Hello,
    I have a Espruino board v 1.3 and I'm using the SD card and Uarts 1 to 4 already but I still need to use one more uart. So my plan is to use uart5/serial5 but that shares a pin with the SD (CS) so my initial thought was to setup serial5 to different pins.

    So I tried to set up Serial5 to two of my available pins (B1 and B0) as
    Serial5.setup(9600,{rx:B1,tx:B0});

    but it does not seem to work.

    Do I need to recompile the firmware to assign serial5 to B1 and B0?

    Any info is appreciated.
    Thx

  • That's a lotta serials...

    The allowable pins for each peripheral are determined by the chip, not the software - the UARTs and SPI's and I2C's shown on the reference are bits of silicon in the chip, and can only be connected to certain pins.

    If you need 5 serials, I'm afraid your options are to either wait for Gordon to implement software serial (this emulates serial in software, working on any pins - he's talked about doing this in other thread: http://forum.espruino.com/conversations/­257119/?offset=25#comment11957278 ), or hack your Espruino board and get a wire onto D2. That would take out the SD card though (there are plans for a change to let you set the pins for SD card, but then you'd have to cut a trace and run another wire).

  • You do have another option, which is that if you don't need serial inputs at the same time, you can use Serial1.setup() to dynamically swap between A9/A10 and B6/B7 as you need it.

    Also, In the next few weeks I should be adding support for swapping the SD card to other pins. That means you could potentially cut the trace for D2 and then use another pin for the SD card...

    Finally you could always use an SPI/I2C USART chip, so you can add extra UARTs that way.

  • Thanks for both answers.
    Then will cut the trace for D2 so I can use it for UART5 RX

    When the new changes are done to assign the SD card pins, are there any restrictions what pin I could use for the SD CS, for example can I use B0 ?

    thx

  • When changing the SD card pins is done, you should be able to use any pin for CS. The only restriction would be on using a hardware SPI port (although that may change at some point too).

  • thank you for your explanation.

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

Serial UART Communication

Posted by Avatar for Sacha @Sacha

Actions