Software > Software

Dweeno Link: Visual Basic robot control

<< < (2/2)

Doug83:
Now we need to put in the code to make the form work.
To do that, we need to get to the coding view of Form1.
The easy way to do that is to double-click on the title bar of the Form1 window in the designer view currently open.

Another tab will open, showing the same form but in its coding view.

It will look basically like this:


--- Code: ---Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       
    End Sub

End Class

--- End code ---

There is another pastebin link below.
Everything in that pastebin paste, you must copy and past *over* everything that is currently on that tab page.
https://pastebin.com/3uUN48Pn

Save the project, and check that there are no errors indicated (there should be no underlined code, or anything showing on the warnings list or error list).

If there is no errors or warnings, then you can run the program.
If the program needs to be rebuilt, Visual Studio will do that automatically.

In one of the toolbars along the top, there is a [Start] button with a green triangle pointing right; that will build the program and then start the program running in the debugger.

Alternately, go to the menu bar and click on [Debug], and then click on the [Start Debugging] option.

Either way, the program should pop up running in a few moments. It can take 30+ seconds to finish building the first time.

The Dweeno Link program window should pop up, on top of the Visual Studio window.
Visual Studio also starts up some performance-monitoring stuff at the same time--little moving charts to show memory usage and so on.

You can stop the Dweeno Link program by either clicking on it's own [close] button in the top-right corner, or by clicking on the [Start] button again in Visual Studio, which will have now changed to a [Stop] button with a red square on it.

Doug83:
Okay, so your Dweeno Slave program has no errors and runs now.
Here is the instructions for how to use it.

1) Start the Dweeno Link program again, in the debugger (-for now).
2) Click the upper-left button that says ?Refresh serial ports?. This will cause all the available serial ports to be listed in the textbox immediately below.

Note: you may prefer to disconnect your Arduino when first pressing the ?Refresh serial ports? button, and then connect the Arduino and wait a couple seconds for it to be recognized, and then press the ?Refresh serial ports? button again. The new COM port will probably be the one that the Arduino is using.
...Or...
-you can just try sending Arduino messages to all the COM ports that show up.
I don't know for a fact that it is safe to do, or that it might harm anything.
I've tried it with my PC and nothing bad seemed to happen, but you are acting on your own there.

3) Then in the listbox, you click on the COM port that you want to use (the one that the Arduino is connected to). That same COM port will then appear in the textbox below, titled ?Selected port:?. This means that the selected COM port is the one that the SerialPortObject in the Dweeno Link program is set to use.

Sometimes when starting the PC program you also have to hit the [reset] button on the Arduino. I have not been able to pin down why this happens yet.

4) When you start Dweeno Link, the [Send Button] starts out disabled because there's no serial port set to send on. When you clicked into the listbox on a COM port, that also should have enabled the [Send Button]. Type your command in the ?Data to send? textbox, and press the [Send Button]. The [Send button] will turn gray, as it is disabled until a response from the Arduino comes.

5) If something goes wrong and you want to re-enable the Send button, then just click again on the selected COM port in the Listbox. That will enable the Send button again.

What commands you can send are shown below.

The blink-slow command:
100:7:*
 --- where 7 is the number of times you want it to blink. Blink time is 1 second on, 1 second off.
It will send the following message when it is finished:
100:*

The blink-fast command:
101:25:*
------where 25 is the number of times you want it to blink. Blink time is on-off, four times per second.
It will send the following message when it is finished:
101:*

To read the digital value of pin 3:
103:*
and it answers with the reply:
103:X:*
------where X is the value of the #3 pin, either zero or 1.

To read the analog value of pin A3:
104:*
and it answers with the reply:
104:X:*
------where X is the value of the pin, an integer from zero to 1023.

,,,,Of course, if you got nothing connected to either of those pins, then the result you will get is random.
But you could hook something suitable onto them to test, if you wanted to.

To digitalWrite to pin 4:
105:X:*
------where X is either zero or 1, that you want the pin set to.
and it answers with the reply:
105:*
------to confirm that it has completed setting the pin.

To analogWrite (PWM) to pin 5:
106:X:*
------where X is a number from zero to 255.
and it answers with the reply:
106:*
------to confirm that it has completed setting the pin.

,,,for the pin-writing commands, you could connect a LED and a 200 ohm resistor to show the pin's states.

arduinoTimeDelay(int t_Minutes, int t_Seconds)] ----- This function causes a time delay.
107:X:Y:*
------where X is the time in minutes to wait.
------Y is the time in seconds to wait.
The total time ends up being (minutes * 60) + seconds.
After the specified time interval has passed, the Arduino replies:
107:*

More info about the arduinoTimeDelay() function:
When a lot of people start using Visual Basic, they ask if there is a way to make it slow down or pause while doing something. There are different ways to do that, but--just like the Arduino's delay() function--the simplest and easiest ways have some serious drawbacks overall and are hardly ever used in the professional world.

This function allows the PC to pause sending more commands, by telling Arduino to pause instead. And the way that this function is written, it doesn't use the delay() function at all, so it does not interfere with any other tasks that the Arduino is supposed to be doing at the same time. For example: if you press one of the Arduino buttons when a the arduinoTimeDelay() function is running, the Arduino will still send the button message back to the PC.

Also there is the buttons:

Any time that button #1 is pressed, the Arduino will send the following message:
btn:1:*

Any time that button #2 is pressed, the Arduino will send the following message:
btn:2:*

Neither of the button commands can be called from the PC, they can only originate from the Arduino.
Both buttons share a one-second de-bounce timer. If you press either button, neither will work until 1 second of time has passed.

There is also a general error message.
If a function number that was sent cannot be matched with a function in the Arduino's sketch, then the commandNotFound() function is called and sends back the following message:
???:*
You can verify that this works by entering a non-existent command, such as 999:*

[EDIT]
This is the same list of commands given a couple posts above. Oh well.
Also a lot of my quotes changed to question marks. --So if you see question marks, those maybe are supposed to be quotes.
[EDIT END]

So now you may begin to see how this code can be modified into an automatic controller for the Arduino.
It doesn't do that right now, but it has the basic event handling in place to do that.
There is even some comments in the Visual Basic code, hinting where things need to be added.

All you need to add at this point is a way to store a list of desired Arduino commands, and then every time the [Send command] button gets re-enabled, that means you can process any data sent back from the Arduino and then send the next Arduino command.

Doug83:
If you run the program from within the Visual Studio program, then what you are really doing is running the source code files inside the Visual Studio debugger. It should work like that, but running the executable file will often work a bit faster and most people probably don't want to open Visual Studio every time just to use the Dweeno Link program anyway.

To get to the executable file, find the project folder that the project was saved into.
For Visual Studio it will be in your Documents folder in [Visual Studio 2017]--[Projects]--[(project name)]--[(project name)]--[bin]--[Debug]
The file has the project filename, and the .exe extension.
If you edited a project that you began in a previous version of Visual Studio, then it will be in the same place, but in the folder for that Visual Studio version instead (such as [Visual Studio 2016] or [Visual Studio 2015]).

When you run the program in the debugger it may work fine and not show any errors, but when you run the executable, the operating system may stop and say it has various errors.
This isn't greatly unusual.
Debuggers don't catch everything; Visual Studio works pretty well but debuggers are programs themselves, and they have bugs too.
The operating system will often catch run-time technical errors that the IDE ignored or missed.

Lastly: if you attempt to run this executable on another computer that does not have Visual Studio installed and it will not run, then you may need to also install a .NET Framework redistributable package from Microsoft. This was the case in past years but MS may have just included it into the normal Windows Updates nowadays.

------

At this point I'm going to wait a few days and see if anyone tries this, and if they have any problems.

The automatic control part can be added on to the Dweeno Link program with the existing controls in place.
I haven't done that yet myself, but it isn't difficult.

Doug83:
Before moving on to other more complicated things, I will add a couple more little features to what is done so far.

1. Add a combobox control to allow changing the connection speed.
2. Add a checkbox control to allow or prevent the [Send button] from disabling automatically until a response is received.

See the image below:



Drag the form's border and the two groupboxes out so you have more room, and arrange the existing controls like you see above.

Then:
1) Drag a new Label from the Toolbox onto the form, and change its Text property value to say Speed . Leave its Name as it is.

2) Drag a ComboBox from the Toolbox onto the form. Leave its Name as it is (ComboBox1). Change the DropDownStyle property to DropDownList.

3) Now, drag a CheckBox control onto the form, below the [Send button]. Change the Text property to say ?Keep send button enabled?. Leave its name as it is (CheckBox1).

Now you need to add some code, so switch to the code-view tab.

Near the top, there is a subroutine that begins with this line:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Create a new line inside that sub and add the lines below:

--- Code: ---ComboBox1.Items.Add(115200)
ComboBox1.Items.Add(57600)
ComboBox1.Items.Add(38400)
ComboBox1.Items.Add(28800)
ComboBox1.Items.Add(19200)
ComboBox1.Items.Add(14400)
ComboBox1.Items.Add(9600)
ComboBox1.SelectedIndex = 0

--- End code ---

Next, go back to the designer tab and double-click on the Listbox1 control. It will switch back to the code-view tab and put the cursor inside the sub that begins with the line:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged

In that sub there is a line that says MySerialPort.Open() .
Make an empty line ABOVE that statement, and then add the line below:

--- Code: ---MySerialPort.BaudRate = Convert.ToInt32(ComboBox1.SelectedItem.ToString)

--- End code ---

Next, go back to the form design tab, and double-click on the checkbox you made earlier.
Visual Studio will create a subroutine as below, but without any lines inside it (just the first and last lines).
You need to copy all inside lines below, and put them into your own sub.


--- Code: ---Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If ListBox1.SelectedIndex <> -1 Then
            If CheckBox1.CheckState = CheckState.Checked Then
                Send_Button.Enabled = True
            End If
        End If
    End Sub

--- End code ---

Next, go back to the form design tab again, and double-click on the [Send button].
Visual Studio will switch to the code view tab and put the cursor into the sub for that button.
You need to change your code so that it looks like what is below.


--- Code: ---Private Sub Send_Button_Click(sender As Object, e As EventArgs) Handles Send_Button.Click
        If MySerialPort.IsOpen() Then
            incomingData = ""
            outgoingData = Sending_TextBox.Text
            'If you wanted the ability to automatically progress through a list of commands,
            'then this is where you would start the process by sending the first command in the list of commands to send.
            SendOutgoingData()
            Receiving_TextBox.Text = ""
            If CheckBox1.CheckState = CheckState.Unchecked Then
                Send_Button.Enabled = False
            End If
        End If
    End Sub

--- End code ---

NOW,,,, if there is no errors, try to build/run the program again.

The connection speed combobox will default to the highest speed of 115200, but now you should be able to set it to slower speeds if you want.
The speed change does not take effect until you click into the listbox below however, on the COM port you want to use.

If you check the "Keep send button enabled" checkbox, then the Send button won't be disabled while waiting for an Arduino response.
And if the Send button is already disabled, checking the checkbox should re-enable it again--even if it was disabled because the PC was already waiting for an Arduino reponse.

Doug83:
In this stage we will add 20 user-editable buttons.
They can be customized by typing the desired settings directly, or by loading (or saving) already-existing settings from a text file. This way you can have as many different button config files as you want, and you can still use any of them easily.

Step 1: drag a TabControl control onto the form, and drag it out big as shown below. (I have moved the "Serial connection" groupbox to make more room).



Step 2: with the new TabControl highlighted, open the property that says TabPages.

Step 3: a dialog box that is titled "TabPage Collection Editor" should open. On the left side, highlight "TabPage1". On the right, find the property "Text" and change it to say "Buttons".



Step 4: On the left side again, highlight "TabPage2". On the right, find the property "Text" and change it to say "Button settings". Press the [OK] button to close that dialog. If you did it correctly, the two TabControl tabs on-screen should now say "Buttons" and "Button settings".

Step 5: Click on the Buttons tab on the TabControl, drag twenty buttons onto it and arrange them as shown below. Change each button's Name property by adding the letters "UP" onto the front--so that Button1 is renamed to UPButton1, and so on. Do that for all twenty buttons. You want them to be named UPButton_1 through UPButton_20 when you are done.

(-the Text properties of the buttons will still say Button1 through Button20, but that isn't important right now. You don't need to change that. Just rename the buttons-)



Step 6: drag twenty labels onto the form as shown above, placing each one at the upper-left corner of each button. Change each label's Text property to the same ending number as the button has (-the reason we are adding these labels is because the text on these buttons is going to be editable by changing text boxes on the other tab, and so you need some way other than the button's text to identify which button is which-).

Note: the buttons don't need to be in straight columns and rows, it was just easier to show the example that way. You can move them around later if you want. You just need to make sure you keep the correct text label next to its button.

Step 7: Now change to the Settings tab on the TabControl. Create a button on the Settings tab, and change its name to LoadUBF_Button. Change its text to say "Load user-set button file". ------- (For these next steps, see the next image further below)

Step 8: create another button and change its name to UPButtons_UseEdits_Button. Change its text to say "Post edits without saving file".

Step 9: Create another button and change its name to SaveUBF_Button. Change its text to say "Save user-set button file".

Step 10: Create a textbox under the left-side button and change its name to UBFpath_TextBox. Change the ReadOnly property to true.

Step 11: Next you need to create a series of textboxes and labels as shown below. The green overlaid text tells what the names of the textboxes must be changed to. When you are done you will have one series of textboxes named UBText_TextBox1 through UBText_TextBox20 and anther series named UBCom_TextBox1 through UBCom_TextBox20, arranged in two groups of two separate columns. The labels only need their text properties changed as you see, none of the labels needs to be renamed.



Step 12: In the ToolBox, open the Dialogs section and drag an OpenFileDialog control onto your form (drop it anywhere) and then drag a CloseFileDialog onto the form (also drop it anywhere). A new panel under the form display will appear, and these two controls will be displayed in there. By default their names will be OpenFileDialog1 and CloseFileDialog1, and just leave their names like that.

Step 13: copy everything from the PasteBin link below, and paste that over everything on the code-view tab of the Dweeno Link project.

https://pastebin.com/rsbZycia

There should be no errors showing at this point.
You can try running it now if you want, just to see that it does,,, but there's no button config file to use yet.

Step 14: the OpenFileDialog will be set to open in the My Documents folder.
Go into your My Documents folder and create another folder named dweeno_link. Then inside that folder, make another folder named button_configs. Inside that folder create a plain text file named user_set_buttons_01.txt
Copy the text from the pastebin link below into that text file and save it.

https://pastebin.com/c6j6EmxS

Each line of this file has three dividing vertical bars (|||, ASCII#124) that separate the file line into four sections.
The first part (button) and the second part (a number) is just indicating what button that line refers to.
The third section is the text that will be used on the button. (Note: no vertical bars allowed)
The fourth section is the actual command that the button will send out over the USB/serial port.

The last line of the text file is just the letters EOF, for an end-of-file marker.

In my example file, only the first three buttons have button text and commands assigned to them. The rest of them don't at the moment. If you don't need to use a button then you can just leave the last line section blank. The Dweeno Link program is set to skip sending it, and pop up a message box that says the button has no command to send.

You can edit the button configuration files directly if you want and then just re-load the button configuration file in Dweeno Link, but you can also edit and save them while in the Dweeno Link program.

HOW TO USE IT:

Now try running the Dweeno Link program again.

On the Settings tab, the left upper button [Load user-set button file] is for loading a button configuration file. Open the file in dweeno_link\button_configs\user_set_buttons_01.txt

The first three button textboxes should show text and a command in them, that sends to the Arduino when they are clicked on. The rest of the buttons aren't set, so they won't do anything yet.

The right upper button [Save user-set button file] does two things. Whatever button text and commands are in all those text boxes gets saved as the named button file, and the button text get re-copied to the actual command buttons so that the command buttons say what they're supposed to.

The center button  [Post edits without saving file] is for using changes without saving a file: if you want to edit any buttons but don't want to save those edits as a file, then clicking on [Post edits without saving file] copies the button text from the Settings tab into the command buttons themselves--but DOES NOT save the file.

Whenever you click on a button on the first tab, it will send whatever serial message is in its corresponding Button command textbox on the second tab.

Navigation

[0] Message Index

[*] Previous page

Go to full version