Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The question seems to be badly worded. You can use Python's raw_input function to read from standard input to ask a question, and you can read bytes from the serial port if you know the BAUD rate the device is communicating in (otherwise you'll get junk). From the example I'm not sure exactly what you're trying to do though (and the code seems to show you asking a question after reading a value, contrary to the description).</p> <p>If you know the serial device will be transmitting <code>\n</code> when it's finished then you can use readline(), otherwise you need to know how many bytes its response will be. It looks like it's a barcode scanner, so make sure that the number of bytes you request will cover any error codes it might print. If you read too many bytes, you'll cut off data from the buffer for the next read, and if you read too few you'll leave some in there. According to <a href="http://www.adams1.com/upccode.html" rel="nofollow">this</a> that's either going to be 6 or 10 bytes for data and the device datasheet will specify for errors.</p> <p>You'll want something like this (COM1 assumes Windows, use /dev/ttyS* for Unix:</p> <pre><code>ser = serial.Serial('COM1', 19200, timeout=1) station = raw_input('What is the station?') barcode = ser.read(10) </code></pre> <p>Edit: Like Paul said, the read() call is a blocking call and will wait until there is serial data available in the buffer, until the timeout period specified in the constructor is reached. This means that you will need to scan something in before the timeout period expires or the code will break. You will either get part of the scanned results or an empty string.</p> <pre><code>print 'What is the barcode?' ser = serial.Serial('COM1', 19200, timeout=1) barcode = ser.read(11) print 'This is the barcode scanned: ' + barcode </code></pre> <p>Note that I've used 11 bytes here as that was what you originally used, I assume your barcode scanner's data sheet says it prints 10 characters and then an end byte such as \n.</p> <p>The info above about any error codes that the scanner might send still stands and you'll need to handle that yourself before printing out the barcode.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload