我一直在扩展阿姆斯特朗经典界面示例的 python 版本。当我通信字节时一切正常。但是,我想传达长整数和浮点数。甚至(哦,不)弦。这是我的代码:
http://pastebin.com/epxgDmvu
http://pastebin.com/rD7CWRkz
首先,我只知道如何发送字节。 erlang 可以向它的接口发送其他东西吗?或者我是否必须将浮点数转换为字节列表,将其发送到python,然后将其组装回python中的浮点数?
而且,还有其他方法:如果我用“f”格式打包,erlang 会将其识别为字节列表。有没有办法说服 erlang 将这些字节作为一个浮点数?如果没有,我在哪里可以找到如何将该 erlang 列表转换为 erlang float?
如果 erlang 只能通信字节,你建议我如何发送整数列表?如果我将整数转换为字节列表,那么我无法在一条消息中发送它们,因为接收者不知道一个整数在哪里结束而另一个整数在哪里开始,对吗?然后我应该一一发送整数吗?
是的,我正在研究 ErlPort 和 py-interface 以及其他一些,但我想从基础开始。
问候,
迪杰斯特拉
I've been expanding on python version of Armstrong's classical example of interfaces. Everything works fine while I communicate bytes. But, I'd like to communicate long integers and floats. Mabye even (oh, no) strings. Here is my code:
http://pastebin.com/epxgDmvu
http://pastebin.com/rD7CWRkz
First of all, all that I know how to send are bytes. Can erlang send anything else to it's inteface? Or do I have to convert float to list of bytes, send it to python and then assemble it back to float in python?
And, the ohter way around: if I pack with 'f' format, erlang recognises that as a list of bytes. Is there a way to persuade erlang to take those bytes as one single float? If not, where can I find out how to convert that erlang list to erlang float?
In case erlang can communicate only bytes, how do you propose that I send a list of integers? If I convert integers to lists of bytes, then I can't send them in one message since reciever won't know where one integer ends and other begins, right? Should I then send integers one by one?
Yes, I am looking into ErlPort and py-interface and some other, but I'd like to start from fundamentals.
Regards,
dijxtra
发布评论
评论(1)
在低级别(从编程的角度来看),您总是通过不同的外部接口(管道、网络、文件...)仅发送/接收一堆字节,与您正在使用的编程语言无关。例如,当您使用管道时(您调用了 open_port/2) 您以字节流的形式发送/接收数据。
要通过流发送任何有用的信息,您需要将其划分为通常命名为消息的块。消息格式定义如何从字节流中提取消息。例如,使用 Type-Length-Value (TLV) 编码 您可以发送以下值不同的长度标有类型。
在 ErlPort 中,它的工作原理如下:
{packet, N}
以便通过此端口发送的所有二进制消息都位于前面按其长度,以 N 个字节发送(长度值编码)。packet=1
)。At a low (from programming point of view) level you always send/receive only a bunch of bytes through different external interfaces (pipes, network, files...) independently from a programming language you're using. For example when you work with pipes (which you got calling open_port/2) you send/receive data as a stream of bytes.
To send any useful information through a stream you need to divide it to chunks which usually named messages. Message format define how messages can be extracted from a stream of bytes. For example using Type-Length-Value (TLV) encoding you can send values of different length marked with a type.
In ErlPort it all works as following:
{packet, N}
so all binary messages sent through this port are preceded by their length, sent in N bytes (Length-Value encoding).packet
value as you pass to open_port/2 (packet=1
by default).