# How to read/write to a serial port without a while(1)-loop?

**URL:** <https://forum.legato.io/t/how-to-read-write-to-a-serial-port-without-a-while-1-loop/2805>\
**Category:** Legato Application Framework\
**Created:** [November 21, 2017, 4:43pm UTC](https://forum.legato.io/t/how-to-read-write-to-a-serial-port-without-a-while-1-loop/2805 "2017-11-21T16:43:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![annaertd](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@annaertd](https://forum.legato.io/u/annaertd)\
**Post date:** [November 21, 2017, 4:43pm UTC](https://forum.legato.io/t/how-to-read-write-to-a-serial-port-without-a-while-1-loop/2805/1 "2017-11-21T16:43:56Z")

</div>

I use Legato 16.10.3 and my mangOH Green board has got an WP8548 module with firmware version SWI9X15Y\_07.12.09.00.  
The Legato docs indicate that a COMPONENT\_INIT should always return.

So, how can I read and write from a serial port without using an endless loop?  
Is there a Serial API with a callback function to handle received characters on a serial port?  
In fact, is there something similar to an ‘le\_data\_AddConnectionStateHandler’ for my DataConnection or an ‘IOT0\_GPIO1\_AddChangeEventHandler’ for my GPIO state changes?

My serial port is connected to ttyHSL1, and in my testapplication the read an write calls work fine. But, I’m not able to return from COMPONENT\_INIT, so, my application doesn’t get fully initialized.

> COMPONENT\_INIT  
> {  
> char uart2ReadBuffer[10] = “”;  
> int uart2ReadBufferLength = 0;  
> char uart2WriteBuffer[50] = “Write from UART2 : 123456789\r\n”;  
> int uart2;
> 
> ```
> LE_INFO("Hello, world.");
> 
> uart2 = open ("/dev/ttyHSL1", O_RDWR | O_NOCTTY | O_NDELAY );
> le_tty_SetRaw(uart2fd, 0, 0);	//ECHO is OFF
> le_tty_SetFraming(uart2, 'N', 8, 1);
> write(uart2, uart2WriteBuffer, strlen(uart2WriteBuffer));
> 
> while(1)
> {
> uart2ReadBufferLength = read(uart2, uart2ReadBuffer, 10 );
> 
> //check if we received a string on UART2
> if (uart2ReadBufferLength > 0)
> {
> LE_INFO("Received from UART2 : %s \n.", uart2ReadBuffer);
> }
> }
> 
> ```
> 
> }

---

<div class="post-metadata">

**Author:** ![TheGod](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/thegod/32/252_2.png) [@TheGod](https://forum.legato.io/u/TheGod)\
**Post date:** [November 21, 2017, 8:02pm UTC](https://forum.legato.io/t/how-to-read-write-to-a-serial-port-without-a-while-1-loop/2805/2 "2017-11-21T20:02:42Z")

</div>

Hi,  
Please check [le\_fdMonitor](http://legato.io/legato-docs/latest/le__fd_monitor_8h.html#a52902d634d810f9b7a23c53c9c5164f0)
