Gary Soh

Gary Soh

  • NA
  • 5
  • 1.8k

Visual C# problem, SerialPort DataReceive

Oct 4 2011 2:28 AM
Hello,

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

[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[/CODE]

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:
[CODE]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;*/
}[/CODE]

Thank you!

Answers (5)