[Solved] Reading character input from the serial port

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

[Solved] Reading character input from the serial port

Postby kolban » Fri Sep 30, 2016 12:36 am

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)
Last edited by kolban on Tue Oct 04, 2016 4:31 am, edited 2 times in total.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
martinayotte
Posts: 141
Joined: Fri Nov 13, 2015 4:27 pm

Re: Reading character input from the serial port

Postby martinayotte » Fri Sep 30, 2016 7:23 pm

Maybe we can use the ROM function in the mean time :

int uart_rx_one_char(uint8_t *ch);

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Reading character input from the serial port

Postby WiFive » Fri Sep 30, 2016 10:42 pm


User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Reading character input from the serial port

Postby kolban » Fri Sep 30, 2016 11:08 pm

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.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: Reading character input from the serial port

Postby ESP_Sprite » Sat Oct 01, 2016 2:01 am

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.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Reading character input from the serial port

Postby kolban » Tue Oct 04, 2016 4:30 am

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.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

jmattsson
Posts: 38
Joined: Fri Jun 03, 2016 5:37 am
Contact:

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

Postby jmattsson » Wed Oct 05, 2016 2:23 am

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.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

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

Postby WiFive » Wed Oct 05, 2016 3:29 am

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)?

jmattsson
Posts: 38
Joined: Fri Jun 03, 2016 5:37 am
Contact:

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

Postby jmattsson » Wed Oct 05, 2016 3:42 am

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 :)

flodis
Posts: 13
Joined: Mon Feb 26, 2018 5:09 am

Re: Reading character input from the serial port

Postby flodis » Mon Feb 26, 2018 6:10 am

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);
	    }
    }
:)

Who is online

Users browsing this forum: cdollar and 171 guests