Serial Port Programming Part 4 - tcdrain - example, internal implementation
The tcdrain will block the process until all the data which is present in the Linux TTY buffer is written to the hardware. tcdrain is a C Library function which translates it to ioctl with command argument set to 'TCSBRK'
Let's see how tcdrain is internally implemented in kernel:
'tty_ioctl' function present in drivers/tty/tty_io.c handles all the ioctl calls from Serial Port
You can see from the above screenshot, it calls tty_wait_until_sent function, it is defined in drivers/tty/tty_ioctl.c file
Let's see how tcdrain is internally implemented in kernel:
'tty_ioctl' function present in drivers/tty/tty_io.c handles all the ioctl calls from Serial Port
You can see from the above screenshot, it calls tty_wait_until_sent function, it is defined in drivers/tty/tty_ioctl.c file
tty_wait_until_sent uses wait_event_interruptible_timeout until the tty_chars_in_buffer function returns 0. tty_chars_in_buffer returns the number of bytes present in the device private output queue. This is implemented by the particular serial port device driver. If we use 'pl2303' device, then it will be in the pl2303 usb serial port driver.
Let's write a C Code to understand better. Connect two serial port devices TX and RX.
Below code accepts number of bytes to transmit from user, fills 'A' in it and transmit it. It then calls tcdrain, captures the timestamp before and after tcdrain, and displays the difference in time
Code:
Output:
You can see as the number of bytes increased, it took a longer time to complete tcdrain system call.
Comments
Post a Comment