Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   Communications (http://www.xtremevbtalk.com/communications/)
-   -   R232 SerialPort Communication (http://www.xtremevbtalk.com/communications/327104-r232-serialport-communication.html)

flashyjunior 06-12-2014 06:37 AM

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()


Flyguy 06-12-2014 06:56 AM

No, that's not how this forum works.

We will try to help you with specific problems or coding question, but we don't rewrite portions of code for you.

flashyjunior 06-12-2014 07:49 AM

Array Copy
 
Sorry Flyguy,
My apologies

I want to copy an array into a second array at the specific index in the second array.
I read about using the CopyMemory windows API but that doesnt give you the option to specify the sourceindex and the destination index in the second array

Can anyone help me out.

Code:

Private Declare Sub CopyMemory Lib "kernel32" _
    Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)


Flyguy 06-13-2014 12:35 AM

If you had 2 arrays of the same size (12 bytes) the syntax would be the following:
Code:

CopyMemory btDest(0), btSource(0), 12
If you want to copy 4 bytes from position 6 to location 2:
Code:

CopyMemory btDest(1), btSource(5), 4


All times are GMT -6. The time now is 02:09 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.