Serielle Schnittstelle bedienen mit VisualBasicExpress2008

Hahnus

Level-2
Beiträge
55
Reaktionspunkte
2
Zuviel Werbung?
-> Hier kostenlos registrieren
Ich möchte mit einer kleinen Windows-Anwendung (Visual Basic Express 2008 ) die Daten eines Scanners (hängt an der seriellen Schnittstelle) empfangen und in eine Textdatei schreiben.
Das funktioniert prinzipiell auch ganz gut. Um zu erkennen ob die serielle Schnittstelle Daten empfangen hat, habe ich folgende Sub programmiert:

Code:
Private Sub LILO(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SerialPort1.DataReceived
        If btnTEST.Enabled = True Then
            btnTEST.Enabled = False
        Else
            btnTEST.Enabled = True

        End If

    End Sub
Wenn nun bei Datenempfang diese angesprungen wird, erhalte ich einen Fehler:

"Ungültiger threadübergreifender Vorgang: Der Zugriff auf das Steuerelement btnTEST erfolgte von einem anderen Thread als dem Thread, für den es erstellt wurde"

Was muss ich hier noch definieren/ deklarieren?

Alles läuft in einem Windows-Formular, d.h. eigentlich sollte der Button Test (btnTEST) der Sub "LILO" bekannt sein und sich im gleichen Thread befinden?

Danke für Eure Unterstützung + Gruß
Hahnus
 
...doch ein eigener Thread

Hi,

versuch's mal so:

Code:
[SIZE=2][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DataReceived( _[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], _[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.IO.Ports.SerialDataReceivedEventArgs) _[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]   Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] serialPort.DataReceived[/SIZE]
[SIZE=2] txtDataReceived.Invoke([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] _[/SIZE]
[SIZE=2] myDelegate([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] updateTextBox), _[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]() {})[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/COLOR][/SIZE][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Delegate[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] myDelegate()[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] updateTextBox()[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] With[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] txtDataReceived[/SIZE]
[SIZE=2]   .Font = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Font([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Garamond"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 12.0!, FontStyle.Bold)[/SIZE]
[SIZE=2]   .SelectionColor = Color.Red[/SIZE]
[SIZE=2]   .AppendText(serialPort.ReadExisting)[/SIZE]
[SIZE=2]   .ScrollToCaret()[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/COLOR][/SIZE]

Gruß
Stefan
 
Zurück
Oben