sparky3489 sparky3489 - 3 months ago 15
Vb.net Question

Data manipulation and conversion

I have eight text boxes each can have any numerical value only consisting of the numbers 1-8 up to eight digits each. No 0's or 9's.

I am using a timer to loop through the sequences of text boxes at a given rate. This is already configured.

Now here's the hard part so bare with me.

I need to output the first value of a text box, wait some time then output it's counter letter (below the number on the keyboard), go to the next text box and repeat. Example:

I have in my eight boxes:


1
34
765
2
56
84
7
12345678


After each output of each consecutive text box I need to output:


Q
ER
UYT
W
TY
IR
U
QWERTYUI


The "output" is going to be
SerialPort1.Write(*numbers or letters*)
separated by a timer tick between each ON/OFF. Numbers turn the device on, letters turn it off. Starting with
SerialPort1.Write(TxtLoop1.Text)


I already have the RS-232 communications set up. The information is going to a DLP-IO8 USB 8-channel data acquisition board.

To better understand the device:

1-8 turns ON each channel (12345678)

Q-I turns OFF each channel (QWERTYUI)

Truth table for device:


1 = Ch1 ON | Q = Ch1 OFF
2 = Ch2 ON | W = Ch2 OFF
3 = Ch3 ON | E = Ch3 OFF
--
8 = Ch8 ON | I = Ch8 OFF


Sending a "124" will turn on channels 1, 2 and 4.
Sending a "QWR" will turn off channels 1, 2 and 4.

I've only been coding for a few days now so go easy. Learning as I go.
My form for reference

Ultimately I'm trying bring my PC Interface project up to date by going from a parallel port to USB. This will be the "Custom Loop" function for the device.

Dim loopnum1 As String
loopnum1 = TxtLoop1.Text
Dim ListOfloop1 As String() = loopnum1.Split(New Char() {""})
Dim singlenum1 As String
Dim offloop1 As String
offloop1 = "Q"
For Each singlenum1 In ListOfloop1
SerialPort1.Write(singlenum1)
Timer2.Interval = (cmbSPLoop.Value) * 12
If singlenum1 = "1" Then offloop1 = "Q"
If singlenum1 = "2" Then offloop1 = "W"
If singlenum1 = "3" Then offloop1 = "E"
If singlenum1 = "4" Then offloop1 = "R"
If singlenum1 = "5" Then offloop1 = "T"
If singlenum1 = "6" Then offloop1 = "Y"
If singlenum1 = "7" Then offloop1 = "U"
If singlenum1 = "8" Then offloop1 = "I"
SerialPort1.Write(offloop1)
Next
Dim loopnum2 As String
loopnum2 = TxtLoop2.Text
Dim ListOfloop2 As String() = loopnum2.Split(New Char() {""})
Dim singlenum2 As String
Dim offloop2 As String
offloop2 = "Q"
For Each singlenum2 In ListOfloop2
SerialPort1.Write(singlenum2)
Timer2.Interval = (cmbSPLoop.Value) * 12
If singlenum2 = "1" Then offloop2 = "Q"
If singlenum2 = "2" Then offloop2 = "W"
If singlenum2 = "3" Then offloop2 = "E"
If singlenum2 = "4" Then offloop2 = "R"
If singlenum2 = "5" Then offloop2 = "T"
If singlenum2 = "6" Then offloop2 = "Y"
If singlenum2 = "7" Then offloop2 = "U"
If singlenum2 = "8" Then offloop2 = "I"
SerialPort1.Write(offloop2)
Next


etc.

I guess I either do this eight times in a row once with each increment of TxtLoop#.text or?

Answer

I either do this eight times in a row once with each increment of TxtLoop#.text or ?

I think you want to try out a nested loop, rather than 8 loops that output each character in the 8 textboxes.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        OutputToSerialPort()
    End Sub


Private Sub OutputToSerialPort()
    Dim txt = New TextBox
    For index As Integer = 1 To 8
        txt = FindControl(GroupBox1.Controls, "TextBox" & index)
        Dim ListOfloop As Char() = txt.Text.ToCharArray()  '<- edits on this line
        Dim singlenum As String
        Dim offloop As String
        offloop = "Q"
        For Each singlenum In ListOfloop
            'SerialPort1.Write(singlenum)
            'Timer2.Interval = (cmbSPLoop.Value) * 12
            If singlenum = "1" Then offloop = "Q"
            If singlenum = "2" Then offloop = "W"
            If singlenum = "3" Then offloop = "E"
            If singlenum = "4" Then offloop = "R"
            If singlenum = "5" Then offloop = "T"
            If singlenum = "6" Then offloop = "Y"
            If singlenum = "7" Then offloop = "U"
            If singlenum = "8" Then offloop = "I"
            'SerialPort1.Write(offloop)
        Next
    Next index
End Sub

Public Function FindControl(ByVal oControls As Control.ControlCollection,
ByVal strControlName As String) As Control
    Dim MyCtl As Control

    For Each MyCtl In oControls
        If MyCtl.Name = strControlName Then
            Return MyCtl
        Else
            If MyCtl.Controls.Count > 0 Then
                Return FindControl(MyCtl.Controls, strControlName)
            End If
        End If
    Next
    Return Nothing
End Function

ps I dont think you need loopnum1.Split (or in my code that is .Split(New Char() {""}) its not turning multiple digits into an array, thats why the italics. We discussed this and you let me know you did want to output each digit, in each textbox and I have edited my answer.