ruby (csv)

折角なので、受信したデータをcsvファイルへ書き出します。csv出力にはFasterCSVってのを使います。早いらしいです。ここからdownloadして、解凍後、ruby setup.rb とするとインスコできます。



先程のsourceを変更し、csvへの出力部分を追加します。CRコード受信時、writeするので受信文字はglobalに保存しておかなければいけません。csvファイルには、日時・受信文字列・フラグの順で出力します。フラグは後々、サーバへのデータ出力時に使用します。旨く行くと、以下の様なファイルが作成されます。



[recv.log]

2008-08-08 15:23:48,xxxxxxxxxxxxxxx,0

2008-08-08 15:23:53,xxxxxxxxxxxxxxx,0



以降の予定

TCP/IPにて、http経由でデータ送信のテスト

・thread処理で上記処理をバーコードリーダ処理ソフトに組み込む

・サーバへの通信エラー時のリトライ処理

いじょ。






[bcr.rb]
require "wincons"
require "wincom"
require 'fastercsv'

class BCR
@bcd

def initialize
@bcd = ""

@con1 = Console.new
@com5 = Serial.new
end

def Open
ir = @com5.open(5, 0x1807, 9600, 8, 0, 0, 256, 256)
return ir
end

def Close
@com5.close
end

def main
rc = @com5.receive
if rc != nil
for ia in 0..(rc.length - 1) do
if rc[ia].chr == "\r"
day = Time.now
printf("%s %s\r\n", day.strftime("%Y-%m-%d %H:%M:%S"), @bcd)

# CSV write
FasterCSV.open("recv.log", "a") { |csv|
csv << [day.strftime("%Y-%m-%d %H:%M:%S"), @bcd, '0']
}
@bcd = ""
else
@bcd += rc[ia].chr
end
end
end

ca, = @con1.keyin
if ca == "e" || ca == "E"
return 1
end
return nil
end
end

b = BCR.new
ic = b.Open
if ic != nil
print "open error"
end

while ic == nil
ic = b.main
sleep 0.01
end
b.Close



鎌倉観光 深谷霊園