Usage of PtAppAddFd

Hello all!
Went to the help page and looked for “PtAppAddFd” but still unsure of how to use it. Can anyone give a code example demonstrating its usage?

Thanks in advance.

Take a look at the select() function. Generally you use select when you want to block waiting for fd to have either something to read, or room to write to. It allows you to block on multiple file descriptors at the same time.

The problem with a Photon app is that you may want to block on a fd, while still responding to Photon events.

Let’s say you have a Photon app, which displays data from a serial port. You also have a cancel or quit button. In this case, you would open the serial port for reading, and call PtAppAddFd with the fd and a function which will get called when there is something to read from the serial port. Think of it as adding a callback to be called when something changes with the file descriptor.

Hope this helps,

Rick…

Hello Rick!
Recently I designed a phab GUI.When a button is pressed,the serial port opens and whatever is being typed into the hyperterminal of a computer will be displayed on the hyperterminal’s window and when letter q is typed, the serial port closes.

Now to display the letters typed onto hyperterminal onto the GUI, I would have to use “PtAppAddFd”.
Then do I just insert the command “PtAppAddFd(PtAppContext_t app, int fd, unsigned Pt_FD_READ, PtFdProc_t fun, void *data)” into my program source code and then it would automatically read the input from the other computer? Do I still need to do anything else besides that like attaching callbacks or anything?

Also I think that my synopsis of the PtAppAddFd(PtAppContext_t app, int fd, unsigned Pt_FD_READ, PtFdProc_t fun, void *data) is wrong but I do not know which part of it is wrong. Can you please guide me?
Thanks in advance!

You have to write the function ‘fun’ and when it is called, it means there is data to read from the serial port. At that point, you read the data, insert it into the widget which you are using to display it, then return.

Make sure your serial port is set up correctly and it set for non-blocking reads. You don’t want to have 1 char in the serial port and block waiting for 10. This would cause your gui to lock up (or appear to).

So the sequence is something like this:

open serial port
setup serial port
call PtAppAddFd()
continue with the app.

when there is data to read from the serial port, fun() will be called:

read serial port
set data into widget
continue

Hope this helps,
Rick…