Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

VB Code Help 16 Bytes at a Time

Status
Not open for further replies.

Suraj143

Active Member
I have a text box It has ASCII characters.What I need is to send characters 16 bytes at a time.

Can somebody assist me to do this.

Code:
MSComm1.Output = Text1.Text & vbCr

Code:
String_Length = Len(Text1.Text)
 
hi Suraj,
Not sure what you are asking.??
 
I'm telling this.Let say I have typed those letters in my text box.

WELCOME TO THE SHOPPING COMPLEX OPEN FROM........

Now what I need is I want to send this string to the PIC UART.But it must send 16 characters at a time until entire string transmits.

Transmit 1st 16 characters---Send 2nd set of characters--Send the balance.
 
I'm telling this.Let say I have typed those letters in my text box.

WELCOME TO THE SHOPPING COMPLEX OPEN FROM........

Now what I need is I want to send this string to the PIC UART.But it must send 16 characters at a time until entire string transmits.

Transmit 1st 16 characters---Send 2nd set of characters--Send the balance.

hi,
If you have a long string you want to break down into 16 character block and output via the MSCOMM.??

You could use:
for p = 1 to 16
mscomm1.output = mid(string,p,1);
next p

for p = 17 to 32
mscomm1.output = mid(string,p,1);
next p


EDIT: or
p=1
mscomm1.output = mid( string, p,16); etc

p=17
mscomm1.output = mid( string, p,16); etc
 
Last edited:
Are you using VB6 or VB 2008 or VB 2010 ? Lots of VBs out there...
This is in VB 2010... this is how i would do it as a example. The listbox of course is not needed heh
Code:
        Dim OutLen As Integer 'Length of entire string...
        Dim OutCount As Integer 'OutLen / 16...
        Dim i As Integer        'Loop Temp
        Dim TestString As String    'Temp string holder
        Dim MyArray As New List(Of String)  'Array Holder
        ListBox1.Items.Clear()      'Clear List

        TestString = TextBox1.Text  'Place out textbox text into string

        OutLen = TestString.Length  ' Get the length of string
        OutCount = OutLen / 16      ' Divide the length by 16

        For i = 0 To OutCount - 1       'Loop to add to array
            MyArray.Add(Mid(TestString, (i * 16) + 1, 16))  'add to array
 
Last edited:
I'd just use a pointer,
Code:
    pointer = 1
    Do While pointer < Len(Text1.Text)
        out = Mid(Text1.Text, pointer, 16)
        Debug.Print out
        pointer = pointer + 16
    Loop
    If pointer <> Len(Text1.Text) Then
        out = Mid(Text1.Text, pointer, 16)
        Debug.Print out
    End If

Change debug to a function to send.

Mike.
 
Last edited:
Mike dont you think if you have lets say 20 characters that it will do only 16 the first loop and the second loop might even try to get characters that arent there like a overrun?
 
Hi Jason,

Yes, you are correct, the second bit isn't needed. That's what happens when you write code after a day drinking. :D

Mike.
 
sHello guys thanks for all of your comments.

I’m using VB 6.I wrote my own routine. Help me to fine tune it.

This is the logic

* There are two text boxes & a command button.
* When command button clicked it will send 16 blocks of Text1.text & shows on the Textbox2.
* It must wait acknowledge from PIC side before sending the other 16 block. Wait until character “A”. In other words for every 16 blocks it waits until receive letter “A”.
*Do this until string length = 0.
*Note = textbox2 only shows the sending 16 characters for debugging.

Code:
Dim string_Length As Integer
Dim pointer As Integer
Dim Ack_Byte As Integer

Private Sub Command1_Click()
	pointer = 1
	string_Length = Len(Text1.Text)
Loop_Here: Call Send_Block		' send 16 characters
	If string_Length > 0 Then	' if string length 0 exit
	While Ack_Byte = A		' wait until acknowledge 'A'
	GoTo Loop_Here			' if not A wait here
	End If
	End Sub

Private Sub Send_Block()
	Ack_Byte = 0
	If string_Length > 16 Then
	Text2.Text = Mid(Text1.Text, pointer, 16)
	pointer = pointer + 16
	string_Length = string_Length - 16

	Else
	If string_Length = 0 Then
	Text2.Text = ""
	Else
	Text2.Text = Mid(Text1.Text, pointer, string_Length)
	string_Length = 0
	End If
	End If
	End Sub

Private Sub Form_Load()
	With MSComm1
    	.CommPort = 1
    	.PortOpen = True
    	.Settings = "9600,n,8,1"
    	End With
	End Sub

Private Sub MSComm1_OnComm()
	If MSComm1.CommEvent = comEvRecieve Then
	Ack_Byte = MSComm1.Input
	End If
	End Sub
 
I can see one problem, you are testing against variable A not ascii "A". Try,
Code:
Loop_Here: Call Send_Block		' send 16 characters
	If string_Length > 0 Then	' if string length 0 exit
	While Ack_Byte[COLOR="red"] <> asc("A")[/COLOR]		' wait until acknowledge 'A'
	[COLOR="red"]Ack_Byte=0                      'clear it[/COLOR]
	GoTo Loop_Here			' if not A wait here
	End If
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top