本文介绍了Arduino 上的 Serial 和 Stream 有什么区别,Serial.write 是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解术语串行和流之间的区别时遇到了很多麻烦.Serial 不是 Stream 的一种吗?

I'm having a lot of trouble understanding the difference between the terms Serial and Stream. Is Serial not a type of Stream?

我有一些我不明白的家庭作业问题.

I have questions on homework that I just don't understand.

计算机通过使用(流还是串行?)来读取"它们相互发送的数据以确定每个字节的含义?

Computers "read" the data they send each other by using (Streams or Serial?) to determine what each byte means?

此外,Serial.write() 也让我很困惑.它返回一个字节的数据,对吗?一个字节是 8 位.因此,如果将 int 类型(16 位)传递给 Arduino 上的 Serial.write(),它将向串行流返回 2 个字节?

Also, Serial.write() confuses me a lot too. It returns a byte of data, correct? A byte is 8 bits. So if an int type (16-bits) is passed to Serial.write() on the Arduino, it would return 2 bytes to the serial stream?

同样,如果 Arduino C 中的变量是 unsigned long,我们如何使用 Serial.write() 将这个变量的最高有效字节表示给串行流?

Similarly, if a variable is an unsigned long in Arduino C, how can we represent the most significant byte of this variable to the serial stream using Serial.write()?

例如,我有变量 x 作为 unsigned long.Serial.write(x>>8) 是否是正确答案,因为 long 是 32 位,因此无符号使其大两倍.由于 Serial.write() 以字节为单位返回,因此 64/8 将是 8.

For example, I have variable x as the unsigned long. Would Serial.write(x>>8) be the correct answer, because a long is 32 bits so unsigned makes it twice as big. Since Serial.write() returns in bytes, 64/8 would be 8.

所有这些问题可能看起来都很业余,但我真的很想学习这些东西,而我的老师并不擅长解释.如果有人能在概念上更清楚地说明这一点,我将永远感激不尽.谢谢!

All of these questions may seem really amateur, but I really want to learn this stuff and my teacher is not the best at explaining. If anyone can make this more clear conceptually, I will be eternally grateful. Thank you!

推荐答案

StreamSerial 继承的基类.Serial 是一种 Stream 但也有其他类型的 Stream.

Stream is the base class that Serial inherits. Serial is a type of Stream but there are other types of Stream as well.

writeprint 在一个重要方面不同:write 将事物作为原始字节发送,而 print 发送ASCII 之类的东西.因此,如果我 Serial.print(255),Arduino 实际上会发送 3 个字节,即所有三个数字的 ASCII 代码.但是,如果我 Serial.write(255) 然后 Arduino 将发送一个值为 255 (0b11111111) 的单个字节.

write is different from print in one important way: write sends things as raw bytes and print sends things as ASCII. So, if I Serial.print(255), the Arduino will actually send out 3 bytes, the ASCII codes for all three digits. However, if I Serial.write(255) then the Arduino will send out one single byte with the value of 255 (0b11111111).

write 返回的数字是写入的字节数.它返回给调用者,而不是串行流.它告诉调用者写入了多少字节.

The number that write returns is the number of bytes that were written. It returns to the caller, not the serial stream. It tells the caller how many bytes got written.

例如,我有一个变量 x 作为 unsigned long.Serial.write(x>>8) 是否是正确答案,因为 long 是 32 位,所以 unsigned 使其大两倍.由于 Serial.write() 以字节为单位返回,因此 64/8 将是 8

您在这里有一些非常根本的误解.无符号版本与有符号版本具有相同的 32 位.它可以容纳两倍大的数字,因为它不需要符号位,但它具有相同的位数.要保留 32 位数量的 MSB,您需要右移 24 位.位移位以位为单位,而不是字节,因此 myLong >>24.

You have some very fundamental misunderstandings here. The unsigned version is the same 32 bits as the signed version. It can hold a number twice as big because it doesn't need a sign bit, but it's got the same number of bits. To leave the MSB of a 32 bit quantity you need to shift right by 24 bits. Bitshifts are in bits, not bytes, so myLong >> 24.

这篇关于Arduino 上的 Serial 和 Stream 有什么区别,Serial.write 是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 07:15