はじめに
Raspberry Piの拡張セットのLazurite Pi Gatewayを使用するときのメモ
使用機器
Raspberry Pi 1個
Lazurite Pi Gateway 1個
Luzurite 1個
Ruby で送受信
送信サンプル
Rubyディレクトリーに移動
$cd ruby
ruby ディレクトリーに以下の3つのファイルがある
BinaryMonitor.rb SendTest.rb SerialMonitor.rb
SendTest.rb の内容を編集する
SendTest.rb
#! /usr/bin/ruby
# -*- coding: utf-8; mode: ruby -*-
# Function:
# Lazurite Sub-GHz/Lazurite Pi Gateway Sample program
# SerialMonitor.rb
require 'net/http'
require 'date'
##
# BP3596 API
##
class BP3596PipeApi
##
# func : Read the data from the receiving pipe
# input : Receive pipe fp
# return : Receive data
##
def write_device(fp,data)
ret = select(nil, [fp], nil, 0.1)
begin
len = fp.write(data);
rescue
raise EACK
end
return len;
end
def read_device(fp)
# Data reception wait (timeout = 100ms)
ret = select([fp], nil, nil, 0.1)
# Reads the size of the received data
len = fp.read(2)
if ((len == "") || (len == nil)) then # read result is empty
return -1
end
size = len.unpack("S*")[0]
# The received data is read
recv_buf = fp.read(size)
if ((recv_buf == "") || (recv_buf == nil)) then # read result is empty
return -1
end
return recv_buf
end
def BinaryMonitor(raw)
len = raw.length
header = raw.unpack("H4")[0]
# unsupported format
if header != "21a8" then
unsupported_format(raw)
return
end
# supported format
seq = raw[2].unpack("H2")[0]
# PANID
myPanid = raw[3..4].unpack("S*")[0]
# RX Address
rxAddr = raw[5..6].unpack("S*")[0]
# TX Address
txAddr = raw[7..8].unpack("S*")[0]
#
print(sprintf("PANID=0x%04X, rxAddr=0x%04X, txAddr=0x%04X, DATA:: ",myPanid, rxAddr, txAddr))
for num in 9..len-2 do
print(raw[num].unpack("H*")[0]," ")
end
print("\n") end
# printing unsupported format
def unsupported_format(raw)
data = raw.unpack("H*")
print("unsupported format::",data,"\n")
end
end
##
# Main function
##
class MainFunction
### Variable definition
bp3596_dev = "/dev/bp3596" # Receiving pipe
finish_flag = 0 # Finish flag
# Process at the time of SIGINT Receiving
Signal.trap(:INT){
finish_flag=1
}
# Open the Receiving pipe
bp3596_fp = open(bp3596_dev, "rb")
bp3596_wr = open(bp3596_dev, "wb")
p bp3596_wr
bp_api = BP3596PipeApi.new
data = [0xa821,0x00,0xabcd,0x8e35,0x87a4,"hello"].pack("scsssa*")
p data
for i in 0..100 do
if finish_flag == 1
next
end
begin
status = bp_api.write_device(bp3596_wr,data)
p status
rescue
print("does not receive ack\n")
end
end
bp3596_fp.close
bp3596_wr.close
end
103行目を編集
data = [0xa821,0x00,0xabcd,0x8e35,0x87a4,"hello"].pack("scsssa*")
0x8e35
の部分を送信先機器のデバイスアドレスを指定する
"hello"
の部分を送信したい文字列に変更する
プログラム実効
ドライバの有効をしてからsudo権限でプログラムを実行する。
$sudo insmod ~/driver/sub-ghz/DRV_802154.ko
$sudo ruby SendTest.rb
受信プログラム
ドライバを有効にしてプログラムをsudo権限で実行すると、送られてきたデータを以下のように表示してくれる
pi@raspberrypi ~/ruby $sudo insmod ~/driver/sub-ghz/DRV_802154.ko
pi@raspberrypi ~/ruby $ sudo ruby recive.rb
PANID=0xABCD, rxAddr=0x668C, txAddr=0x5F5A, Strings:: Welcome to Lazurite Sub-GHz
PANID=0xABCD, rxAddr=0x668C, txAddr=0x5F5A, Strings:: Welcome to Lazurite Sub-GHz
PANID=0xABCD, rxAddr=0x668C, txAddr=0x5F5A, Strings:: Welcome to Lazurite Sub-GHz
PANID=0xABCD, rxAddr=0x668C, txAddr=0x5F5A, Strings:: Welcome to Lazurite Sub-GHz
PANID=0xABCD, rxAddr=0x668C, txAddr=0x5F5A, Strings:: Welcome to Lazurite Sub-GHz
受信 プログラムの編集
SerialMonitor.rb
#! /usr/bin/ruby
# -*- coding: utf-8; mode: ruby -*-
# Function:
# Lazurite Sub-GHz/Lazurite Pi Gateway Sample program
# SerialMonitor.rb
require 'net/http'
require 'date'
##
# BP3596 API
##
class BP3596PipeApi
##
# func : Read the data from the receiving pipe
# input : Receive pipe fp
# return : Receive data
##
def read_device(fp)
# Data reception wait (timeout = 100ms)
ret = select([fp], nil, nil, 0.1)
# Reads the size of the received data
len = fp.read(2)
if ((len == "") || (len == nil)) then # read result is empty
return -1
end
size = len.unpack("S*")[0]
# The received data is read
recv_buf = fp.read(size)
if ((recv_buf == "") || (recv_buf == nil)) then # read result is empty
return -1
end
return recv_buf
end
def print_raw(raw)
fc = raw.unpack("H*")
len = raw.length
print(len,"bytes: ",fc,"\r\n")
end
def print_raw2(raw)
len = raw.length
header = raw.unpack("H4")[0]
# unsupported format
if header != "21a8" then
unsupported_format(raw)
return
end
# supported format
seq = raw[2].unpack("H2")[0]
# PANID
myPanid = raw[3..4].unpack("S*")[0]
# RX Address
rxAddr = raw[5..6].unpack("S*")[0]
# TX Address
txAddr = raw[7..8].unpack("S*")[0]
#
str_buf = raw[9..len-2].unpack("Z*")[0]
print(sprintf("PANID=0x%04X, rxAddr=0x%04X, txAddr=0x%04X, Strings:: %s",myPanid,rxAddr,txAddr,str_buf))
end
# printing unsupported format
def unsupported_format(raw)
data = raw.unpack("H*")
print("unsupported format::",data,"\n")
end
end
##
# Main function
##
class MainFunction
### Variable definition
bp3596_dev = "/dev/bp3596" # Receiving pipe
finish_flag = 0 # Finish flag
# Process at the time of SIGINT Receiving
Signal.trap(:INT){
finish_flag=1
}
# Open the Receiving pipe
bp3596_fp = open(bp3596_dev, "rb")
bp_api = BP3596PipeApi.new
# Loop until it receives a SIGINT
while finish_flag==0 do
# Read device
recv_buf = bp_api.read_device(bp3596_fp)
if recv_buf == -1
next
end
# Create a transmit buffer from the receive buffer
bp_api.print_raw2(recv_buf)
end
# Close the Receiving pipe
bp3596_fp.close
end
受信した文字列 (67行)
print(sprintf("PANID=0x%04X, rxAddr=0x%04X, txAddr=0x%04X, Strings:: %s",myPanid,rxAddr,txAddr,str_buf))
受信された文字列は str_buf 変数に保存される。
なので、受信した結果より制御をしたい場合は、
print(sprintf("PANID=0x%04X, rxAddr=0x%04X, txAddr=0x%04X, Strings:: %s",myPanid,rxAddr,txAddr,str_buf))
if str_buf == "hello\r\n"
p "hello"
elsif str_buf == "world\r\n"
p "world"
end
のようにするとよい。