gpt4 book ai didi

java - 究竟什么是串口通信?

转载 作者:行者123 更新时间:2023-11-29 05:33:29 26 4
gpt4 key购买 nike

我最近偶然发现了 Java Communication API ,这是一个 javax 包,用于纯 Java 中的“串行通信”。以前听说过很多次串行通信,但我想我不明白“串行通信”到底是什么意思,或者它意味着什么。

根据 Wikipedia :

In telecommunication and computer science, serial communication is the process of sending data one bit at a time, sequentially, over a communication channel or computer bus.

好的...所以 Java 通信 API 允许我一次读取/写入数据一位。 但这到底是什么意思?!?我不能自己做吗(这里是伪代码)?:

String str = "I want to send this bit by bit.";
byte[] strAsBytes = str.getBytes();
byte[] bits = new byte[strAsBytes.length * 8]; // For each byte we need 8 bits.
for(int i = 0; i < strAsBytes.length; i++) {
for(int j = 0; j < 8; j++) {
// Something like this (again this is just pseudo-code).
// Regardless, populate all elements in the bits array with the
// bit in the position of the current byte.
bits[i*j + j] = getBit(strAsBytes[i], j);
}
}

// Sends the array of bits over a network, maybe using Netty and TCP,
// or something. Sends each bit one by one.
networkManager.sendBitByBit(bits);

// Retrieves the value of a bit at any position in a byte (theByte).
public byte getBit(byte theByte, int position) {
return (theByte) & (0x01 << pos) ;
}

我觉得术语“串行通信”不仅仅意味着“一次发送一位”。这是否意味着我可以使用它从机器上的串行端口读取/写入?还有什么?!?

我想我正在寻找通俗易懂的英语解释,解释更广泛的术语“串行通信”是什么以及您可以用它做什么类型的事情,然后将其放入 Java Communications API 的上下文中。如果有一个或两个示例说明您可以使用 API 完成什么,那也很棒。

最佳答案

在这种情况下,序列指的是 serial port ,这是一个(基本上过时的)连接器,允许进行串行通信(实际上,这只不过是一种通过一根专用线一次发送一位的协议(protocol))。这与 parallel port 形成对比它有多条用于发送和接收的电线。不要让针脚的数量让您感到困惑。只有两条串行线用于数据传输,每条都在一个方向(一根用于发送,一根用于接收),如 this pin diagram 中所述。 .

Java Communications API 提供了与底层系统的串行端口和并行端口进行通信的方法,您可以从这个documentation page 中快速看出这一点。 .由于大多数现代计算机不再有这些端口,因此此 API 可能仅在某些仍使用这些端口进行外部连接的嵌入式设备上运行时才有用。

This HowStuffWorks article通俗易懂地解释了串行端口的大部分内容。

让我进一步消除“串行端口”和“串行协议(protocol)”的歧义。串行端口使用串行协议(protocol),USB 和 Firewire 也是如此。他们每个方向(发送和接收)也只有一根传输线,但他们的协议(protocol)和技术使他们能够更快地传输数据。相比之下,并行协议(protocol)需要更多的功能,并且由于同步开销,截至 2013 年,几乎没有(我想不出任何,欢迎贡献)可以与串行协议(protocol)的速度相媲美。

关于java - 究竟什么是串口通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20311744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com