[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Reading from $stdin

Adrian Roskrow

1/17/2007 1:56:00 PM

Hi

I need to read from a usb connected barcode reader and I thought ruby
would be a good way to do it. I have written a quick program to read
from the $stdin object but it just sits there and waits. Im very new to
ruby so I have probably made a fundamental mistake. Can anyone see what
I have done, or can anyone point me in the right direction or provide an
example?

Below is a simple bit of code

#!/usr/bin/env ruby
text = $stdin.read
lines = text.split("\n")
i = 1
for line in lines do
puts "#{i}. " + line
i += 1
end

Adrian

--
Posted via http://www.ruby-....

9 Answers

Robert Klemme

1/17/2007 2:19:00 PM

0

On 17.01.2007 14:56, Adrian Roskrow wrote:
> I need to read from a usb connected barcode reader and I thought ruby
> would be a good way to do it. I have written a quick program to read
> from the $stdin object but it just sits there and waits. Im very new to
> ruby so I have probably made a fundamental mistake. Can anyone see what
> I have done, or can anyone point me in the right direction or provide an
> example?

Do you actually redirect stdin to read from that device?

> Below is a simple bit of code
>
> #!/usr/bin/env ruby
> text = $stdin.read
> lines = text.split("\n")
> i = 1
> for line in lines do
> puts "#{i}. " + line
> i += 1
> end

I'd rather do

$stdin.each do |line|
printf "%4d %s", $stdin.lineno, line
end

or

$stdin.each_with_index do |line, index|
printf "%4d %s", index, line
end

HTH

Kind regards

robert

Ara.T.Howard

1/17/2007 3:26:00 PM

0

Adrian Roskrow

1/17/2007 7:54:00 PM

0

Robert Klemme wrote:
> On 17.01.2007 14:56, Adrian Roskrow wrote:
>> I need to read from a usb connected barcode reader and I thought ruby
>> would be a good way to do it. I have written a quick program to read
>> from the $stdin object but it just sits there and waits. Im very new to
>> ruby so I have probably made a fundamental mistake. Can anyone see what
>> I have done, or can anyone point me in the right direction or provide an
>> example?
>
> Do you actually redirect stdin to read from that device?
>
>> Below is a simple bit of code
>>
>> #!/usr/bin/env ruby
>> text = $stdin.read
>> lines = text.split("\n")
>> i = 1
>> for line in lines do
>> puts "#{i}. " + line
>> i += 1
>> end
>
> I'd rather do
>
> $stdin.each do |line|
> printf "%4d %s", $stdin.lineno, line
> end
>
> or
>
> $stdin.each_with_index do |line, index|
> printf "%4d %s", index, line
> end
>
> HTH
>
> Kind regards
>
> robert

Hi

In answer to your question, no I don't redirct stdin, should I?

I like your code snippets and will try them out post hast.

Thanks

Adrian


--
Posted via http://www.ruby-....

Adrian Roskrow

1/17/2007 8:01:00 PM

0

unknown wrote:
> On Wed, 17 Jan 2007, Adrian Roskrow wrote:
>
>> Hi
>>
>> I need to read from a usb connected barcode reader and I thought ruby
>> would be a good way to do it. I have written a quick program to read
>> from the $stdin object but it just sits there and waits.


Hmmm I need to read and digest this. Thankyou
>
> in any case it looks like you're on the right track - welcome to ruby!
>
> -a


Hmmm I need to read and digest what you have said Thankyou


Hmmm I need to read and digest this. Thankyou

--
Posted via http://www.ruby-....

Robert Klemme

1/17/2007 9:54:00 PM

0

On 17.01.2007 20:53, Adrian Roskrow wrote:
> In answer to your question, no I don't redirct stdin, should I?

Well, if you don't the script will sit there and wait for you to enter
something via keyboard. And you won't see any output from your version
of the script until you press Ctrl-Z or whatever is the stream
termination sequence on your operating system because the #read call
reads all the way to the end of the stream. So, as Ara said, as long as
there is no end #read cannot return and you don't see any output. The
story is different with a line based implementation like mine.

> I like your code snippets and will try them out post hast.

Have fun!

robert

Adrian Roskrow

1/18/2007 11:33:00 AM

0

Well I have written a small script to read from stdin and guess what, it
just sits and does nothing!! I guess I must of got it wrong! Here is
small snip-it
@number = $stdin.each_with_index do |line, index|
printf "%4d %s", index, line
end

which is just the same as roberts code. I have a question, the bar code
reader is connected to a usb port, do I still use stdin (or is this just
for the keyboard?)

how do I redirect the input to my code?

I'm sorry to be asking basic questions but I am new to ruby.

Adrian

Robert Klemme wrote:
> On 17.01.2007 20:53, Adrian Roskrow wrote:
>> In answer to your question, no I don't redirect stdin, should I?
>
> Well, if you don't the script will sit there and wait for you to enter
> something via keyboard. And you won't see any output from your version
> of the script until you press Ctrl-Z or whatever is the stream
> termination sequence on your operating system because the #read call
> reads all the way to the end of the stream. So, as Ara said, as long as
> there is no end #read cannot return and you don't see any output. The
> story is different with a line based implementation like mine.
>
>> I like your code snippets and will try them out post hast.
>
> Have fun!
>
> robert

--
Posted via http://www.ruby-....

Martin Portman

1/18/2007 12:31:00 PM

0

Adrian Roskrow wrote:
> Well I have written a small script to read from stdin and guess what, it
> just sits and does nothing!! I guess I must of got it wrong! Here is
> small snip-it
> @number = $stdin.each_with_index do |line, index|
> printf "%4d %s", index, line
> end
>

each_with index reads one line at a time. A line needs to be
terminated with an end of line character(s) before it can be read.

Does your barcode reader insert end of line characters(s) at the end
of the content of the barcode?

I've used a usb barcode reader (in windows xp) and it does insert
\r\n on the end of each sucessful read. Try the reader out in
a text editor to see if it does. If it doesn't, you'll need to
adopt a different approach than reading lines. Alternatively
in the barcode driver, there may be an option to force it to emit
end of line characters.

If the barcode reader inserts chars into the editor, just like a keyboard,
you shouldn't need to redirect the input at all. It should
appear on $stdin.

Martin.

Adrian Roskrow

1/18/2007 7:51:00 PM

0

Hi

I must be really stupid but I don't understand this at all, How does the
usb scanner connect, to a text editor. I don't know how to do that. I
understand how the scanner reads one line at a time, the end of the line
being \r\n. What prompts the scanner to read another line, does the \r
or \n do it? Barcode reader driver? there is no driver, have I got to
write one?

Adrian

Martin Portman wrote:
> Adrian Roskrow wrote:
>> Well I have written a small script to read from stdin and guess what, it
>> just sits and does nothing!! I guess I must of got it wrong! Here is
>> small snip-it
>> @number = $stdin.each_with_index do |line, index|
>> printf "%4d %s", index, line
>> end
>>
>
> each_with index reads one line at a time. A line needs to be
> terminated with an end of line character(s) before it can be read.
>
> Does your barcode reader insert end of line characters(s) at the end
> of the content of the barcode?
>
> I've used a usb barcode reader (in windows xp) and it does insert
> \r\n on the end of each sucessful read. Try the reader out in
> a text editor to see if it does. If it doesn't, you'll need to
> adopt a different approach than reading lines. Alternatively
> in the barcode driver, there may be an option to force it to emit
> end of line characters.
>
> If the barcode reader inserts chars into the editor, just like a
> keyboard,
> you shouldn't need to redirect the input at all. It should
> appear on $stdin.
>
> Martin.


--
Posted via http://www.ruby-....

Marcel Ward

1/18/2007 10:00:00 PM

0

On 18/01/07, Adrian Roskrow <adrian@hang10systems.co.uk> wrote:
> Hi
>
> I must be really stupid but I don't understand this at all, How does the
> usb scanner connect, to a text editor. I don't know how to do that. I

Hi Adrian,

[what follows has nothing to do with Ruby; it concerns device setup]

What you are talking about is in industry terms a "keyboard stuffer"
device (also sometimes referred to as a "keyboard wedge" device).
This is basically a device which injects characters into the input
buffer stream. In the case of a USB device, it does this through
software drivers. Either these drivers are installed automatically
(e.g. on a plug-and-play O/S) or you must choose to install device
drivers manually for your O/S.

This is not the only way such a device may provide you with input
data; for example, some wedge devices require that you access the
incoming data by means of special driver interfaces and many permit
two-way communication with the device. These kind of devices often do
not come configured as keyboard stuffers by default.

The easiest way to see if you have a keyboard stuffer is simply to
plug it in, run up your favourite text editor and scan a barcode.
Once it beeps to indicate successful read, you should see characters
appear on your editor window. If nothing appears then your device is
probably NOT configured as a keyboard stuffer.

So, first things first, before you even start to run up Ruby, are you
seeing digits on-screen when you do a valid scan?

If so, does each barcode you scan appear as a sequence of digits on a
separate line or do all the digits join together on the same line? If
the latter then you will have to consult your device's manual to see
how the terminating character can be set. Usually this is a simple
matter of scanning the relevant setup barcodes from the manual pages.

If you do not see anything at this stage then you won't see anything
with $stdin either. Then it's time to seek out the device manual to
see how to put it in keyboard stuffing mode.

Hope that helps...

--
Marcel