rs232 kann man in .net einfach als "Variable" definieren
z.B.
Code:
Dim FLASHER5_SerialPort() As SerialPort
Dim x As Int16
ReDim Preserve FLASHER5_SerialPort(0 To PRG_FLASHER5.GetUpperBound(0))
For x = 0 To PRG_FLASHER5.GetUpperBound(0)
If FLASHER5_SerialPort(x) Is Nothing Then ' Wenn Anschluss schon instanziert
FLASHER5_SerialPort(x) = New SerialPort(PRG_FLASHER5(x).PORT, 115200, Parity.Odd, 8, StopBits.One)
FLASHER5_SerialPort(x).Handshake = Handshake.None
FLASHER5_SerialPort(x).ReadBufferSize = 4096
FLASHER5_SerialPort(x).WriteBufferSize = 2024
FLASHER5_SerialPort(x).ReadTimeout = -1
FLASHER5_SerialPort(x).WriteTimeout = -1
End If
......
Public Function open(ByVal ProgIndex As Int16) As Boolean
Dim counter As Int16 = 0
With FLASHER5_SerialPort(ProgIndex)
Do Until .IsOpen
DoEvents()
Try
.Open()
Catch
If counter > 100 Then Return False
counter += 1
End Try
Loop
Return True
End With
End Function
Public Function closeAll() As Boolean
Dim x As Int16
For x = 0 To PRG_FLASHER5.GetUpperBound(0)
Dim counter As Int16 = 0
With FLASHER5_SerialPort(x)
Do Until Not (.IsOpen)
DoEvents()
Try
.Close()
Catch
If counter > 100 Then
FLASHER5_MESSAGES.Enqueue("could not close " & .PortName)
Return False
End If
counter += 1
End Try
Loop
End With
Next x
Return True
End Function
Public Function CmdSend(ByVal stringg As String, ByVal ProgIndex As Int16) As String
With FLASHER5_SerialPort(ProgIndex)
If .IsOpen Then
.WriteLine(stringg & vbCrLf)
'x = Chr(13)
Do While .BytesToWrite > 0
DoEvents()
Loop
Else
FLASHER5_MESSAGES.Enqueue(.PortName & " not open")
Exit Function
End If
Dim x As String = ""
Dim temp() As String
Dim counter As Int16 = 0
Do
Try
DoEvents()
System.Threading.Thread.Sleep(20)
x &= .ReadExisting
If x.Length < 1 Then counter += 1
If counter > 100 Then Return "timeout"
Catch ex As Exception
FLASHER5_MESSAGES.Enqueue(ex.Message)
Return ex.Message
End Try
If x.StartsWith("#NACK" & vbCr) Then Return "failure"
Loop Until x.StartsWith("#ACK" & vbCr & "#") And x.EndsWith(vbCr)
temp = x.Split("#")
x = temp(2).TrimEnd(vbCr)
Return x
End With
End Function
Next x
einfach maln bisschen Codefutter
wenn ihr Fragen zu .net habt kann ich dienen...
Lesezeichen