2012年6月27日水曜日

Ruby1.9.3[i386-mingw32] + wincom RS-232Cターミナル


TEXCELL Rubyシリアル通信ライブラリ(Windows)を使用して作ってみた。
SD-RDLoopbackでテストした。

C:\rubyapi\ci-v>ruby -v
ruby 1.9.3p194 (2012-04-20) [i386-mingw32]

C:\rubyapi\ci-v>type wincom_s2_20120622.rb
# -*- coding: shift_jis -*-
require "./wincom"      #ruby1.9.3 only
require 'dl/import'

module MSVCRT           #ruby-list:42280 kara
    extend DL::Importer
    dlload "MSVCRT.DLL"
    extern "int _kbhit()"
    extern "int _getch()"
end

class C24com

  def initialize
    @comp1 = Serial.new
  end

  def open
    @comp1.comno = 1
    @comp1.bRate = CBR_115200
    @comp1.byteSize = 8
    @comp1.parity = NOPARITY
    @comp1.fOutxCtsFlow = 0
    @comp1.fOutxDsrFlow = 0
        @comp1.fDsrSensitivity = 0
#    @comp1.fDtrControl = DTR_CONTROL_DISABLE
    ir = @comp1.open
    if ir == nil
       ir = @comp1.escapeCommFunc(SETDTR)
    end
    return ir
  end

  def rclose
     @comp1.close
  end

  def receive
    sr = @comp1.receive
    if sr != nil
       sr.each_byte{ |c|
         ic = c.to_i
       }
    end
  end

  def send(schar)
        @comp1.send(schar)
  end

end

c24com1 = C24com.new
ic = c24com1.open
if ic != nil
  print "openエラー " + ic.to_s
end

loop do
        rx_data = c24com1.receive
        unless rx_data.nil?
                print rx_data
                if rx_data == "\r"
                        print "\n"
                end
        end

        if MSVCRT._kbhit != 0
                ch = MSVCRT._getch
            c24com1.send(ch.chr)
        end
  sleep 0.01
end

sp.close

C:\rubyapi\ci-v>ruby wincom_s2_20120622.rb
01234567890
abcdefghijklmnopqrstuvwxyz

0 件のコメント:

コメントを投稿