Click here to Skip to main content
16,019,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Dim keysPressed As New HashSet(Of Keys)

    

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With SerialPort1
            .Close()
            .PortName = "COM10"
            .BaudRate = 9600
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = StopBits.One
            .DtrEnable = True
            .RtsEnable = True
            .ReceivedBytesThreshold = 1
            .Open()
        End With
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        keysPressed.Add(e.KeyCode)

        If keysPressed.Contains(Keys.Left) AndAlso keysPressed.Contains(Keys.Right) Then
            SerialPort1.Write("Q")
            Label1.Text = "Мирување"
            Label1.Text = "Мирување"
        ElseIf keysPressed.Contains(Keys.Left) Then
            SerialPort1.Write("L")
            Label1.Text = "Лево"

        ElseIf keysPressed.Contains(Keys.Right) Then
            SerialPort1.Write("D")
            Label1.Text = "Десно"
        End If


        If keysPressed.Contains(Keys.Up) AndAlso keysPressed.Contains(Keys.Down) Then
            SerialPort1.Write("E")
            Label1.Text = "Мирување"
            Label1.Text = "Мирување"
        ElseIf keysPressed.Contains(Keys.Up) Then
            SerialPort1.Write("N")
            Label1.Text = "Напред"
        ElseIf keysPressed.Contains(Keys.Down) Then
            SerialPort1.Write("S")
            Label1.Text = "Назад"
        End If

        Refresh()

    End Sub

    Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        Select Case e.KeyCode
            Case Keys.Left
                SerialPort1.Write("Q")
                Label1.Text = "Мирување"
                keysPressed.Clear()
            Case Keys.Right
                SerialPort1.Write("W")
                Label1.Text = "Мирување"
                keysPressed.Clear()
            Case Keys.Up
                SerialPort1.Write("E")
                Label1.Text = "Мирување"
                keysPressed.Clear()
            Case Keys.Down
                SerialPort1.Write("R")
                Label1.Text = "Мирување"
                keysPressed.Clear()
        End Select
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class


and here are the project files
http://www.mediafire.com/download/hpc9btpsyz27y5c/Project+files.rar</a>[^]
Posted
Comments
OriginalGriff 27-Oct-13 4:57am    
And?
You haven't told us what problem you are having, or what you have tried, or where you are stuck.
What help do you need from us?

1 solution

First add a combobox to your Form

In your Form Load method add

VB
ComboBox1.DataSource = SerialPort.GetPortNames()


GetPortNames() returns all the ports available on your machine.


For baud rate and other info on the port select you can find add this to your Form Load method
VB
ComboBox1.DataSource = SerialPort.GetPortNames()

Dim sport As New SerialPort(ComboBox1.Text)
TextBox1.Text = sport.BaudRate


and further to the combo box index changed event method you can add the below

VB
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim sport As New SerialPort(ComboBox1.Text)
    TextBox1.Text = sport.BaudRate
End Sub


hope this helps.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900