This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
component:modem:zh [2023/10/25 08:05] hfsr created |
component:modem:zh [2024/02/13 03:45] (current) hfsr [组件:调制解调器] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Component: Modem | + | 组件:调制解调器 |
================ | ================ | ||
- | This component is provided by [[item:network_card|network cards]]. Wireless network cards behave much like normal network cards, but additionally send the message as a wireless "packet" when a strength is set. The modem's address must be used for networking. It can be found through component.modem.address. | + | 此组件由[[item:network_card:zh|网卡]]提供。无线网卡的特性与普通网卡基本相同,但是当设定了信号强度时还能将报文以无线“数据包”的形式发送出去。在组网时需要用到网卡的地址,地址可通过`component.modem.address`获取。 |
- | Component name: `modem`. | + | 组件名:`modem`。 |
- | Callbacks: | + | |
+ | 回调函数: | ||
- `isWireless(): boolean` | - `isWireless(): boolean` | ||
- | Returns whether this modem is capable of sending wireless messages. | + | 返回此调制解调器是否可用于发送无线报文。 |
+ | \\ | ||
- `maxPacketSize(): number` | - `maxPacketSize(): number` | ||
- | Returns the maximum packet size for sending messages via network cards. Defaults to 8192. You can change this in the OpenComputers configuration file. | + | 返回通过网卡传输报文时的最大数据包大小,默认为8192。你可以在OC模组的配置文件中修改此数值。 |
- | Every value in a message adds two bytes of overhead. (Even if there's only one value.) | + | 报文中的每个值都会使其开头增加2字节。(就算只有一个值也会添加) |
- | Numbers add another 8 bytes, true/false/nil another 4 bytes, and strings exactly as many bytes as the string contains---though empty strings still count as one byte. | + | 数字型变量占用8字节,true/false/nil占用4字节,字符串所占字节数与字符串本身的字节数相等,空字符串按照1字节计算。 |
- | Examples: | + | 样例: |
- | * `"foo"` is a 5-byte packet; two bytes of overhead and a three byte string. | + | * `"foo"`是一个5字节长的数据包;开头添加2字节,内容有3字节长的字符串。 |
- | * `"currentStatus",300` is a 25-byte packet; four bytes overhead, a 13-byte string, and 8 bytes for a number. | + | * `"currentStatus",300`是一个25字节长的数据包;开头添加4字节,内容有13字节长的字符串和一个占8字节的数字。 |
+ | \\ | ||
- `isOpen(port: number): boolean` | - `isOpen(port: number): boolean` | ||
- | Returns whether the specified "port" is currently being listened on. Messages only trigger signals when they arrive on a port that is open. | + | 返回指定“端口”是否处于侦听状态。报文只有在传输到开放的端口时才会触发信号。 |
+ | \\ | ||
- `open(port: number): boolean` | - `open(port: number): boolean` | ||
- | Opens the specified port number for listening. Returns `true` if the port was opened, `false` if it was already open. **Note: maximum port is 65535** | + | 开放指定编号的端口,使其开始侦听。开启成功则返回`true`,若已开启则返回`false`。**注: 最大端口号为65535** |
+ | \\ | ||
- `close([port: number]): boolean` | - `close([port: number]): boolean` | ||
- | Closes the specified port (default: all ports). Returns true if ports were closed. | + | 关闭指定端口(不指定则默认为全部端口)。若端口成功关闭则返回`true`。 |
+ | \\ | ||
- `send(address: string, port: number[, ...]): boolean` | - `send(address: string, port: number[, ...]): boolean` | ||
- | Sends a network message to the specified address. Returns `true` if the message was sent. This does *not* mean the message was received, only that it was sent. No port-sniffing for you. | + | 向指定地址发送一条网络报文。若报文发送成功返回 `true` ,但**不意味着**报文已被接收,只意味着已被发送。我们不提供端口嗅探功能。 |
- | Any additional arguments are passed along as data. These arguments must be basic types: nil, boolean, number and string values are supported, tables and functions are not. See [[api:serialization|the serialization API]] for serialization of tables. | + | 传递给此函数的所有额外参数都将被作为数据传输。这些参数必须是基本类型:支持`nil`、布尔型、数字型以及字符串型,而不支持表和函数。参阅[[api:serialization:zh|serialization(序列化) API]]以获取将表序列化的方法。 |
- | The number of additional arguments is limited. The default limit is 8. It can be changed in the OpenComputers configuration file, but this is //not// recommended; higher limits can allow relatively weak computers to break relatively strong ones with no defense possible, while lower limits will prevent some protocols from working. | + | 额外参数的数量有上限,默认限制为8。此限制可以在OC模组的配置文件中修改,但是**不推荐**这样做。将限制调高可能会使性能较弱的电脑不可避免地经常崩溃,而将限制调低可能会使某些协议无法工作。 |
+ | \\ | ||
- `broadcast(port: number, ...): boolean` | - `broadcast(port: number, ...): boolean` | ||
- | Sends a broadcast message. This message is delivered to all reachable network cards. Returns `true` if the message was sent. Note that broadcast messages are *not* delivered to the modem that sent the message. | + | 发送一条广播报文。此报文会被发送给所有可到达的网卡。若报文被成功发送则返回 `true`。请注意广播报文**不会**传递给发送这条报文的调制解调器自身。 |
- | All additional arguments are passed along as data. See `send`. | + | 所有额外参数都将作为数据一并发送,参见`send`。 |
+ | \\ | ||
- `getStrength(): number` | - `getStrength(): number` | ||
- | The current signal strength to apply when sending messages. | + | 获取当前发送报文时使用的信号强度。 |
- | //Wireless network cards only.// | + | **仅限无线网卡。** |
+ | \\ | ||
- `setStrength(value: number): number` | - `setStrength(value: number): number` | ||
- | Sets the signal strength. If this is set to a value larger than zero, sending a message will also generate a wireless message. Also, calls to set the strength that exceed the installed modem's maximum strength will simply set the modem's strength to it's maximum. The higher the signal strength the more energy is required to send messages, though. | + | 设置信号强度。若此值被设定为大于0,则发送报文时会同时生成一条无线报文。若调用此函数时设定的强度超出调制解调器的最大信号强度,则会将其信号强度设定为允许的最大值。信号强度越高,发送报文时所需的能量就越多。 |
- | //Wireless network cards only.// | + | **仅限无线网卡。** |
+ | \\ | ||
- `getWakeMessage():string` | - `getWakeMessage():string` | ||
- | Gets the current wake-up message. When the network card detects the wake message (a string in the first argument of a network packet), on any port and the machine is off, the machine is started. Works for robots, cases, servers, drones, and tablets. [[component:tunnel|Linked Cards]] provide this same functionality. | + | 获取当前设置的唤醒消息。当电脑处于关闭状态时,网卡的任意端口接收到了唤醒消息(某网络数据包第一个参数中的字符串),则电脑会启动。此功能可用于机器人、普通电脑、服务器、无人机以及平板电脑。[[component:tunnel:zh|连接卡]]也提供了相同的功能。 |
+ | \\ | ||
- `setWakeMessage(message: string, [fuzzy: boolean]):string` | - `setWakeMessage(message: string, [fuzzy: boolean]):string` | ||
- | Sets the wake-up message to the specified **string**. The message matching can be fuzzy (default is false). A fuzzy match ignores additional trailing arguments in the network packet. | + | 将唤醒消息设定为给定的**字符串**。消息可以模糊匹配(由`fuzzy`参数决定,默认为`false`)。模糊匹配时会忽略网络数据包中的其他参数。 |
- | This component generates a signal named `modem_message` if a message from another network card is received.\\ | + | 当收到来自其他网卡的报文时,此组件会产生名为`modem_message`的信号。 |
- | It has the signature `localAddress: string, remoteAddress: string, port: number, distance: number, ...`. | + | 信号的签名为:`localAddress: string, remoteAddress: string, port: number, distance: number, ...`。 |
- | - `localAddress` is the address of the modem component the message was received by. | + | - `localAddress`为收到报文的调制解调器组件地址。 |
- | - `remoteAddress` is the address of the network card the message was sent from. | + | - `remoteAddress`为发送报文的调制解调器地址。 |
- | - `port` is the port number the message was delivered to. | + | - `port`为报文被发送到的端口号。 |
- | - `distance` is the distance to the modem that sent the message. This is only set for wireless messages. For normal messages this is always 0. | + | - `distance`为到发送报文的网卡之间的距离。此值只对无线报文有意义。对于普通报文此值总为0。 |
- | - All further values are values passed along by the sender (i.e. the `...` in `send` and `broadcast`). | + | - 其他所有值均为发送者一并传输的数据(即`send`与`broadcast`函数中的`...`)。 |
- | Example use: | + | 使用例: |
```lua | ```lua | ||
local component = require("component") | local component = require("component") | ||
local event = require("event") | local event = require("event") | ||
- | local m = component.modem -- get primary modem component | + | local m = component.modem --获取首选调制解调器组件 |
m.open(123) | m.open(123) | ||
print(m.isOpen(123)) -- true | print(m.isOpen(123)) -- true | ||
- | -- Send some message. | + | --发送信息。 |
m.broadcast(123, "this is a test") | m.broadcast(123, "this is a test") | ||
- | -- Wait for a message from another network card. | + | --等待来自其他网卡的信息。 |
local _, _, from, port, _, message = event.pull("modem_message") | local _, _, from, port, _, message = event.pull("modem_message") | ||
print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message)) | print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message)) |