Search code examples
c#vb.netlegacy

Convert old Code Help. VB6 (I think) to C#


I have tried follow this through, but I am struggling to get it into 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

I have 2 bytes that I think are passed into here. 4 and 176. I can't run the code either.

Can anyone tell me what the equivalent C# would be? Or just explain what happens to nChar on the way through. Many thanks!


Solution

  • 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;
    }