LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert.ToByte and logical masks, from VB to LabVIEW

Solved!
Go to solution

Hi guys,

 

I have to write a VI communicating via RS232 with a microcontroller for a PWM application. A colleague has given me the GUI he has developpd in VB and I would like to integrate it into my LabVIEW programme by converting the VB code into Labview code.  Here's the code:

 

**************************************************************************************

 

Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadButton.Click
        Dim bufLen As Integer = 12      // Buffer length
        Dim freq As Integer = 0      // frequency
        Dim pWidth As Integer = 0      // pulse width
        Dim dac As Integer = 0       // Value used in oscillator setting for generating pulse frequency
        Dim addr As Integer = 0      // Address of the pulse width in the pulse generator.
        Dim rTime As Integer = 0      // duration of machining/pulse train in ms.
        Dim returnValue As Byte = 0      // A variable for storing the value returned by the 
                                                       //microcontroller after it receives some data
        Dim buffer(bufLen) As Byte       // creates an array of bytes with 12 cells => buffer size = 8 x12 = 96 bits
// can you explain a bit please I know you're converting the entered values into byte and put them one by one in a specific order. This order is how the 
//microcontroller expects them
            addr = (Floor((pWidth - Tinh) / Tinc)) // Formula from hardware, calculates address setting for pulse generator to set pulse width.
            buffer(0) = Convert.ToByte(Floor(3.322 * (Log10(freq / 1039)))) // Caluclates OCT value for use in setting oscillator for pulse freq.
            dac = (Round(2048 - ((2078 * (2 ^ (10 + buffer(0)))) / freq)))  // Calculates DAC value for use in setting oscillator for pulse freq.

            buffer(1) = Convert.ToByte((dac And &HF00) >> 😎                         //
// &H is the vb.net to tell it its hex code, F00 gives the top 4 bits from a 12 bit value.
            buffer(2) = Convert.ToByte(dac And &HFF) // For values that are larger than 256 (8bits) the value needs to be split across 2 bytes (16 bits) this gets the //bottom 8 bits.  &H is the vb.net to tell it its Hex.
            buffer(3) = Convert.ToByte((addr And &HFF0000) >> 16) // This value may be so large it requires 3 bytes (24bits). This gets the top 8 bits.
            buffer(4) = Convert.ToByte((addr And &HFF00) >> 😎 // This gets the middle 8 bits.
            buffer(5) = Convert.ToByte(addr And &HFF)// This gets the bottom 8 bits.
            buffer(6) = Convert.ToByte((rTime And &HFF0000) >> 16) //This value may also be 3 bytes long.

            buffer(7) = Convert.ToByte((rTime And &HFF00) >> 😎
            buffer(8) = Convert.ToByte(rTime And &HFF)
            buffer(9) = Convert.ToByte(2.56 * ocpBox.Value)  // The ocp pulse period is formed of 256 steps or counts, so if a percentage is requested, this formula gives the number of steps/counts required to set the pulse width

            buffer(10) = Convert.ToByte(tempBox.Value)
            If (tempCheck.Checked = True) Then
                buffer(11) = 1
            Else
                buffer(11) = 0
            End If
            freq = ((2 ^ buffer(0)) * (2078 / (2 - (dac / 1024))))
            pWidth = Tinh + ((((Convert.ToInt32(buffer(3))) << 16) Or ((Convert.ToInt32(buffer(4))) << 😎 Or (Convert.ToInt32(buffer(5)))) * Tinc)
            ' Connect to device
            serialPort1.Write(1) // Sends the number 1. This tells the microcontroller we are about to start sending data. It should respond with a zero if it is ready 
                                         //and the connection is good.



The line "serialPort1.Write(buffer, 0, bufLen)" sends the buffered data where the variables are: buffer =  the buffered data; 0 = the position in the buffer to start from; bufLen = the position in the buffer to stop sending data.
*************************************************************************************************
What's the best way to deal with the Convert.ToBytes and the logical masks And ??
Thanks in advance for your time and consideration,
regards
Alex
0 Kudos
Message 1 of 3
(2,626 Views)
Solution
Accepted by topic author Darxtar

Try this

to byte.png


CLA CTAChampionI'm attending the GLA Summit!
Subscribe to the Test Automation user group: UK Test Automation Group
Message 2 of 3
(2,616 Views)

Thanks aCe,

 

I used a similar way to solve my problem by converting the numbers to bit arrays and used a bit array to mask them 🙂

 

Regards

 

alex

0 Kudos
Message 3 of 3
(2,593 Views)