0

我已经尝试过遵循这一点,但我正在努力将它融入 C#。

Private Function TransmitHex(nChar As Byte, nOption As Boolean) As Boolean
    Dim sHex As String
    Dim nHi As Byte
    Dim nLo As Byte

    sHex = Right("00" + Hex(nChar), 2)

    nHi = AscW(Left$(sHex, 1))
    nLo = AscW(Right$(sHex, 1))

    Comm.Output = ChrW$(nHi)

    Comm.Output = ChrW$(nLo)

End Function

我有 2 个字节,我认为它们被传递到这里。4 和 176。我也无法运行代码。

谁能告诉我等效的 C# 是什么?或者只是解释一下 nChar 在通过的过程中会发生什么。非常感谢!

4

1 回答 1

1
public bool TransmitHex(byte char, bool opt)
{
    //convert to chat to a hex string and that to an array of chars
    var hex = char.ToString("X2").ToCharArray();
    //open a connection to a serialport
    var sp = new SerialPort("COM1");
    //write the hex vals
    sp.Write(hex,0,1);
    sp.Write(hex,1,1);
    return true;
}
于 2012-11-01T10:05:48.840 回答