无法使用 GPIO 引脚将类型 'int' 隐式转换为 'bool'
Cannot implicitly convert type 'int' to 'bool' working with GPIO pins
我正在用我的 Raspberry Pi 3 做一些工作,它是 GPIO 引脚。我正在使用一个 PDF 文件,不幸的是,它是用 Arduino 编码的。我试过查找从 Arduino 到 C# 的转换,但只能找到从 Arduino 到本机 C 的转换。我从中得到了这段代码,它正在编译错误,我想知道为什么。我检查了 Stack Overflow 并在任何地方都找不到这个,所以我自己问了。 (我已经将一些代码转换成我在c#中使用的代码,如顶行。其余的我没有接触过。
void ShiftOut(GpioPin dataPin, GpioPin clockPin, bool MSBFIRST, byte command)
{
for (int i = 0; i < 8; i++)
{
bool output = false;
if (MSBFIRST)
{
output = command & 0b10000000;
command = command << 1;
}
else
{
output = command & 0b00000001;
command = command >> 1;
}
}
}
在 C# 中,bool
和 int
之间没有隐式转换。在这一行中:
output = command & 0b10000000;
command
是一个 byte
0b10000000
是一个 int
command & 0b1000000
returns 一个 int
.
output
是一个 bool
。没有从 int
到 bool
的隐式(或显式)转换,因此赋值失败。
这里的问题是为什么 output
声明为 bool
?
我正在用我的 Raspberry Pi 3 做一些工作,它是 GPIO 引脚。我正在使用一个 PDF 文件,不幸的是,它是用 Arduino 编码的。我试过查找从 Arduino 到 C# 的转换,但只能找到从 Arduino 到本机 C 的转换。我从中得到了这段代码,它正在编译错误,我想知道为什么。我检查了 Stack Overflow 并在任何地方都找不到这个,所以我自己问了。 (我已经将一些代码转换成我在c#中使用的代码,如顶行。其余的我没有接触过。
void ShiftOut(GpioPin dataPin, GpioPin clockPin, bool MSBFIRST, byte command)
{
for (int i = 0; i < 8; i++)
{
bool output = false;
if (MSBFIRST)
{
output = command & 0b10000000;
command = command << 1;
}
else
{
output = command & 0b00000001;
command = command >> 1;
}
}
}
在 C# 中,bool
和 int
之间没有隐式转换。在这一行中:
output = command & 0b10000000;
command
是一个byte
0b10000000
是一个int
command & 0b1000000
returns 一个int
.output
是一个bool
。没有从int
到bool
的隐式(或显式)转换,因此赋值失败。
这里的问题是为什么 output
声明为 bool
?