Hello,

I am trying to translate my Visual Basic program to C#.
Right now i am have having problems translating this code

Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

inchar = SerialPort1.ReadLine
IDstr = Microsoft.VisualBasic.Left(inchar, 2)
Datastr = Microsoft.VisualBasic.Right(inchar, 6)

result = Val("&h" & Datastr)
''Energy = (result * 38) / 1000

End Sub

I'd like to know how to make my IDstr read the most left 2 bits, and Datastr read the right most 6 bits.

For now i have this:

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
string inchar, IDstr, Datastr;
inchar = SerialPort1.ReadLine();
IDstr = Microsoft.VisualBasic.Left(inchar, 2); // need help translating this line
Datastr = Microsoft.VisualBasic.Right(inchar, 6); // need help translating this line

result = Val("&h" & Datastr);
/*''Energy = (result * 38) / 1000;*/
}

Thank you!

After some research,

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            string inchar = string.Empty;
            string IDstr = string.Empty;
            string Datastr = string.Empty;
            Int32 result;

            inchar = SerialPort1.ReadLine();

            IDstr = inchar.Substring(0, 2);
            Datastr = inchar.Substring(inchar.Length - 7);

            result = Val("&h" & Datastr);

            /*''Energy = (result * 38) / 1000;*/
        }

What is Val("&h" & Datastr);?
Whats is Val function for? and &h is some sort of hex value?

This article has been dead for over six months. Start a new discussion instead.