使用 Ruby 将字节数组写入串行
我不清楚如何用 ruby 编写简单的字节码数组,更多我完全不知道如何使用 Ruby SerialPort 库,老实说,我让它工作得很好,但我只成功地通过串行端口发送 ASCII。
例如,编写 ASCII 非常简单:
@sp = SerialPort.new "/dev/tty.usbserial-A6004cNN", 19200
@sp.write "test"
显然将 test
写入该串行设备。这工作正常,我已经能够将所有预期结果发送到微控制器(arduino)在这种情况下。问题是我需要编写串行设备将读取的输出,如下所示:
{0x01,0x09,0x04,0x00, 'f',0xff,0xcc,0x33}
我尝试使用 str.unpack
但我仍然无法生成所需的十六进制值输出作为字节,如上所述。
在 Java 中,使用它的串行库很简单:
byte[] cmd = { 0x01,0x09,0x04,0x00, 'f',(byte)0xff,(byte)0xcc,(byte)0x33 };
serialPort.write( cmd );
如何使用 Ruby 将正确的字节码输出到我的串行设备?
I'm not clear on how to write simple byte code arrays with ruby, more-so I'm absolutely stumped on how to use the Ruby SerialPort library, well to be honest I have it working pretty well however I have only been successful in sending ASCII over the serial port.
For example it's really simple to write ASCII:
@sp = SerialPort.new "/dev/tty.usbserial-A6004cNN", 19200
@sp.write "test"
Which obviously writes test
to that serial device. This works fine and I've been able to get all the expected results sent to a micro-controller (arduino) in this case. The issue is that I need to write output which the serial device will read like so:
{0x01,0x09,0x04,0x00, 'f',0xff,0xcc,0x33}
I've tried using str.unpack
but am still unable to produce the desired hex values output as bytes as above.
In Java it is simple using it's serial library:
byte[] cmd = { 0x01,0x09,0x04,0x00, 'f',(byte)0xff,(byte)0xcc,(byte)0x33 };
serialPort.write( cmd );
How can I output the proper bytecode to my serial device with Ruby?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但我们可以玩得更开心(哈哈哈……)
那么:
But we can have more fun than that (muhahaha...)
So then: