M
M Griffin
You will need the Pyserial serial port library. If you are using Linux, it is usually in your repository, so you can install it from your package manager. (The Debian package name is python-serial, which of course will be the same for Ubuntu).
If you are using MS Windows, you can download it from here:
http://sourceforge.net/projects/pyserial/
The Linux version may work on Mac OS/X, but I don't anything about that.
Documentation is here:
http://pyserial.sourceforge.net/
The documentation is very good, so make sure to have a close look at it.
A brief summary though is you need to do the following:
First, configure the serial port. See the examples for that. The details of the configuration will depend on what you are trying to do.
Next, you read the serial port. How you do that depends on whether you are doing a "blocking" or "non-blocking" read. A blocking read will wait until it reads the specified number of bytes. A non-blocking read will just read what is currently available in the buffer. You make it blocking or non-blocking by how you set the time-out value (no time-out waits forever, a specified time-out will stop waiting after the set time).
If you are writing a simple command line program that just waits for input, then prints it and exits, then you may want to use blocking reads.
If you are writing a GUI program, then you want to use non-blocking reads and use a timer to "wake up" the program on regular intervals to check the serial port. When you are using non-blocking reads you need to check if the data is complete so you can if necessary concatenate the reply fragments until you accumulate a complete reply. The best way to do this depends on what the data is supposed to look like.
If you have more questions, then provide some more detail of what you are trying to do so I can direct the answers more precisely. It's actually pretty easy, but the details will depend on things like:
1) what the data looks like (fixed length, variable length, binary, ASCII, known delimiter or not, etc.),
2) how the overall program is supposed to work (simple script, large GUI program, log to a file, etc.).
If you are using MS Windows, you can download it from here:
http://sourceforge.net/projects/pyserial/
The Linux version may work on Mac OS/X, but I don't anything about that.
Documentation is here:
http://pyserial.sourceforge.net/
The documentation is very good, so make sure to have a close look at it.
A brief summary though is you need to do the following:
First, configure the serial port. See the examples for that. The details of the configuration will depend on what you are trying to do.
Next, you read the serial port. How you do that depends on whether you are doing a "blocking" or "non-blocking" read. A blocking read will wait until it reads the specified number of bytes. A non-blocking read will just read what is currently available in the buffer. You make it blocking or non-blocking by how you set the time-out value (no time-out waits forever, a specified time-out will stop waiting after the set time).
If you are writing a simple command line program that just waits for input, then prints it and exits, then you may want to use blocking reads.
If you are writing a GUI program, then you want to use non-blocking reads and use a timer to "wake up" the program on regular intervals to check the serial port. When you are using non-blocking reads you need to check if the data is complete so you can if necessary concatenate the reply fragments until you accumulate a complete reply. The best way to do this depends on what the data is supposed to look like.
If you have more questions, then provide some more detail of what you are trying to do so I can direct the answers more precisely. It's actually pretty easy, but the details will depend on things like:
1) what the data looks like (fixed length, variable length, binary, ASCII, known delimiter or not, etc.),
2) how the overall program is supposed to work (simple script, large GUI program, log to a file, etc.).