Forum: PC-Programmierung ComPort empfangen - VisualBasic express


von EinSteigÄr (Gast)


Lesenswert?

Hallo liebe Leute!!


Ich möchte nun ein kleines Programm in Visual Basic 2005 express 
schreiben, welches die COM3-Schnittstelle liest (von meinem µC gesendet, 
Baudrate 9600, 8 Datenbits, keine Parität, 1 Stoppbit), und diese Daten 
in ein Label/TextBox schreiben soll.

Könntet ihr mir helfen? Habe kaum Ahnung von Visual Basic 2005.

Vielen Dank!

von Dirk (Gast)


Lesenswert?

Wie waere es die Suche zu bedienen?
1
Imports System.IO.Ports
2
3
4
Public Delegate Sub rxDataDelegate()
5
6
Public Class Form1
7
    Dim WithEvents SerialPort As New SerialPort
8
    Dim rev(80) As Byte
9
    Dim cnt_index As Byte
10
11
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
12
13
        If SerialPort.IsOpen Then
14
            SerialPort.Close()
15
        End If
16
17
        Try
18
            With SerialPort
19
                .PortName = "Com4"
20
                .BaudRate = 9600
21
                .Parity = Parity.None
22
                .DataBits = 8
23
                .StopBits = StopBits.One
24
            End With
25
            SerialPort.Open()
26
        Catch ex As Exception
27
            MsgBox(ex.ToString)
28
        End Try
29
30
31
    End Sub
32
33
34
    REM Private Sub com1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles com1.DataReceived
35
    REM Dim returnStr As String
36
    REM returnStr = com1.ReadLine
37
    REM ReceiveSerialData(returnStr)
38
    REM End Sub
39
40
41
42
    Sub SendSerialData(ByVal data As String)
43
        ' Send strings to a serial port.
44
        SerialPort.WriteLine(data)
45
    End Sub
46
47
48
    Private Sub ReceiveSerialData()
49
        'TextBox1.AppendText(SerialPort.ReadLine)
50
        rev(cnt_index) = SerialPort.ReadByte
51
        ListBox1.Items.Add(rev(cnt_index))
52
        ListBox1.Items.Add(cnt_index)
53
        cnt_index = cnt_index + 1
54
    End Sub
55
56
57
    Private Sub serialport_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived, SerialPort1.DataReceived
58
        TextBox1.Invoke(New rxDataDelegate(AddressOf ReceiveSerialData), New Object() {})
59
    End Sub
60
61
62
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
63
        'SendSerialData("TestString")
64
        SendSerialData("hhhh")
65
    End Sub
66
67
   
68
    Private Sub GetSerialPortNames()
69
        ' Show all available COM ports.
70
        For Each sp As String In My.Computer.Ports.SerialPortNames
71
            ListBox1.Items.Add(sp)
72
        Next
73
    End Sub
74
75
76
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
77
        GetSerialPortNames()
78
    End Sub
79
80
81
    Private Sub SetuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetuToolStripMenuItem.Click
82
        GetSerialPortNames()
83
    End Sub
84
End Class

von EinSteigÄr (Gast)


Lesenswert?

ok hat geklappt danke :)

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.