Jruby学习笔记 -- 用Jruby和java-comm对串口编程

学到的Jruby知识:
Jruby基本知识
Jruby对Java的类常量的引用
Jruby里java bype与ruby string的转换
Jruby实现Java接口的虚函数


本想按装ruby-serialport这个gem,没想到还需要安装nmake,不如用Jruby直接调用Java的包来的简单些,顺便在学习下Jruby

目前,常见的Java串口包有SUN在1998年发布的串口通信API:comm2.0.jar(Windows
下)、comm3.0.jar(Linux/Solaris),可是现在SUN对windows版本的comm.jar已经不再提供下载(可以google javacomm20-win32的到下载连接)。不过http://www.rxtx.org提供了另外一个串口的driver -- RXTXcomm.jar,这个包可以和SUN的comm.jar一起完成串口变成工作。

首先须对comm.jar指定串口driver用RXTXcomm.jar中的gnu.io.RXTXCommDriver,这个需要在comm.jar的目录或者jre的lib目录下创建javax.comm.properties:


Driver=gnu.io.RXTXCommDriver


下面是一个列出所有串口的Jruby程序

require 'java'
require 'comm.jar'
require 'RXTXcomm.jar'

import java.lang.System

# add comm.jar to the class path to make sure the program
# get the properties file
# This line must be before import
System.setProperty("java.class.path", "#{System.getProperty("java.class.path")}comm.jar")

import javax.comm.CommPortIdentifier
# Jruby convert method getPortIdentifiers to identifiers to be more ruby style
# Of course, getPortIdentifiers works too
CommPortIdentifier.port_identifiers.each do |portId|

# need to convert CommPortIdentifier.PORT_SERIAL -- java format
# to CommPortIdentifier::PORT_SERIAL -- ruby format

if (portId.getPortType() == CommPortIdentifier::PORT_SERIAL)
puts portId.getName()
end

end


如果用2.0版的SUN的comm.jar,步骤是类似的:
javax.comm.properties:


Driver=com.sun.comm.Win32Driver


程序也是类似的,只需要移除下面这行

require 'RXTXcomm.jar'


下面是个稍微复杂的例子,用来读写串口

require 'java'
require 'comm.jar'
import java.lang.System

System.setProperty("java.class.path", "#{System.getProperty("java.class.path")};comm.jar")
import javax.comm.CommPortIdentifier
import javax.comm.SerialPort

CommPortIdentifier.port_identifiers.each do |portId|
if ( portId.name() == "COM3")
serial_port = portId.open("Modem", 2000)
serial_port.setSerialPortParams(9600,
SerialPort::DATABITS_8,
SerialPort::STOPBITS_1,
SerialPort::PARITY_NONE)
serial_port.output_stream.write("AT+GMM\r".to_java_bytes)
bytes = Java::byte[20].new
len = serial_port.input_stream.read(bytes)
puts String.from_java_bytes bytes
end
end



再复杂一点儿的例子,用Jruby完成java的接口,完成回调函数


require 'java'
require 'comm.jar'
import java.lang.System

System.setProperty("java.class.path", "#{System.getProperty("java.class.path")};comm.jar;.")
import javax.comm.CommPortIdentifier
import javax.comm.SerialPort
import javax.comm.SerialPortEventListener
import javax.comm.SerialPortEvent

#完成SerialPortEventListener的serialEvent接口
module SerialPortEventListener
def initialize(serial_port)
@serial_port = serial_port
end

def serialEvent(e)
case e.event_type
when SerialPortEvent::DATA_AVAILABLE
bytes = Java::byte[20].new
len = @serial_port.input_stream.read(bytes)
puts String.from_java_bytes bytes
when SerialPortEvent.BI
puts "\n--- BREAK RECEIVED ---\n"
end
end
end

CommPortIdentifier.port_identifiers.each do |portId|
if ( portId.name() == "COM3")
serial_port = portId.open("Modem", 2000)

serial_port.notifyOnDataAvailable(true)
serial_port.notifyOnBreakInterrupt(true)

serial_port.addEventListener(SerialPortEventListener.new(serial_port))
serial_port.setSerialPortParams(9600,
SerialPort::DATABITS_8,
SerialPort::STOPBITS_1,
SerialPort::PARITY_NONE)
puts "Write command to the port COM3"
serial_port.output_stream.write("AT+GMM\r".to_java_bytes)
sleep 3
puts "Write command to the port COM3"
serial_port.output_stream.write("AT+GMM\r".to_java_bytes)
sleep 3
puts "Write command to the port COM3"
serial_port.output_stream.write("AT+GMM\r".to_java_bytes)

exit
end
end

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值