LoginSignup
4
3

More than 5 years have passed since last update.

ネットワーク接続されたプリンタの名前をmrubyで取得するプログラム

Last updated at Posted at 2014-04-16

島ぶくろ ネットワーク接続されたプリンタのステータスを取得するプログラムを受けて、mrubyで簡単に実装してみました。

実用性ゼロの動作確認レベルです。

1. mrubyをダウンロードする

$ git clone https://github.com/mruby/mruby.git

2. ビルド設定を編集する

build_config.rb
MRuby::Build.new do |conf|
  toolchain :clang # :gcc, :visualcpp 

  conf.bins = %w(mrbc)

  # mruby's Core GEMs
  conf.gem 'mrbgems/mruby-bin-mirb'
  conf.gem 'mrbgems/mruby-bin-mruby'
  conf.gem 'mrbgems/mruby-print'

  # user-defined GEMs
  conf.gem :git => 'https://github.com/iij/mruby-io.git'
  conf.gem :git => 'https://github.com/iij/mruby-mtest.git'
  conf.gem :git => 'https://github.com/iij/mruby-socket.git'
  conf.gem :git => 'https://github.com/iij/mruby-pack.git'
end

3. ビルドする

$ rake
CC    tools/mrbc/mrbc.c -> build/host/tools/mrbc/mrbc.o
CC    src/array.c -> build/host/src/array.o
CC    src/backtrace.c -> build/host/src/backtrace.o
... 中略 ...
Build summary:

================================================
      Config Name: host
 Output Directory: build/host
         Binaries: mrbc
    Included Gems:
             mruby-bin-mirb - mirb command
               - Binaries: mirb
             mruby-bin-mruby - mruby command
               - Binaries: mruby
             mruby-io
             mruby-socket
             mruby-pack
================================================

4. mrubyのスクリプトを作成する

sysDescr.rb
sendMsg = [0x30,0x2C,0x02,0x01,0x00,0x04,0x06,0x70,0x75,0x62,0x6C,0x69,0x63,0xA0,0x1F,0x02,
           0x04,0x30,0xB1,0xF0,0x32,0x02,0x01,0x00,0x02,0x01,0x00,0x30,0x11,0x30,0x0F,0x06,
           0x0B,0x2B,0x06,0x01,0x02,0x01,0x19,0x03,0x02,0x01,0x03,0x01,0x05,0x00].pack("c*")

sock = UDPSocket.open

sock.send(sendMsg, 0, "192.168.0.xx", 161) # set the IP address of your printer

recvMsg = sock.recv(65535)

p recvMsg

sock.close

5. 上記のスクリプトを実行する。

$ cd bin
$ ./mruby sysDescr.rb
"0@\002\001\000\004\006public\2423\002\0040\261\3602\002\001\000\002\001\0000%0#\006\v+\006\001\002\001\031\003\002\001\003\001\004\024EPSON EP-806A Series"

バイナリの解析を省略しているので読みにくいですが、バイト列の最後にプリンター名 "EPSON EP-806A Series" を取得できていることがわかります。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3