Dobrý den, vytvořil jsem si jednoduchý prográmek pro komunikaci s COM portem a mám problém s počítáním řádků v textboxu. Chci, aby mi přičetl +1 do lablu při každém přidaném řádku. Prosím o pomoc. Zde je můj kod
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Imports System.Windows.Forms
Public Class frmMain
Dim myPort As Array 'COM Ports +detected on the system will be stored here
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
Dim bytes() As Byte = {&H2A, &H61, &H0, &H5, &HFE, &H2, &H60, &HF, &HD}
Dim buffer As String
Dim count As Integer
Dim enc = New System.Text.UTF8Encoding
Dim currentLineIndex As Integer = 0
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
cmbBaud.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)
For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list
btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup
SerialPort1.BaudRate = cmbBaud.Text 'Set Baud rate to the selected value on
'Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8 'Open our serial port
Try
SerialPort1.Open()
Catch
MessageBox.Show("Přístup ke COM portu byl odepřen")
End Try
btnConnect.Enabled = False 'Disable Connect button
btnDisconnect.Enabled = True 'and Enable Disconnect button
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close() 'Close our Serial Port
btnConnect.Enabled = True
btnDisconnect.Enabled = False
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Try
SerialPort1.Write(bytes, 0, bytes.Length)
Catch
MessageBox.Show("Port je zavřený")
End Try
'SerialPort1.Write(txtTransmit.Text & vbCr) 'The text contained in the txtText will be sent to the serial port as ascii
'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
Dim bytelenght As Integer = SerialPort1.BytesToRead
Dim test As String
Dim testpole(bytelenght - 1) As Byte
SerialPort1.Read(testpole, 0, bytelenght)
test = BitConverter.ToString(testpole, 0)
' test = test.Replace("-", "")
ReceivedText(test)
Me.rtbReceived.Text = String.Format("{0}", vbCrLf & (currentLineIndex + 1).ToString())
Label1.Text = rtbReceived.Lines.Count.ToString
End Sub
Private Sub ReceivedText(ByVal [text] As String)
'compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text] + vbLf
End If
End Sub
Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text 'pop a message box to user if he is changing ports
Else 'without disconnecting first.
MsgBox("Uzavřete port", vbCritical)
End If
End Sub
Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text 'pop a message box to user if he is changing baud rate
Else 'without disconnecting first.
MsgBox("Uzavřete port", vbCritical)
End If
Nejlepší by bylo synchronizovat program s ryanem , ale to už je nad moje síly. Potřebuji automaticky počítat řádky. Díky za pomoc Ryanova stránka http://ryanfarley.com/blog/archive/2004/...
|