编译器使用 Port.Write 的字符重载而不是字节
本文关键字:重载 字节 字符 Port Write 编译器 | 更新日期: 2023-09-27 18:37:14
我正在尝试移植.将字节变量写入串行端口,但编译器仍然给我一个错误
无法将字节转换为字符[]
看起来它使用了错误的重载(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
}