View Single Post
 
Old 06-12-2014, 07:37 AM
flashyjunior flashyjunior is offline
Newcomer
 
Join Date: Jun 2014
Posts: 2
Default R232 SerialPort Communication

Hi,
Can someone please assist me urgently to translate this vb.net piece of code into vb6 for me.

Thanks everyone
Please its urgent


Code:
Static SeqNum As Byte = 60

                Dim rtype As String = cboRecType.Text
                Dim MRC As String = txtMRC.Text
                Dim TIN As String = txtTIN.Text
                Dim RecNo As String = txtRecNo.Text
                Dim recDate As String = Format(Date.Now, "dd/MM/yyyy")
                Dim recTime As String = Format(Date:confused:.Now, "HH:mm:ss").ToString
                Dim RecDateTime As String = recDate & " " & recTime

                'Dim StringData As String = rtype & MRC & "," & TIN & "," & RecDateTime & "," & RecNo & ",0.00,18.00,0.00,0.00,11.00,12.00,0.00,0.00,0.00,1.83,0.00,0.00"
                Dim StringData As String = rtype & MRC & "," & TIN & "," & RecDateTime & "," & RecNo & ",0.00,18.00,0.00,0.00,00.00,4525.42,0.00,0.00,0.00,814.58,0.00,0.00"

                Dim DataLen As Byte = CByte(StringData.Length)
                Const HeaderLen As Byte = 4  'Preamble(01), Len, Seq, Cmd
                Const _1 As Byte = 1 'with Option Strict On, you can't do SeqNum += 1 'because 1 is not a byte, so we'll use the constant _1

                Dim leng As Byte = CByte(HeaderLen + DataLen + 32) ' +32 protocol specified bias 
                Dim btMessage(HeaderLen + DataLen + 6 - 1) As Byte '+ 6 for post amble, CRC and Terminator (- 1 because 0 based array)

                SeqNum += _1
                If SeqNum > 127 Then SeqNum = 32

                'set the other values here
                btMessage(0) = 1
                btMessage(1) = leng
                btMessage(2) = SeqNum 'sequence no 
                btMessage(3) = 198  'C6

                'get the string into a byte array
                Dim btData As Byte() = System.Text.Encoding.ASCII.GetBytes(StringData)
                'copy to the right position in the message
                Array.Copy(btData, 0, btMessage, 4, btData.Length)

                Dim ptr As Integer = HeaderLen + DataLen  'point to next byte (post amble) after header and data
                btMessage(ptr) = 5     'Post amble (summed as part of BCC, so we'll set it before BCC calc)

                'Getting ready to calc BCC
                Dim BCC As Integer = 0
                For i As Int32 = 1 To ptr  'sum from the second byte to and including the post amble byte
                    BCC = BCC + btMessage(i)
                Next

                btMessage(ptr + 1) = CByte(((BCC And &HF000) >> 12) + 48)
                btMessage(ptr + 2) = CByte(((BCC And &HF00) >> 8) + 48)
                btMessage(ptr + 3) = CByte(((BCC And &HF0) >> 4) + 48)
                btMessage(ptr + 4) = CByte((BCC And &HF) + 48)
                btMessage(ptr + 5) = 3 'terminator

                'send btMessage out the serial port here

                'ADRport.Encoding = System.Text.Encoding.GetEncoding(1252)
                ADRport.Write(btMessage, 0, btMessage.Length)

                TextBox1.Text = ADRport.ReadExisting()
Reply With Quote