Click here to Skip to main content
15,895,799 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Concurrency violation or Insert Pin
Eddy Vluggen28-Mar-13 7:15
professionalEddy Vluggen28-Mar-13 7:15 
QuestionHow make steganography in video (.AVI) Pin
Khairul Imam25-Mar-13 11:42
Khairul Imam25-Mar-13 11:42 
AnswerRe: How make steganography in video (.AVI) Pin
GuyThiebaut25-Mar-13 22:45
professionalGuyThiebaut25-Mar-13 22:45 
QuestionI want to do all the functionalty of painting on an image?? Pin
Lolo Hyun25-Mar-13 6:55
Lolo Hyun25-Mar-13 6:55 
AnswerRe: I want to do all the functionalty of painting on an image?? Pin
Richard MacCutchan25-Mar-13 7:39
mveRichard MacCutchan25-Mar-13 7:39 
AnswerRe: I want to do all the functionalty of painting on an image?? Pin
Eddy Vluggen25-Mar-13 8:06
professionalEddy Vluggen25-Mar-13 8:06 
Generalkeyboard controlling arduino rc car with UDP packets (VB 2008) Pin
Jose Sallamanca20-Mar-13 8:59
Jose Sallamanca20-Mar-13 8:59 
AnswerRe: keyboard controlling arduino rc car with UDP packets (VB 2008) Pin
MicroVirus27-Mar-13 4:11
MicroVirus27-Mar-13 4:11 
Hey there,

Interesting project you have here Smile | :) Must be a lot of fun to work on. I see this post was from a week back, so likely you have already figured this out for yourself, but for what it's worth I'll reply.
I added a few remarks/comments to your code; I'm not going to nitpick on the finer details, as most of the code looks good! I did notice that you are using the Select Case in an odd way and it's probably best to just make an If-Then-ElseIf statement of it.

Jose Sallamanca wrote:
Anyway, the thing is working, and sending packets only on initial press or release of key(s). The one glitch I've so far detected is that when releasing all the keys, which should send the "z" character and stop the car, the same character that was previously sent is re-sent, rather than the 'stop' character "z".

 

So here's the code, VB 2008:

 



 

VB
 
    Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
        Tmr.Stop()
' You don't need to stop the timer and then restart it. Just let it continue running (or did you find a specific need to restart it?)
 
        If Not controlChoice = controlChoicePrev Then
 
            'UDP_______________________________________________________

' I see here that you are constantly (every timer tick!) rebinding the udpClient to the target. Luckily, it's UDP so this is not disastrous, but it is
' a bit unnecessary. It's probably a better idea to relocate the GLOIP = .. GLOINTPORT = .. udpClient.Connect(..) to your Form1_Load event handler.
' Also, see my comment 1) at the end for a cautionary tip, which applies when you are running a 32 bits program under 64 bits windows.

' === BLOCK 1 ===
            Try
' ------- START of the MOVE (when moved, enclose both in Try-Catch and handle the errors appropriately) -------
                GLOIP = IPAddress.Parse("192.168.1.177")
                GLOINTPORT = 8888
                udpClient.Connect(GLOIP, GLOINTPORT)
' ------- END of the MOVE -------
                bytCommand = Encoding.ASCII.GetBytes(cc)
                udpClient.Send(bytCommand, bytCommand.Length)
 
            Catch ex As Exception
                Console.WriteLine(ex.Message)
 
            End Try
 
            'UDP_______________________________________________________

        End If
 
' === END BLOCK 1 ===
' === BLOCK 2 ===
        If GetAsyncKeyState(Keys.Up) Or GetAsyncKeyState(Keys.Down) Then
 

            Select Case True
 
                Case GetAsyncKeyState(Keys.Up)
 
                    If (GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Left)) Then
                        Label1.Text = "Forwards Left"
                        controlChoicePrev = controlChoice
                        controlChoice = 11
                        cc = "b"
 
                    ElseIf (GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Right)) Then
                        Label1.Text = "Forwards Right"
                        controlChoicePrev = controlChoice
                        controlChoice = 12
                        cc = "c"
 
                    Else
                        Label1.Text = "Forwards"
                        controlChoicePrev = controlChoice
                        controlChoice = 10
                        cc = "a"
 
                    End If
 

                Case GetAsyncKeyState(Keys.Down)
 

 

                    If (GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Left)) Then
                        Label1.Text = "Backwards Left"
                        controlChoicePrev = controlChoice
                        controlChoice = 21
                        cc = "e"
 
                    ElseIf (GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Right)) Then
                        Label1.Text = "Backwards Right"
                        controlChoicePrev = controlChoice
                        controlChoice = 22
                        cc = "f"
 
                    Else
                        Label1.Text = "Backwards"
                        controlChoicePrev = controlChoice
                        controlChoice = 20
                        cc = "d"
 
                    End If
 
            End Select
 
        Else
            Label1.Text = "Stopped"
            controlChoicePrev = controlChoice
            controlChoice = 0
' Here is why the STOP command is not working: you forgot to set cc to "z".
 
        End If
 
' === END BLOCK 2 ===
        Tmr.Start()
 
    End Sub
End Class


My first comment 1) only applies if you are using Win64 and running/coding a 32 bits application (typical). There is a major bug in windows which prevents exceptions being thrown from the Load event to be caught by your program or the debugger - the exceptions are silently thrown away and execution continues with the hope that the exception isn't too serious. If you're interested in reading about it, here's a link to the article I found after many frustrations with this problem: http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/[^].
For this reason, it might actually be a good idea to place the majority of the code in the Load event in 'Sub New' (the constructor) instead. When you create a new function called Sub New in the IDE, by just typing in the Form code Sub New<enter> Visual Basic will automatically generate the following code for you:
VB
    Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
            ADD THE LOAD CODE HERE
End Sub

This is the best place to put such code and still have exceptions work (actually, it's always the best place to initialise members).

Secondly, the way BLOCK 1 and BLOCK 2 are positioned is that at the first tick it is detected that a key is pressed but only at the second tick is the change transmitted, so you are manually introducing a 100 ms lag.
If you swap the positions of BLOCK 1 and BLOCK 2 then the code first checks the key states and then immediately transmits.

Finally, UDP is not a reliable protocol by design, so it might be a good idea to send the controlChoice even if it is equal to the controlChoicePrev every couple of ticks (like, every half second/5 ticks) to mitigate the results of package loss, which can occur with UDP.

Hope this helps you, if only a bit. Good luck.
Best Regards,

Richard

Questionremove or disconnect disk/sotrage Pin
V1rus9r20-Mar-13 3:51
V1rus9r20-Mar-13 3:51 
AnswerRe: remove or disconnect disk/sotrage Pin
Richard MacCutchan20-Mar-13 4:04
mveRichard MacCutchan20-Mar-13 4:04 
GeneralRe: remove or disconnect disk/sotrage Pin
V1rus9r20-Mar-13 6:40
V1rus9r20-Mar-13 6:40 
QuestionAccess VBA Timing Problem Pin
Ingo19-Mar-13 2:12
Ingo19-Mar-13 2:12 
AnswerRe: Access VBA Timing Problem Pin
Dave Kreskowiak19-Mar-13 3:37
mveDave Kreskowiak19-Mar-13 3:37 
GeneralRe: Access VBA Timing Problem Pin
Ingo19-Mar-13 4:41
Ingo19-Mar-13 4:41 
GeneralRe: Access VBA Timing Problem Pin
Dave Kreskowiak19-Mar-13 7:18
mveDave Kreskowiak19-Mar-13 7:18 
GeneralRe: Access VBA Timing Problem Pin
Ingo19-Mar-13 7:25
Ingo19-Mar-13 7:25 
GeneralRe: Access VBA Timing Problem Pin
Dave Kreskowiak19-Mar-13 8:10
mveDave Kreskowiak19-Mar-13 8:10 
GeneralRe: Access VBA Timing Problem Pin
Ingo20-Mar-13 0:03
Ingo20-Mar-13 0:03 
AnswerRe: Access VBA Timing Problem Pin
GuyThiebaut19-Mar-13 5:04
professionalGuyThiebaut19-Mar-13 5:04 
GeneralRe: Access VBA Timing Problem Pin
Ingo19-Mar-13 6:49
Ingo19-Mar-13 6:49 
GeneralRe: Access VBA Timing Problem Pin
GuyThiebaut19-Mar-13 7:21
professionalGuyThiebaut19-Mar-13 7:21 
GeneralRe: Access VBA Timing Problem Pin
Ingo19-Mar-13 7:28
Ingo19-Mar-13 7:28 
GeneralRe: Access VBA Timing Problem Pin
Dave Kreskowiak19-Mar-13 8:09
mveDave Kreskowiak19-Mar-13 8:09 
AnswerRe: Access VBA Timing Problem Pin
Maciej Los19-Mar-13 6:54
mveMaciej Los19-Mar-13 6:54 
GeneralRe: Access VBA Timing Problem Pin
Ingo19-Mar-13 7:15
Ingo19-Mar-13 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.