user1870619
user1870619

Reputation: 177

linux serial ports -- mulithread program

I am working on a smartcard reader project here i will have to read/write data from the smartcard reader. Also i will have to read/write data from PC application.

There are two serial port on my microcontroller one connected to smartcard reader other to PC.

Smartcard reader <------> Microcontroller <-----> PC

I have ported linux & using /ttys0 & /ttys1 driver for this.

1> My question is if application have to find that some data is available to be read from the port than will i have to always check it with read() system call ?
2> Does ttys0 driver have internal buffer to store received data ? Or data is lost if application do not read data immediately ?
3> Here using seprate threads for rx/tx from each port, is it right approach ?

Please guide me i am new to Embedded linux.

//John

Upvotes: 0

Views: 327

Answers (2)

Chris Stratton
Chris Stratton

Reputation: 40347

Yes, there's a fair amount of buffering on linux tty's.

You have a few choices for how to interact with them.

  • you can make them non-blocking, and frequently poll to see if you can read data from them (but this may result in uselessly spinning CPU cycles, slowing other tasks)

  • you can use select() to yield to the scheduler until one of your devices has data for you to act on

  • you can use blocking I/O, however since you have multiple ports that may also require multiple threads

Upvotes: 1

stdcall
stdcall

Reputation: 28870

TTY programming is similar to socket programming in Linux. So basically you can set the socket to be a asynchronous and receive a signal once data is available. Regarding buffering, yes it's buffered using two flipping buffers. You can check chapter 18 in Linux device drivers 3rd edition regarding TTY implementation in the kernel.

Upvotes: 0

Related Questions