Windows Serial Port

Is there a way to access the serial port using Ruby and Windows? If so,
can someone point me to some examples?

Thanks,
Travis Whitton whitton@atlantic.net

p.s., I apologize if this gets duplicate posted. I’m having issues
with my news program.

Is there a way to access the serial port using Ruby and Windows? If so,
can someone point me to some examples?
Maybe this entry in the RAA helps.
http://www.ruby-lang.org/raa/list.rhtml?name=ruby-serialport

I have never tried it. One way that should work is go
via OLE. Search the mailinglist archive …s.b. posted
an example in February or March I think.

Good night,
-Armin

···

Armin Roehrl, http://www.approximity.com
We manage risk

require ‘Win32API’

NULL = 0
GENERIC_READ = 0x80000000
GENERIC_WRITE = 0x40000000
OPEN_EXISTING = 3

fCreateFile = Win32API.new(“kernel32”, “CreateFileA”, ‘PLLLLLL’, ‘L’)
fWriteFile = Win32API.new(“kernel32”, “WriteFile”, ‘LPLPL’, ‘L’)
fReadFile = Win32API.new(“kernel32”, “ReadFile”, ‘LPLPL’, ‘L’)

hCom = fCreateFile.call(“COM3”, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)

ptxBytes = [0].pack(“L”)
prxBytes = [0].pack(“L”)

txBuffer = “ATZ\r”
rxBuffer = " " * 256

nRet = fWriteFile.call(hCom, txBuffer, txBuffer.size, ptxBytes, NULL)
txBytes = ptxBytes.unpack(“L”)[0] #=> 4

nRet = fReadFile.call(hCom, rxBuffer, rxBuffer.size, prxBytes, NULL)
rxBytes = prxBytes.unpack(“L”)[0] #=> 10

rxBuffer[0…rxBytes] #=> “ATZ\r\r\nOK\r\n”

···

On Mon, 06 Jan 2003 20:51:47 GMT Travis Whitton whitton@atlantic.net wrote:

Is there a way to access the serial port using Ruby and Windows? If so,
can someone point me to some examples?


Shusaku tsyk@yk.rim.or.jp

nRet = fWriteFile.call(hCom, txBuffer, txBuffer.size, ptxBytes, NULL)
txBytes = ptxBytes.unpack(“L”)[0] #=> 4

The program hangs right here waiting for a read to happen. I know that
the write is successful because the SD light on my modem blinks. I have
to kill the cygwin window that ruby is running in in order to kill the program.

nRet = fReadFile.call(hCom, rxBuffer, rxBuffer.size, prxBytes, NULL)
rxBytes = prxBytes.unpack(“L”)[0] #=> 10

rxBuffer[0…rxBytes] #=> “ATZ\r\r\nOK\r\n”

Any ideas?

Thanks,
Travis Whitton whitton@atlantic.net

nRet = fWriteFile.call(hCom, txBuffer, txBuffer.size, ptxBytes, NULL)
txBytes = ptxBytes.unpack(“L”)[0] #=> 4

The program hangs right here waiting for a read to happen. I know that
the write is successful because the SD light on my modem blinks. I have
to kill the cygwin window that ruby is running in in order to kill the program.

I got more serious failure when I tried to reproduce your problem
using Cygwin 1.3.18.

irb(main):012:0* hCom = fCreateFile.call(“COM3”, … )
(irb):12: [BUG] Segmentation fault
ruby 1.6.8 (2002-12-24) [i686-cygwin]
Aborted (core dumped)

Any ideas?

Use mswin32 version of ruby. :slight_smile:
I had tested the script using “ruby 1.7.3 (2002-12-04) [i386-mswin32]”.

···

On Tue, 07 Jan 2003 16:57:34 GMT Travis Whitton whitton@atlantic.net wrote:


Shusaku tsyk@yk.rim.or.jp

The program hangs right here waiting for a read to happen. I know that
the write is successful because the SD light on my modem blinks. I have
to kill the cygwin window that ruby is running in in order to kill the program.

Hey, if you are using Cygwin & Cygwin compiled ruby try using the serial
port package that Armin mentioned:

http://www.ruby-lang.org/raa/list.rhtml?name=ruby-serialport

I’ve used it at work a few times now and it works like a charm

Rob