编译器使用 Port.Write 的 char 重载而不是 byte

Compiler uses char overload of Port.Write instead of byte

我正在尝试 Port.Write 一个字节变量到串行端口,但编译器仍然给我一个错误

cannot convert byte to char[]

看起来它使用了错误的重载 (char[], int, int) 而不是 (byte, int, int)。如何强制编译器使用正确的? 这是我的代码:

private void sendbtn_Click(object sender, EventArgs e)
{
    byte temp;
    temp = (byte) 0x01;
    //Wyslij(sndbox.Text);
    Wyslij(temp, 0, 1);
}
private void Wyslij(byte buffer, int offset, int count)
{
    try { Port.Write(buffer, offset, count); }
#if DEBUG
    catch { return; }
#else
    catch { MessageBox.Show( "Nie można zapisać do portu\nPrawdopodobnie port jest zamknięty."); }
#endif
}

没有接受 byte 参数的重载。有一个接受 byte[] 的重载:SerialPort.Write (Byte[], Int32, Int32),但您需要重写所有代码。

private void sendbtn_Click(object sender, EventArgs e)
{
    byte temp;
    temp = (byte)0x01;
    //Wyslij(sndbox.Text);
    Wyslij(new[] { temp }, 0, 1);
}

private void Wyslij(byte[] buffer, int offset, int count)
{
    try { Port.Write(buffer, offset, count); }
#if DEBUG
    catch { return; }
#else
    catch { MessageBox.Show( "Nie można zapisać do portu\nPrawdopodobnie port jest zamknięty."); }
#endif
}