转换旧代码帮助。 VB6(我认为)到C#

时间:2012-11-01 09:46:44

标签: c# vb.net legacy

我已经尝试过这一步,但我正在努力将其纳入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

我认为有两个字节传到这里。 4和176.我也无法运行代码。

谁能告诉我C#的等价物是什么?或者只是解释一下nChar会发生什么。非常感谢!

1 个答案:

答案 0 :(得分: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;
}