Reading input from a scanner using serial port in Python
I'm working on reading input from a scanner but I want to ask a question first then read the incoming data.
This is what I have now.
ser = serial.Serial()
ser.baudrate = 9600
PORT = 1
#for windows
ser = serial.Serial(PORT, 19200, timeout=1)
x = ser.read()
s = ser.read(11)
line = ser.readline()
#Read input
station = int(raw_input('What is the barcode? '))
Can someone point me in the right direction.
UPDATE:开发者_JAVA百科 The input device is a barcode scanner. The ouput device is a console window.
I would like the output to be like this:
What is the barcode?
(would wait for scan....)
(Once scanned it would show up on the screen and stored in a variable)
This is the barcode scanned: AAA00000011
I'm also using the example on this page
The link in your post is broken, and you don't mention what kind of serial device you're using, it's hard to know what you want to do.
I don't think raw_input will do what you want, as the way you are using it in the above code, it would use standard out and standard in, and I'm assuming you want to print a question and then take input from your serial connection.
If that is indeed what you want to do, then you'll want to write to whereever you'd like to display your question, perhaps ser.write, and then execute your read code.
Edit: sourceforge is back up. the read() method is blocking, according to the docs, so you'll just print your question to standard output (probably the screen), and then call "read()". your program should wait (block) until it receives the correct number of bytes, in your case, 11. I am assuming that the code you posted for reading the bytes is actually giving you what you want.
print "What is the barcode?"
code = ser.read(11)
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).
If you know the serial device will be transmitting \n
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 this that's either going to be 6 or 10 bytes for data and the device datasheet will specify for errors.
You'll want something like this (COM1 assumes Windows, use /dev/ttyS* for Unix:
ser = serial.Serial('COM1', 19200, timeout=1)
station = raw_input('What is the station?')
barcode = ser.read(10)
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.
print 'What is the barcode?'
ser = serial.Serial('COM1', 19200, timeout=1)
barcode = ser.read(11)
print 'This is the barcode scanned: ' + barcode
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.
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.
精彩评论