RS232 IO Ansteuerung UDF

  • Hallo an alle,
    Ich bin neu hier, und hab auch gleich ne UDF!
    Hab ziemlich lange daran gesessen.
    Damit könnt ihr die Ausgänge der RS232 Schnittstelle high oder low schalten
    und die Eingänge abfragen.
    Freu mich auf euer Feedback oder Kritik! :)
    Wenn ihr Fehler findet dann bitte posten.

    [autoit]

    #include-once

    [/autoit][autoit][/autoit][autoit]

    ; #INDEX# =======================================================================================================================
    ; Title .........: SerialPort
    ; AutoIt Version : 3.6.1
    ; Language ......: English
    ; Description ...: Functions to set and read RS232-LINES
    ; Author(s) .....: Julian Bohn (PLFire)
    ; Dll(s) ........: kernel32.dll
    ; ===============================================================================================================================

    [/autoit][autoit][/autoit][autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _SP_PortOpen
    ; Description ...: Opens a RS232-Port.
    ; Syntax.........: _SP_PortOpen($port)
    ; Parameters ....: $port - The COM-PORT ID
    ; |COM1 - The first COM-PORT
    ; |COM2 - The second COM-PORT
    ; |COM3 - The third COM-PORT
    ; |COM4 - The fourth COM-PORT
    ; Return values .: Failure - -1
    ; Author ........: Julian Bohn (PLFire)
    ; Modified.......:
    ; Remarks .......:
    ; Related .......:
    ; Link ..........:
    ; Example .......: No
    ; ===============================================================================================================================
    Func _SP_PortOpen($port)
    $port_open = DllCall("kernel32.dll", "hwnd", "CreateFile", "str", "\\.\" & $port, "int", 0xC0000000, "int", 0, "ptr", 0, "int", 3, "int", 0, "int", 0)
    $PORT_OPEN_FC=$port_open
    DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 6)
    DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 4)
    DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 9)
    EndFunc

    [/autoit][autoit][/autoit][autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _SP_PortClose
    ; Description ...: Close a RS232-Port.
    ; Syntax.........: _SP_PortClose()
    ; Parameters ....:
    ; Return values .: Failure - -1
    ; Author ........: Julian Bohn (PLFire)
    ; Modified.......:
    ; Remarks .......:
    ; Related .......:
    ; Link ..........:
    ; Example .......: No
    ; ===============================================================================================================================
    Func _SP_PortClose()
    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $PORT_OPEN_FC[0])
    EndFunc

    [/autoit][autoit][/autoit][autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _SP_SetOutput
    ; Description ...: Sets a RS232-LINE high or low.
    ; Syntax.........: _SP_SetOutput($pin, $state)
    ; Parameters ....: $pin - The RS232-LINE to set
    ; |DTR - DATA TRANSMIT READY - LINE
    ; |RTS - READY TO SEND - LINE
    ; |TXD - TRANSMIT DATA - LINE
    ; $state - HIGH (+12VDC) or LOW (-12VDC)
    ; |1 - Sets the RS232-LINE high
    ; |0 - Sets the RS232-LINE low
    ; Return values .: Failure - -1
    ; Author ........: Julian Bohn (PLFire)
    ; Modified.......:
    ; Remarks .......:
    ; Related .......:
    ; Link ..........:
    ; Example .......: No
    ; ===============================================================================================================================
    Func _SP_SetOutput($pin, $state)
    If $pin="dtr" Then
    If $state=1 Then
    DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 5) ;DTR
    ElseIf $state=0 Then
    DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 6) ;DTR
    Else
    Beep (800, 500)
    MsgBox(0, "ERROR", "WRONG PARAMETERS", 10)
    Exit
    EndIf
    ElseIf $pin="rts" Then
    If $state=1 Then
    DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 3) ;RTS
    ElseIf $state=0 Then
    DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 4) ;RTS
    Else
    Beep (800, 500)
    MsgBox(0, "ERROR", "WRONG PARAMETERS", 10)
    Exit
    EndIf
    ElseIf $pin="txd" Then
    If $state=1 Then
    DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 8) ;TXD
    ElseIf $state=0 Then
    DllCall("kernel32.dll", "long", "EscapeCommFunction", "hwnd", $PORT_OPEN_FC[0], "long", 9) ;TXD
    Else
    Beep (800, 500)
    MsgBox(0, "ERROR", "WRONG PARAMETERS", 10)
    Exit
    EndIf
    Else
    Beep (800, 500)
    MsgBox(0, "ERROR", "WRONG PARAMETERS", 10)
    Exit
    EndIf
    EndFunc

    [/autoit][autoit][/autoit][autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _SP_ReadInput
    ; Description ...: Reads the state of a RS232-LINE.
    ; Syntax.........: _SP_ReadInput($pin)
    ; Parameters ....: $pin - The RS232-LINE to read
    ; |CTS - CLEAR TO SEND - LINE
    ; |DSR - DATA SET READY - LINE
    ; |RI - RING INDICATOR - LINE
    ; |DCD - DATA CARRIER DETECT - LINE
    ; Return values .: Success - The state of the RS232-LINE
    ; Failure - -1
    ; Author ........: Julian Bohn (PLFire)
    ; Modified.......:
    ; Remarks .......:
    ; Related .......:
    ; Link ..........:
    ; Example .......: No
    ; ===============================================================================================================================
    Func _SP_ReadInput($pin)
    Local $port_stat = DllStructCreate("DWORD")
    DllCall("kernel32.dll", "long", "GetCommModemStatus", "HWND", $PORT_OPEN_FC[0], "Ptr", DllStructGetPtr($port_stat))
    If $pin="cts" Then
    If BitAND(DllStructGetData($port_stat, 1), 0x10) Then ;CTS
    Return 1
    Else
    Return 0
    EndIf
    ElseIf $pin="dsr" Then
    If BitAND(DllStructGetData($port_stat, 1), 0x20) Then ;DSR
    Return 1
    Else
    Return 0
    EndIf
    ElseIf $pin="ri" Then
    If BitAND(DllStructGetData($port_stat, 1), 0x40) Then ;RI
    Return 1
    Else
    Return 0
    EndIf
    ElseIf $pin="dcd" Then
    If BitAND(DllStructGetData($port_stat, 1), 0x80) Then ;DCD
    Return 1
    Else
    Return 0
    EndIf
    Else
    Beep (800, 500)
    MsgBox(0, "ERROR", "WRONG PARAMETERS", 10)
    Exit
    EndIf
    EndFunc

    [/autoit]
  • Hallo und herzlich willkommen hier bei AutoIt.de!
    Danke für deine UDF, irgendwo glaube ich habe ich schon mal sowas gesehen. Bin mir fast sicher.

    Müsste man die Variable $PORT_OPEN_FC nicht irgendwo global deklarieren, oder besser noch das Port-Handle allen Funktionen mitgeben? Dann könnten auch mehrere Ports gleichzeitig geöffnet werden.

  • Warum beendest du denn die Funktionen bei einem Fehler mit Exit?
    Normalerweise macht man das mit Return SetError(1, 0, -1).
    Ich frage mich auch warum du überall schreibst, dass man bei einem Fehler eine -1 bekommt. Aber nirgends ist ein Return -1 oder ein Return SetError(1, 0, -1) zu sehen.