Solved

C# Problem translating Comm functions from VB.Net

Posted on 2012-12-30
6
688 Views
Last Modified: 2012-12-30
The two enclosed proc works perfectly in VB2008 but I'm unable to translate them correctly to C#
If I am understanding well the doc I need to use "static" features but they don't compile.
I'm quite sure the error will be obvious to an expert !
Thanks for the help  

Original VB2008 Code:

Public Function OpenCommPort(ByVal SaxComm As CommStudio.SaxComm, ByVal nCommPort As Integer) As Integer

        Dim vInput As Object

        If (SaxComm.PortOpen = True) Then
            If (SaxComm.CommPort = "COM" & CStr(nCommPort)) Then 'correct comm port already open
                OpenCommPort = nCommPort
                Exit Function
            Else 'port open, but wrong comm port
                SaxComm.PortOpen = False    'close the port before re-assigning CommPort
            End If
        End If

        SaxComm.CommPort = nCommPort    'can not re-assign unless comm port closed

        On Error Resume Next 'skip error messages from appearing
        SaxComm.PortOpen = True

        If (SaxComm.PortOpen = True) Then
            ' Comm Port open OK
            SaxComm.InputLen = 0
            vInput = SaxComm.Input  'Clear Buffer and reset count
        End If

        OpenCommPort = nCommPort

    End Function

    Private Sub RxOEMDF1()

        Static vInput As Object

        If blnInSync Then 'In-sync
            If vInput(SaxComm1.InBufferCount >= 3) Then
                SaxComm1.InputLen = 3 'get next 3 characters
                vInput = SaxComm1.Input
                nNoDataCtr = TB1_TWO_SEC

                ' put data in Byte array
                naByte(1) = vInput(0)
                naByte(2) = vInput(1)
                naByte(3) = vInput(2)

                ' test frame
                If ((naByte(1) >= &H80) And (naByte(2) < &H80) And (naByte(3) < &H80)) Then
                    ' Frame OK
                    nSpO2_Status = naByte(1) And &H7C
                    nFloatByte(1) = naByte(1) And &H3
                    nFloatByte(2) = naByte(2)
                    nFloatByte(3) = naByte(3)
                    blnNewFrame = True      ' signal New Frame
                    blnNewPacket = True     ' signal New Packet
                Else ' out of sync
                    blnInSync = False
                End If ' Frame test
            End If ' There was 3 characters

        Else ' Not-in-sync
            If vInput Then '(SaxComm1.InBufferCount >= 1) Then
                SaxComm1.InputLen = 1 'to get one character
                vInput = SaxComm1.Input 'get character
                nNoDataCtr = TB1_TWO_SEC

                ' shift byte into Byte array
                naByte(1) = naByte(2)
                naByte(2) = naByte(3)
                naByte(3) = vInput(0)

                ' test frame
                If ((naByte(1) >= &H80) And (naByte(2) < &H80) And (naByte(3) < &H80)) Then
                    ' Frame OK
                    nSpO2_Status = naByte(1) And &H7C
                    nFloatByte(1) = naByte(1) And &H3
                    nFloatByte(2) = naByte(2)
                    nFloatByte(3) = naByte(3)
                    blnInSync = True
                End If ' Frame OK
            End If ' There was a character

        End If ' Else Not-in-sync

    End Sub

Open in new window


My translated code

private void RxOEMDF1()
    {
        //In-sync
        if (blnInSync)
        {
           
            if SaxComm1.InBufferCount >= 3
            {
                SaxComm1.InputLen = 3;
                //get next 3 characters
                static_RxOEMDF1_vInput = SaxComm1.Input;
                nNoDataCtr = TB1_TWO_SEC;

                // put data in Byte array
                naByte(1) = static_RxOEMDF1_vInput(0);
                naByte(2) = static_RxOEMDF1_vInput(1);
                naByte(3) = static_RxOEMDF1_vInput(2);

                // test frame
                if (((naByte(1) >= 0x80) & (naByte(2) < 0x80) & (naByte(3) < 0x80)))
                {
                    // Frame OK
                    nSpO2_Status = naByte(1) & 0x7c;
                    nFloatByte(1) = naByte(1) & 0x3;
                    nFloatByte(2) = naByte(2);
                    nFloatByte(3) = naByte(3);
                    blnNewFrame = true;
                    
                    // signal New Frame
                    blnNewPacket = true;
                    
                    // signal New Packet
                    // out of sync
                }
                else
                {
                    blnInSync = false;
                }
                // Frame test
            }
            // There was 3 characters

            // Not-in-sync
        }
        else
        {
            (SaxComm1.InBufferCount >= 1) Then
            if (static_RxOEMDF1_vInput)
            {
                SaxComm1.InputLen = 1;
                //to get one character
                static_RxOEMDF1_vInput = SaxComm1.Input;
                //get character
                nNoDataCtr = TB1_TWO_SEC;

                // shift byte into Byte array
                naByte(1) = naByte(2);
                naByte(2) = naByte(3);
                naByte(3) = static_RxOEMDF1_vInput(0);

                // test frame
                if (((naByte(1) >= 0x80) & (naByte(2) < 0x80) & (naByte(3) < 0x80)))
                {
                    // Frame OK
                    nSpO2_Status = naByte(1) & 0x7c;
                    nFloatByte(1) = naByte(1) & 0x3;
                    nFloatByte(2) = naByte(2);
                    nFloatByte(3) = naByte(3);
                    blnInSync = true;
                }
                // Frame OK
            }
            // There was a character

        }
        // Else Not-in-sync

    }

Open in new window

0
Comment
Question by:luciepaul
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:gt2847c
ID: 38731395
One quick thing I see is that you're trying to address arrays with () rather than [].  C# uses square brackets for arrays.  Arrays are also numbered starting with zero rather than one.  So the first element in an array is myArray[0], not myArray[1].

Also, static in C# means that the object does not need to be "new'd".  With static objects and functions, they have no dynamic members so you can't have more than one of them at a time.  (btw, this is a very oversimplified description and you should read more on the subject...)

I'll try and look through and see if anything else jumps out...
0
 
LVL 9

Expert Comment

by:gt2847c
ID: 38731423
Other items I see:

Line 7 needs parenthesis around your if statement.  You have:
 if SaxComm1.InBufferCount >= 3      
Should read
 if ( if SaxComm1.InBufferCount >= 3  )

naByte does not appear to be declared anywhere, so I don't know what type it is or how large it is.  And as stated in my previous comment, your references to naByte and nFloatByte should start at zero, for example:
                // put data in Byte array
                naByte[0] = static_RxOEMDF1_vInput[0];
                naByte[1] = static_RxOEMDF1_vInput[1];
                naByte[2] = static_RxOEMDF1_vInput[2];

Open in new window


Try those fixes and see what you get.  Also, if you can include specific compiler errors, it may help determine what else needs to be worked on...
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38731454
C# doesn't have a direct equivalent of VB's local, static variables.  For C#, simply move the declaration of that variable from the method, out to class level.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:luciepaul
ID: 38731494
@gt2847c
After fixing what you suggest everything seems to compile now.
I'm just left with another faulty line :

In VS2008:
         If (naByte(2) And &H1) Then

Translated to  C#
         if ((naByte[2] & 0x1)
gives an error message : "unable to convert implicitly int in bool"
0
 
LVL 9

Accepted Solution

by:
gt2847c earned 500 total points
ID: 38731513
C# does not do implicit int (zero/non-zero) conversions to boolean true/false like C and other languages did (forcing you into strong typing in use of booleans).  You'll have to test explicitly what you're looking for.  If you want a non-zero value to be true, you'll need to write your if statement like:

if(( naByte[2] & 0x1) == 0x1)

or

if(( naByte[2] & 0x1) != 0)

or

if(( naByte[2] & 0x1) > 0)

Whatever your preference is
0
 

Author Closing Comment

by:luciepaul
ID: 38731548
Great help
Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now