I want to communicate with arduino from the PC(linux) and that i've been able to accomplish with a program in c++, the thing is, i want to "tell" the arduino when to read as in the same way i would like to "tell" the pc when to read. I dont want any loops to see when theres data available ( in c++ i 'm not even sure if i could do that, at least using files to read/write COM/ttyUSB as it's my case).
Do you tihnk a virtual COM port and a USB/RS232 converter would sovle my problem, as i would be able to use RTS(ready to send) and CTS(clear to send) pins to control the communication flow?
Using POSIX serial communications, you can just set up buffers and read from them in a loop (I'm not sure how you're running a program without a loop - it'd exit after a single run, no opportunity for user interaction). You shouldn't be using the Serial port as a file - while workable in the short term, POSIX does define a serial port interface that is actually designed to be used that way =). Look into usage of termios.h
Unfortunately, the Arduino does NOT support hardware flow control - the FTDI IC and 8U2 firmware actually support RTS and CTS, but RTS is used to reset the Arduino for programming, and is not suitable for actual communications. Set flow control to NONE instead.
Thanks for the reply, your answer was of great value to me.
I was in doubt wether arduino would support or not rts and cts.
My programs do have a loop, but as i'm already near arduino processing capacity limit ( at least i think i am) because i'm reading motors encoders atached to interrupts, i would like to make the reading serial another interrupt in order to remove that part of the code(although i'm not sure if that would optimize it...).
On the other hand i only need to send data from time to time from the PC in very well defined time and wait for response (when a button is clicked on my c++ application).
i think i wouldnt need to check all the time if any data is available and besides i dont even know how to do that using files (as you said not the best solution). I've used before rxtx and java but in c++ i dont have any idea how to do it, i'll read about termios as u said. I dont want to get advantage of ur good will but if by chance u have an example laying around that u could send me i would apreciate.
Best regards,
Braga
i'm sorry, i think i was already using termios.h :
reading termios.h docs i acknowledge FD_ISSET function but when i put it un my code it gives an error (2 parameters needed) while in the docs only one is used...
As long as you don't close the serial port (/dev/ttyUSB0), it should be buffering the received messages - processing it in a loop is the most time-efficient and cpu-efficient method. As for the Arduino, the encoder interrupts will interrupt the Serial loop, so there's no processing overhead there. Serial read/write is already an interrupt - it's just processing the received data from the buffer that is a loop. So you're not really optimizing anything there. Also realize that the Arduino runs something in the vicinity of 100 loops in the time it takes a character to be sent via serial - you will want to check continuously to see if the packet is received, thus ending up in a loop =).
You can just run the loop after you've sent the serial command (while (receivedChar != terminationChar); or something similar)). That way you only spin while expected data to come in.
An excellent implementation that I've been playing with recently (in conjunction with ROS) is the CerealPort class, which you can adapt to your needs (look at the source code xD).
Thanks for your time and putting some light in concepts that were a bit confused in my mind.
Aeturnalus:
As long as you don't close the serial port (/dev/ttyUSB0), it should be buffering the received messages - processing it in a loop is the most time-efficient and cpu-efficient method. in.
I didnt know this, but it simplifies it a lot.
Aeturnalus:
An excellent implementation that I've been playing with recently (in conjunction with ROS) is the CerealPort class, which you can adapt to your needs (look at the source code xD).