问题描述
我很难理解串行"和流"这两个术语之间的区别.串行不是流的一种吗?
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中是无符号长整数,我们如何使用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作为无符号长整数. 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!
推荐答案
Stream是Serial继承的基类.串行是一种类型的流,但是还有其他类型的流.
Stream is the base class that Serial inherits. Serial is a type of Stream but there are other types of Stream.
写与打印在一个重要方面不同.写将事物发送为原始字节,而打印将事物发送为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 I Serial.print(255) the arduino will actually send out 3 bytes, the ascii codes for all three digits. But if I Serial.write(255) then the Arduino will send out one single byte with the value of 255 (0b11111111).
写入返回的数字是已写入的字节数.它返回给调用者,而不是串行流.它告诉调用方已写入多少字节.
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.
您在这里有一些非常基本的误解.未签名版本与签名版本的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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!