Stephan Samuelson <SamuelsS@kci1.com> wrote:
Does anyone know the code I would use to read data from the serial port? I
have written a Photon program that acts as a master looking for a specific
device on a serial buss. When the device recognizes the masters address, it
sends back several bytes of data. I have the write working, and my hardware
is communicating, but the read() function does not seem to work.
Thank you for any help and example code in advance.
Normally, code to read from a serial port would look like:
fd = open("/dev/ser1", …)
read(fd, buf, bytes);
But, a serial port is a terminal device – this means it may expect to be
in line-mode, rather than character mode and other stuff like this.
Take a look at “stty -a </dev/ser1” on the place you want to read – this
will list all the flags. In particular, look for “icanon” – to get
characters directly, you probably have to do a “stty -icanon < /dev/ser1”
before using it. Also check for flow-control bits – if you don’t have
the serial port flow-control hooked up, you may have to disable hardware
flow control. To set this sort of stuff from a C program, look at the
tcgetattr()/tcsetattr() functions, and the tcflow() function.
(Short cut for stty is to do “stty +raw < /dev/ser1” to get it into raw,
that is unprocessed mode.)
For more precise control of reading from a serial port, look at the docs
for dev_read() – this allows you to specify things like min number of
bytes to wait for, inter-byte timeouts before returning, etc.
Hope this helps.
QNX Training Services