Page 1 of 2

[Solved] Reading character input from the serial port

Posted: Fri Sep 30, 2016 12:36 am
by kolban
I've reached the stage in a project where I would like to read serial data from the serial port. Given that printf() writes to the serial port, can I assume that getchar() will read from the serial port?

... later ...

It appears that as of this post date, the ability to read from UART through stdin is not yet supported. The following can be found:
C library functions which read from stdin are not connected to UART yet.
here:

https://github.com/espressif/esp-idf/releases (for release 0.9)

Re: Reading character input from the serial port

Posted: Fri Sep 30, 2016 7:23 pm
by martinayotte
Maybe we can use the ROM function in the mean time :

int uart_rx_one_char(uint8_t *ch);

Re: Reading character input from the serial port

Posted: Fri Sep 30, 2016 10:42 pm
by WiFive

Re: Reading character input from the serial port

Posted: Fri Sep 30, 2016 11:08 pm
by kolban
martinayotte wrote:Maybe we can use the ROM function in the mean time :

int uart_rx_one_char(uint8_t *ch);
That sure did look interesting, however the full description found in the uart.h found here reads:

https://github.com/espressif/esp-idf/bl ... rom/uart.h

Code: Select all

/**
  * @brief Get an input char from message channel.
  *        Please do not call this function in SDK.
  *
  * @param  uint8_t *pRxChar : the pointer to store the char.
  *
  * @return OK for successful.
  *         FAIL for failed.
  */
STATUS uart_rx_one_char(uint8_t *pRxChar);
The "Please do not call this function in SDK" causes me pause. I'm guessing that means that it is not meant to be called by normal "ESP32" applications.

Re: Reading character input from the serial port

Posted: Sat Oct 01, 2016 2:01 am
by ESP_Sprite
The problem with this function is that it uses an idle-loop, that is, it actively polls for a character to appear. That basically hangs up the rest of the SDK.

Re: Reading character input from the serial port

Posted: Tue Oct 04, 2016 4:30 am
by kolban
I'd reached the stage in my project where I couldn't go much further without being able to read from the serial input. Re-reading this forum and reading the comment from @martinayotte, I went back to the <rom/uart.h> and re-read. I decided to test a call to it in a loop just to see what happened. To my delight, it seems to "mechanically work".

Calling:

Code: Select all

    while(1) {
		 uint8_t myChar;
		 STATUS s = uart_rx_one_char(&myChar);
		 if (s == OK) {
			 printf("%c\n", myChar);
		 }
	 }
works just as I hoped it would. It also doesn't seem to block ... and returns FAIL if there is no character available. While I am cognizant of @ESP_Sprite's caution in that it is "polling" the serial input ... as opposed to being interrupt driven ... it still is enough to allow me to make functional progress at this time.

Re: [Solved] Reading character input from the serial port

Posted: Wed Oct 05, 2016 2:23 am
by jmattsson
As WiFive already mentioned (and linked to), in NodeMCU I've got the UART hooked up to stdio for input. It was surprisingly easy to do.

Re: [Solved] Reading character input from the serial port

Posted: Wed Oct 05, 2016 3:29 am
by WiFive
jmattsson wrote:As WiFive already mentioned (and linked to), in NodeMCU I've got the UART hooked up to stdio for input. It was surprisingly easy to do.
Yes, it is nice, except I am not sure why you are echoing the chars in the ISR (maybe just quick and dirty debug/test)?

Re: [Solved] Reading character input from the serial port

Posted: Wed Oct 05, 2016 3:42 am
by jmattsson
Whoops... indeed that was for debugging. Thanks for pointing it out - I had it on my todo list to work out why Esplorer wasn't happy loading the file list but now I know :)

Re: Reading character input from the serial port

Posted: Mon Feb 26, 2018 6:10 am
by flodis
kolban wrote:I'd reached the stage in my project where I couldn't go much further without being able to read from the serial input. Re-reading this forum and reading the comment from @martinayotte, I went back to the <rom/uart.h> and re-read. I decided to test a call to it in a loop just to see what happened. To my delight, it seems to "mechanically work".

Calling:

Code: Select all

    while(1) {
		 uint8_t myChar;
		 STATUS s = uart_rx_one_char(&myChar);
		 if (s == OK) {
			 printf("%c\n", myChar);
		 }
	 }
works just as I hoped it would. It also doesn't seem to block ... and returns FAIL if there is no character available. While I am cognizant of @ESP_Sprite's caution in that it is "polling" the serial input ... as opposed to being interrupt driven ... it still is enough to allow me to make functional progress at this time.
The classic fgetc() and fputc() from stdio.h also works on the USB serial before event sophistication level is reached.

Code: Select all

    while(1) {
		uint8_t ch;
	    ch = fgetc(stdin);
	    if (ch!=0xFF)
	    {
		    fputc(ch, stdout);
	    }
    }
:)