Does anyone know how to read data from a serial port (RS232)?
A tried to use open and read commands.
It seems that the open command worked well, but the read command always returns -1.
This is how I used them:
fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NONBLOCK);
I'm trying to test this with a serial barcode scanner.
If there is anyone who can help me, I'd be very grateful.
It depends what type of operating system environment that your program is running on.
If you are using windows then look for microsoft doc on the WIN32 comm API. A better method would be to buy a 3rd party communication package that know the intricies of the comm API such as the Sysfire/Greenleaf comm libraries.
I'm sorry that I can't supply you with info on serial comms on other operating systems.
Well unless you have already scanned something, your read call will return immediately, whether there is any data or not.
Either remove the NON_BLOCK and let read just wait until there's something on the port, or surround the read with a loop to continually monitor for possible input.