1. Dashboard
  2. Mitglieder
    1. Letzte Aktivitäten
    2. Benutzer online
    3. Team
    4. Mitgliedersuche
  3. Forenregeln
  4. Forum
    1. Unerledigte Themen
  • Anmelden
  • Registrieren
  • Suche
Alles
  • Alles
  • Artikel
  • Seiten
  • Forum
  • Erweiterte Suche
  1. AutoIt.de - Das deutschsprachige Forum.
  2. Mitglieder
  3. veronesi

Beiträge von veronesi

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 17:55

    Danke für den Tipp. Ich muss das morgen oder übermorgen testen.
    Das DCD kennst Du vielleicht als DTR oder DCR.
    Das ist eigentlich auch das RTS aber auf der EMPFÄNGERSEITE!

    Hast Du eine Idee, wie man dessen Status abfragt?
    Lg und bis morgen oder übermorgen!

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 17:44

    Aaaalso ;)
    Mit der UDF, welche ich gepostet habe, klappt nun das serielle senden und empfangen "ohne" Windows fremde Dll.
    Auch das RTS setzen kann ich.

    Aber ich möchte noch folgendes:
    - DCD Status abfragen
    - RTS Status setzen - unabhängig von der Portinitialisierung (Klar, der Port muss mal geöffnet und initialisiert sein. Aber ich möchte ohne den Port zu schliessen das RTS zurücksetzen können!)

    Danke für Hilfe! Ich poste anschliessend die fertige UDF für alle!
    Das war immer wieder ein Thema!

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 16:16

    Bzw. vielleicht musst du besser _HexToString() anstelle von String() verwenden!

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 16:04

    Hast Du es mal mit so etwas versucht:

    [autoit]

    #include "Func_Serial.au3"

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

    ;Alle Konstanten müssen angepasst werden!
    ;Gemäss Spezifikationen der Karte!
    Global Const $ComPort = 3
    Global Const $Baudrate = 9600
    Global Const $Bits = 8
    Global Const $Parity = 0
    Global Const $Stop = 0
    Global Const $Rts = 0

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

    Global Const $Byte1 = 0x01
    Global Const $Byte2 = 0x02
    Global Const $Byte3 = 0xFF
    Global Const $Byte4 = BitXOR($Byte1, $Byte2, $Byte3)

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

    Local $SendPort = _OpenCOMPort($ComPort, $Baudrate, $Bits, $Parity, $Stop, $Rts)
    If $SendPort = -1 Then MsgBox(0, "Fehler", "Der sendende COM-Port konnte nicht geöffnet werden!")

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

    _SendSerialString($SendPort, String($Byte1 & $Byte2 & $Byte3 & $Byte4))
    Sleep(100)
    _CloseCOMPort($SendPort)

    [/autoit]

    Dazu natürlich diese UDF:

    Spoiler anzeigen
    [autoit]

    #cs
    UDF cfx.au3
    serial functions using kernel32.dll
    V1.0
    Uwe Lahni 2008
    V2.0
    Andrew Calcutt 05/16/2009 - Started converting to UDF
    V2.1
    Mikko Keski-Heroja 02/23/2011 - UDF is now compatible with Opt("MustDeclareVars",1) and Date.au3. Global variable $dll is renamed to $commDll.
    V2.2
    Veronesi 04/26/2011 - Changed some cosmetics and documentation
    #ce
    #include-once
    Global $commDll
    Global $hSerialPort
    Global $dcb_Struct
    Global $commtimeout
    Global $commtimeout_Struct
    Global $commState
    ;~ Global $commState_Struct
    ;~ Global Const $STX = Chr(2)
    ;~ Global Const $ETX = Chr(3)
    ;~ Global Const $EOT = Chr(4)
    ;~ Global Const $ENQ = Chr(5)
    ;~ Global Const $ACK = Chr(6)
    ;~ Const $NAK = Chr(15)
    ;~ Const $DLE = Chr(16)

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

    ;====================================================================================
    ; Function Name: _OpenCOMPort($CommPort, $CommBaud, $CommBits, $CommParity, $CommStop, $SetRTS)
    ; Description: Opens serial port
    ; Parameters: $CommPort
    ; $CommBits - 4-8
    ; $CommParity - 0=none, 1=odd, 2=even, 3=mark, 4=space
    ; $CommStop - 0,1,5,2
    ; $SetRTS - 1 = Set it / 0 = Don't set it
    ; Returns: on success, returns serial port id?
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _OpenCOMPort($CommPort, $CommBaud = '9600', $CommBits = '8', $CommParity = '0', $CommStop = '0', $SetRTS = 0)
    #cs
    CommCtrl => fBitfields => http://msdn.microsoft.com/en-us/library/…4(v=vs.85).aspx
    http://www.hpcc.ecs.soton.ac.uk/software/Win32API.Txt
    ' The fourteen actual DCB bit-sized data fields within the four bytes of fBitFields can be manipulated by bitwise logical And/Or operations.
    ' FieldName Bit # Description
    ' ----------------- ----- ------------------------------
    ' fBinary 1 binary mode, no EOF check
    ' fParity 2 enable parity checking
    ' fOutxCtsFlow 3 CTS output flow control
    ' fOutxDsrFlow 4 DSR output flow control
    ' fDtrControl 5 DTR flow control type (2 bits)
    ' fDsrSensitivity 7 DSR sensitivity
    ' fTXContinueOnXoff 8 XOFF continues Tx
    ' fOutX 9 XON/XOFF out flow control
    ' fInX 10 XON/XOFF in flow control
    ' fErrorChar 11 enable error replacement
    ' fNull 12 enable null stripping
    ' fRtsControl 13 RTS flow control (2 bits)
    ' fAbortOnError 15 abort reads/writes on error
    ' fDummy2 16 reserved
    #ce
    Local $CommCtrl = 0x1
    If $SetRTS = 1 Then $CommCtrl = BitOR($CommCtrl, 0x1001)
    $commDll = DllOpen("kernel32.dll")
    Local $dcbs = "long DCBlength;long BaudRate; long fBitFields;short wReserved;" & _
    "short XonLim;short XoffLim;byte Bytesize;byte parity;byte StopBits;byte XonChar; byte XoffChar;" & _
    "Byte ErrorChar;Byte EofChar;Byte EvtChar;short wReserved1"

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

    Local $commtimeouts = "long ReadIntervalTimeout;long ReadTotalTimeoutMultiplier;" & _
    "long ReadTotalTimeoutConstant;long WriteTotalTimeoutMultiplier;long WriteTotalTimeoutConstant"

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

    Local Const $GENERIC_READ_WRITE = 0xC0000000
    Local Const $OPEN_EXISTING = 3
    Local Const $FILE_ATTRIBUTE_NORMAL = 0x80
    ;~ Local Const $NOPARITY = 0
    ;~ Local Const $ONESTOPBIT = 0

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

    $dcb_Struct = DllStructCreate($dcbs)
    If @error Then Return SetError(1, 1, -1)

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

    $commtimeout_Struct = DllStructCreate($commtimeouts)
    If @error Then Return SetError(1, 1, -1)

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

    $hSerialPort = DllCall($commDll, "hwnd", "CreateFile", "str", "COM" & $CommPort, _
    "int", $GENERIC_READ_WRITE, _
    "int", 0, _
    "ptr", 0, _
    "int", $OPEN_EXISTING, _
    "int", $FILE_ATTRIBUTE_NORMAL, _
    "int", 0)
    If @error Then Return SetError(1, 1, -1)
    If Number($hSerialPort[0]) < 1 Then Return SetError(1, 1, -1)
    $commState = DllCall($commDll, "long", "GetCommState", "hwnd", $hSerialPort[0], "ptr", DllStructGetPtr($dcb_Struct))
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "DCBLength", DllStructGetSize($dcb_Struct))
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "BaudRate", $CommBaud)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "Bytesize", $CommBits)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "fBitfields", Number($CommCtrl))
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "Parity", $CommParity)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "StopBits", '0x' & $CommStop)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "XonLim", 2048)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "XoffLim", 512)
    If @error Then Return SetError(1, 1, -1)
    $commState = DllCall($commDll, "short", "SetCommState", "hwnd", $hSerialPort[0], "ptr", DllStructGetPtr($dcb_Struct))
    If @error Then Return SetError(1, 1, -1)

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

    If $commState[0] = 0 Then Return SetError(1, 1, -1)
    DllStructSetData($commtimeout_Struct, "ReadIntervalTimeout", -1)
    $commtimeout = DllCall($commDll, "long", "SetCommTimeouts", "hwnd", $hSerialPort[0], "ptr", DllStructGetPtr($commtimeout_Struct))
    If @error Then Return SetError(1, 1, -1)
    Return Number($hSerialPort[0])
    EndFunc ;==>_OpenCOMPort

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

    ;====================================================================================
    ; Function Name: _CloseCOMPort($CommSerialPort)
    ; Description: Closes serial port
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _CloseCOMPort($CommSerialPort)
    Local $closeerr = DllCall($commDll, "int", "CloseHandle", "hwnd", $CommSerialPort)
    DllClose($commDll)
    If @error Then Return SetError(1, 1, -1)
    Return ($closeerr[0])
    EndFunc ;==>_CloseCOMPort

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

    ;====================================================================================
    ; Function Name: _SendSerialString($CommSerialPort, $sSensString)
    ; Description: Send a String
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; $sSendString - String to send
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _SendSerialString($CommSerialPort, $sSendString)
    Local $lptr0 = DllStructCreate("long_ptr")
    DllCall($commDll, "int", "WriteFile", "hwnd", $CommSerialPort, _
    "str", $sSendString, _
    "int", StringLen($sSendString), _
    "long_ptr", DllStructGetPtr($lptr0), _
    "ptr", 0)
    If @error Then Return SetError(1, 1, -1)
    EndFunc ;==>_SendSerialString

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

    ;====================================================================================
    ; Function Name: _ReceiveStringWait($CommSerialPort, $MinBufferSize, $MaxWaitTime)
    ; Description: Recieves data
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; $MinBufferSize - Buffer size to wait for
    ; $MaxWaitTime - Maximum time to wait before failing
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _ReceiveStringWait($CommSerialPort, $MinBufferSize, $MaxWaitTime)
    Local $rxbuf
    Local $jetza = TimerInit()
    Local $lptr0 = DllStructCreate("long_ptr")

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

    Local $rxr, $rxl, $to
    Do
    $rxr = DllCall($commDll, "int", "ReadFile", "hwnd", $CommSerialPort, _
    "str", " ", _
    "int", 1, _
    "long_ptr", DllStructGetPtr($lptr0), _
    "ptr", 0)
    If @error Then Return SetError(1, 1, -1)
    $rxl = DllStructGetData($lptr0, 1)
    If $rxl >= 1 Then
    $rxbuf &= $rxr[2]
    EndIf
    $to = TimerDiff($jetza)
    Until StringLen($rxbuf) >= $MinBufferSize Or $to > $MaxWaitTime
    Return ($rxbuf)
    EndFunc ;==>_ReceiveStringWait

    [/autoit]
  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 14:30

    Mit diesen Änderungen ist es nun möglich (beim öffnen des Ports) den RTS Status zu setzen.
    Leider ist es mir nicht gelungen

    • Eine eigene Funktion zu schreiben, mit welcher einfach RTS gesetzt und rückgesetzt werden kann. (Unabhängig der Port öffnen Funktion! Natürlich muss der Port dazu offen sein!)

    • Eine Funktion zu schreiben welche das DCD Bit abfragt und zurück gibt.


    Da ich momentan leider keine Zeit mehr für dies habe, wäre ich froh, wenn mir jemand ein paar Tipps dafür geben könnte, damit ich die UDF noch ganz fertig machen kann.

    Spoiler anzeigen
    [autoit]

    #cs
    UDF cfx.au3
    serial functions using kernel32.dll
    V1.0
    Uwe Lahni 2008
    V2.0
    Andrew Calcutt 05/16/2009 - Started converting to UDF
    V2.1
    Mikko Keski-Heroja 02/23/2011 - UDF is now compatible with Opt("MustDeclareVars",1) and Date.au3. Global variable $dll is renamed to $commDll.
    V2.2
    Veronesi 04/26/2011 - Changed some cosmetics and documentation
    #ce
    #include-once
    Global $commDll
    Global $hSerialPort
    Global $dcb_Struct
    Global $commtimeout
    Global $commtimeout_Struct
    Global $commState
    ;~ Global $commState_Struct
    ;~ Global Const $STX = Chr(2)
    ;~ Global Const $ETX = Chr(3)
    ;~ Global Const $EOT = Chr(4)
    ;~ Global Const $ENQ = Chr(5)
    ;~ Global Const $ACK = Chr(6)
    ;~ Const $NAK = Chr(15)
    ;~ Const $DLE = Chr(16)

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

    Local $SendPort = _OpenCOMPort(3, 9600, 8, 0, 0, 1)
    If $SendPort = -1 Then MsgBox(0, "Fehler", "Der sendende COM-Port konnte nicht geöffnet werden!")
    ;~ $ReceivePort = _OpenCOMPort(4, 9600, 8, 0, 0)
    ;~ If $ReceivePort = -1 Then MsgBox(0, "Fehler", "Der empfangende COM-Port konnte nicht geöffnet werden!")

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

    _SendSerialString($SendPort, "This is a Teststring")
    ;~ MsgBox(0, "Empfangen", _ReceiveStringWait($ReceivePort, 20, 50))
    Sleep(2000)

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

    _CloseCOMPort($SendPort)
    ;~ _CloseCOMPort($ReceivePort)

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

    ;====================================================================================
    ; Function Name: _OpenCOMPort($CommPort, $CommBaud, $CommBits, $CommParity, $CommStop, $SetRTS)
    ; Description: Opens serial port
    ; Parameters: $CommPort
    ; $CommBits - 4-8
    ; $CommParity - 0=none, 1=odd, 2=even, 3=mark, 4=space
    ; $CommStop - 0,1,5,2
    ; $SetRTS - 1 = Set it / 0 = Don't set it
    ; Returns: on success, returns serial port id?
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _OpenCOMPort($CommPort, $CommBaud = '9600', $CommBits = '8', $CommParity = '0', $CommStop = '0', $SetRTS = 0)
    #cs
    CommCtrl => fBitfields => http://msdn.microsoft.com/en-us/library/…4(v=vs.85).aspx
    http://www.hpcc.ecs.soton.ac.uk/software/Win32API.Txt
    ' The fourteen actual DCB bit-sized data fields within the four bytes of fBitFields can be manipulated by bitwise logical And/Or operations.
    ' FieldName Bit # Description
    ' ----------------- ----- ------------------------------
    ' fBinary 1 binary mode, no EOF check
    ' fParity 2 enable parity checking
    ' fOutxCtsFlow 3 CTS output flow control
    ' fOutxDsrFlow 4 DSR output flow control
    ' fDtrControl 5 DTR flow control type (2 bits)
    ' fDsrSensitivity 7 DSR sensitivity
    ' fTXContinueOnXoff 8 XOFF continues Tx
    ' fOutX 9 XON/XOFF out flow control
    ' fInX 10 XON/XOFF in flow control
    ' fErrorChar 11 enable error replacement
    ' fNull 12 enable null stripping
    ' fRtsControl 13 RTS flow control (2 bits)
    ' fAbortOnError 15 abort reads/writes on error
    ' fDummy2 16 reserved
    #ce
    Local $CommCtrl = 0x1
    If $SetRTS = 1 Then $CommCtrl = BitOR($CommCtrl, 0x1001)
    $commDll = DllOpen("kernel32.dll")
    Local $dcbs = "long DCBlength;long BaudRate; long fBitFields;short wReserved;" & _
    "short XonLim;short XoffLim;byte Bytesize;byte parity;byte StopBits;byte XonChar; byte XoffChar;" & _
    "Byte ErrorChar;Byte EofChar;Byte EvtChar;short wReserved1"

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

    Local $commtimeouts = "long ReadIntervalTimeout;long ReadTotalTimeoutMultiplier;" & _
    "long ReadTotalTimeoutConstant;long WriteTotalTimeoutMultiplier;long WriteTotalTimeoutConstant"

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

    Local Const $GENERIC_READ_WRITE = 0xC0000000
    Local Const $OPEN_EXISTING = 3
    Local Const $FILE_ATTRIBUTE_NORMAL = 0x80
    ;~ Local Const $NOPARITY = 0
    ;~ Local Const $ONESTOPBIT = 0

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

    $dcb_Struct = DllStructCreate($dcbs)
    If @error Then Return SetError(1, 1, -1)

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

    $commtimeout_Struct = DllStructCreate($commtimeouts)
    If @error Then Return SetError(1, 1, -1)

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

    $hSerialPort = DllCall($commDll, "hwnd", "CreateFile", "str", "COM" & $CommPort, _
    "int", $GENERIC_READ_WRITE, _
    "int", 0, _
    "ptr", 0, _
    "int", $OPEN_EXISTING, _
    "int", $FILE_ATTRIBUTE_NORMAL, _
    "int", 0)
    If @error Then Return SetError(1, 1, -1)
    If Number($hSerialPort[0]) < 1 Then Return SetError(1, 1, -1)
    $commState = DllCall($commDll, "long", "GetCommState", "hwnd", $hSerialPort[0], "ptr", DllStructGetPtr($dcb_Struct))
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "DCBLength", DllStructGetSize($dcb_Struct))
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "BaudRate", $CommBaud)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "Bytesize", $CommBits)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "fBitfields", Number($CommCtrl))
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "Parity", $CommParity)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "StopBits", '0x' & $CommStop)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "XonLim", 2048)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "XoffLim", 512)
    If @error Then Return SetError(1, 1, -1)
    $commState = DllCall($commDll, "short", "SetCommState", "hwnd", $hSerialPort[0], "ptr", DllStructGetPtr($dcb_Struct))
    If @error Then Return SetError(1, 1, -1)

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

    If $commState[0] = 0 Then Return SetError(1, 1, -1)
    DllStructSetData($commtimeout_Struct, "ReadIntervalTimeout", -1)
    $commtimeout = DllCall($commDll, "long", "SetCommTimeouts", "hwnd", $hSerialPort[0], "ptr", DllStructGetPtr($commtimeout_Struct))
    If @error Then Return SetError(1, 1, -1)
    Return Number($hSerialPort[0])
    EndFunc ;==>_OpenCOMPort

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

    ;====================================================================================
    ; Function Name: _CloseCOMPort($CommSerialPort)
    ; Description: Closes serial port
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _CloseCOMPort($CommSerialPort)
    Local $closeerr = DllCall($commDll, "int", "CloseHandle", "hwnd", $CommSerialPort)
    DllClose($commDll)
    If @error Then Return SetError(1, 1, -1)
    Return ($closeerr[0])
    EndFunc ;==>_CloseCOMPort

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

    ;====================================================================================
    ; Function Name: _SendSerialString($CommSerialPort, $sSensString)
    ; Description: Send a String
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; $sSendString - String to send
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _SendSerialString($CommSerialPort, $sSendString)
    Local $lptr0 = DllStructCreate("long_ptr")
    DllCall($commDll, "int", "WriteFile", "hwnd", $CommSerialPort, _
    "str", $sSendString, _
    "int", StringLen($sSendString), _
    "long_ptr", DllStructGetPtr($lptr0), _
    "ptr", 0)
    If @error Then Return SetError(1, 1, -1)
    EndFunc ;==>_SendSerialString

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

    ;====================================================================================
    ; Function Name: _ReceiveStringWait($CommSerialPort, $MinBufferSize, $MaxWaitTime)
    ; Description: Recieves data
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; $MinBufferSize - Buffer size to wait for
    ; $MaxWaitTime - Maximum time to wait before failing
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _ReceiveStringWait($CommSerialPort, $MinBufferSize, $MaxWaitTime)
    Local $rxbuf
    Local $jetza = TimerInit()
    Local $lptr0 = DllStructCreate("long_ptr")

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

    Local $rxr, $rxl, $to
    Do
    $rxr = DllCall($commDll, "int", "ReadFile", "hwnd", $CommSerialPort, _
    "str", " ", _
    "int", 1, _
    "long_ptr", DllStructGetPtr($lptr0), _
    "ptr", 0)
    If @error Then Return SetError(1, 1, -1)
    $rxl = DllStructGetData($lptr0, 1)
    If $rxl >= 1 Then
    $rxbuf &= $rxr[2]
    EndIf
    $to = TimerDiff($jetza)
    Until StringLen($rxbuf) >= $MinBufferSize Or $to > $MaxWaitTime
    Return ($rxbuf)
    EndFunc ;==>_ReceiveStringWait

    [/autoit]

    Vielen Dank!
    Veronesi

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 14:23

    Hallo Rene,
    im Moment bin ich leider ziemlich im Stress sorry. Aber Mit dieser UDF sollte das Ansteuern der seriellen (!) Schnittstelle kein Problem mehr sein.
    Einfach die Handshake Leitungen (RTS....) können (noch) nicht gesetzt und abgefragt werden.

    Die Befehle sind in der UDF Dokumentiert. Bei Fragen fragen ;)

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 13:17

    Du wirst es nicht glauben, aber genau das habe ich auch gedacht! Und diese Seite habe ich auch schon gesucht, aber noch nicht gefunden!
    Danke!
    Ich werde es mal versuchen zu ändern!

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 12:53

    Also, ich habe nun die UDF etwas umformatiert, damit sie auch etwas besser aussieht, vollständig dokumentiert ist und keine überflüssigen Einträge (Funktionen / Variablen) mehr enthält.
    Senden und empfangen funktioniert tadellos, sowohl mit realen, als auch mit USB-zu-Serial Adaptern.

    Einzig folgendes: Wie kann ich das RTS setzen / rücksetzen und auf der anderen Seite den DCD Status abfragen?
    Hier die UDF:

    Spoiler anzeigen
    [autoit]

    #cs
    UDF cfx.au3
    serial functions using kernel32.dll
    V1.0
    Uwe Lahni 2008
    V2.0
    Andrew Calcutt 05/16/2009 - Started converting to UDF
    V2.1
    Mikko Keski-Heroja 02/23/2011 - UDF is now compatible with Opt("MustDeclareVars",1) and Date.au3. Global variable $dll is renamed to $commDll.
    V2.2
    Veronesi 04/26/2011 - Changed some cosmetics and documentation
    #ce
    #include-once
    Global $commDll
    Global $hSerialPort
    Global $dcb_Struct
    Global $commtimeout
    Global $commtimeout_Struct
    Global $commState
    ;~ Global $commState_Struct
    ;~ Global Const $STX = Chr(2)
    ;~ Global Const $ETX = Chr(3)
    ;~ Global Const $EOT = Chr(4)
    ;~ Global Const $ENQ = Chr(5)
    ;~ Global Const $ACK = Chr(6)
    ;~ Const $NAK = Chr(15)
    ;~ Const $DLE = Chr(16)

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

    Local $SendPort = _OpenCOMPort(3, 9600, 8, 0, 0)
    If $SendPort = -1 Then MsgBox(0, "Fehler", "Der sendende COM-Port konnte nicht geöffnet werden!")
    $ReceivePort = _OpenCOMPort(4, 9600, 8, 0, 0)
    If $ReceivePort = -1 Then MsgBox(0, "Fehler", "Der empfangende COM-Port konnte nicht geöffnet werden!")

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

    _SendSerialString($SendPort, "This is a Teststring")
    MsgBox(0, "Empfangen", _ReceiveStringWait($ReceivePort, 20, 50))

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

    _CloseCOMPort($SendPort)
    _CloseCOMPort($ReceivePort)

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

    ;====================================================================================
    ; Function Name: _OpenCOMPort($CommPort, $CommBaud, $CommBits, $CommParity, $CommStop, $CommCtrl)
    ; Description: Opens serial port
    ; Parameters: $CommPort
    ; $CommBits - 4-8
    ; $CommParity - 0=none, 1=odd, 2=even, 3=mark, 4=space
    ; $CommStop - 0,1,5,2
    ; $CommCtrl = 0011
    ; Returns: on success, returns serial port id?
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _OpenCOMPort($CommPort, $CommBaud = '9600', $CommBits = '8', $CommParity = '0', $CommStop = '0', $CommCtrl = '0011')
    $commDll = DllOpen("kernel32.dll")
    Local $dcbs = "long DCBlength;long BaudRate; long fBitFields;short wReserved;" & _
    "short XonLim;short XoffLim;byte Bytesize;byte parity;byte StopBits;byte XonChar; byte XoffChar;" & _
    "Byte ErrorChar;Byte EofChar;Byte EvtChar;short wReserved1"

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

    Local $commtimeouts = "long ReadIntervalTimeout;long ReadTotalTimeoutMultiplier;" & _
    "long ReadTotalTimeoutConstant;long WriteTotalTimeoutMultiplier;long WriteTotalTimeoutConstant"

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

    Local Const $GENERIC_READ_WRITE = 0xC0000000
    Local Const $OPEN_EXISTING = 3
    Local Const $FILE_ATTRIBUTE_NORMAL = 0x80
    ;~ Local Const $NOPARITY = 0
    ;~ Local Const $ONESTOPBIT = 0

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

    $dcb_Struct = DllStructCreate($dcbs)
    If @error Then Return SetError(1, 1, -1)

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

    $commtimeout_Struct = DllStructCreate($commtimeouts)
    If @error Then Return SetError(1, 1, -1)

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

    $hSerialPort = DllCall($commDll, "hwnd", "CreateFile", "str", "COM" & $CommPort, _
    "int", $GENERIC_READ_WRITE, _
    "int", 0, _
    "ptr", 0, _
    "int", $OPEN_EXISTING, _
    "int", $FILE_ATTRIBUTE_NORMAL, _
    "int", 0)
    If @error Then Return SetError(1, 1, -1)
    If Number($hSerialPort[0]) < 1 Then Return SetError(1, 1, -1)
    $commState = DllCall($commDll, "long", "GetCommState", "hwnd", $hSerialPort[0], "ptr", DllStructGetPtr($dcb_Struct))
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "DCBLength", DllStructGetSize($dcb_Struct))
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "BaudRate", $CommBaud)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "Bytesize", $CommBits)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "fBitfields", Number('0x' & $CommCtrl))
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "Parity", $CommParity)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "StopBits", '0x' & $CommStop)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "XonLim", 2048)
    If @error Then Return SetError(1, 1, -1)
    DllStructSetData($dcb_Struct, "XoffLim", 512)
    If @error Then Return SetError(1, 1, -1)
    $commState = DllCall($commDll, "short", "SetCommState", "hwnd", $hSerialPort[0], "ptr", DllStructGetPtr($dcb_Struct))
    If @error Then Return SetError(1, 1, -1)

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

    If $commState[0] = 0 Then Return SetError(1, 1, -1)
    DllStructSetData($commtimeout_Struct, "ReadIntervalTimeout", -1)
    $commtimeout = DllCall($commDll, "long", "SetCommTimeouts", "hwnd", $hSerialPort[0], "ptr", DllStructGetPtr($commtimeout_Struct))
    If @error Then Return SetError(1, 1, -1)
    Return Number($hSerialPort[0])
    EndFunc ;==>_OpenCOMPort

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

    ;====================================================================================
    ; Function Name: _CloseCOMPort($CommSerialPort)
    ; Description: Closes serial port
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _CloseCOMPort($CommSerialPort)
    Local $closeerr = DllCall($commDll, "int", "CloseHandle", "hwnd", $CommSerialPort)
    If @error Then Return SetError(1, 1, -1)
    Return ($closeerr[0])
    EndFunc ;==>_CloseCOMPort

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

    ;====================================================================================
    ; Function Name: _SendSerialString($CommSerialPort, $sSensString)
    ; Description: Send a String
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; $sSendString - String to send
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _SendSerialString($CommSerialPort, $sSendString)
    Local $lptr0 = DllStructCreate("long_ptr")
    DllCall($commDll, "int", "WriteFile", "hwnd", $CommSerialPort, _
    "str", $sSendString, _
    "int", StringLen($sSendString), _
    "long_ptr", DllStructGetPtr($lptr0), _
    "ptr", 0)
    If @error Then Return SetError(1, 1, -1)
    EndFunc ;==>_SendSerialString

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

    ;====================================================================================
    ; Function Name: _ReceiveStringWait($CommSerialPort, $MinBufferSize, $MaxWaitTime)
    ; Description: Recieves data
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; $MinBufferSize - Buffer size to wait for
    ; $MaxWaitTime - Maximum time to wait before failing
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _ReceiveStringWait($CommSerialPort, $MinBufferSize, $MaxWaitTime)
    Local $rxbuf
    Local $jetza = TimerInit()
    Local $lptr0 = DllStructCreate("long_ptr")

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

    Local $rxr, $rxl, $to
    Do
    $rxr = DllCall($commDll, "int", "ReadFile", "hwnd", $CommSerialPort, _
    "str", " ", _
    "int", 1, _
    "long_ptr", DllStructGetPtr($lptr0), _
    "ptr", 0)
    If @error Then Return SetError(1, 1, -1)
    $rxl = DllStructGetData($lptr0, 1)
    If $rxl >= 1 Then
    $rxbuf &= $rxr[2]
    EndIf
    $to = TimerDiff($jetza)
    Until StringLen($rxbuf) >= $MinBufferSize Or $to > $MaxWaitTime
    Return ($rxbuf)
    EndFunc ;==>_ReceiveStringWait

    [/autoit]
  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 12:24

    Ok. Ich bin gerade auf den wohl ältesten Fehler reingefallen:
    Beim _TX muss natürlich nicht die Portnummer rein, sondern der Rückgabewert der _OpenComm Funktion!

    Nun funktioniert das Senden und auch das Empfangen. Doch wie kann ich damit RTS setzen und zurücksetzen??

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 09:57

    Leider habe ich mit dieser UDF so meine Probleme.
    Ich habe ein serielles Programm (gekauft), mit welchem ich erfolgreich strings von COM3 nach COM4 und zurück senden kann.
    Nun möchte ich im ersten Schritt den COM3 von dieser UDF steuern. Also schliesse ich den COM3 mit dem externen Programm, damit er nicht mehr belegt ist und sende von diesen kurzen Script unten.
    Jedoch kommt nie irgendetwas an.

    Spoiler anzeigen
    [autoit]

    #cs
    UDF cfx.au3
    serial functions using kernel32.dll
    V1.0
    Uwe Lahni 2008
    V2.0
    Andrew Calcutt 05/16/2009 - Started converting to UDF
    V2.1
    Mikko Keski-Heroja 02/23/2011 - UDF is now compatible with Opt("MustDeclareVars",1) and Date.au3. Global variable $dll is renamed to $commDll.
    #ce
    #include-once
    Global $commDll
    Global $hSerialPort
    Global $dcb_Struct
    Global $commtimeout
    Global $commtimeout_Struct
    Global $commState
    Global $commState_Struct
    ;Global $rxbuf
    Global Const $STX=chr(2)
    Global Const $ETX=chr(3)
    Global Const $EOT=chr(4)
    Global Const $ENQ=chr(5)
    Global Const $ACK=chr(6)
    Const $NAK=chr(15)
    Const $DLE=chr(16)

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

    $Return = _OpenComm(3, 9600)
    If $Return = -1 Then MsgBox(0,"Fehler", "Der COM-Port konnte nicht geöffnet werden!")

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

    _Tx(3, "Hallo")

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

    Sleep(2000)

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

    _CloseComm(3)

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

    ;====================================================================================
    ; Function Name: _OpenComm($CommPort, $CommBaud, $CommBits, $CommParity, $CommStop, $CommCtrl)
    ; Description: Opens serial port
    ; Parameters: $CommPort
    ; $CommBits - 4-8
    ; $CommParity - 0=none, 1=odd, 2=even, 3=mark, 4=space
    ; $CommStop - 0,1,5,2
    ; $CommCtrl = 0011
    ; Returns: on success, returns serial port id?
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _OpenComm($CommPort, $CommBaud = '4800', $CommBits = '8', $CommParity = '0', $CommStop = '0', $CommCtrl = '0011', $DEBUG = '0')
    $commDll = DllOpen("kernel32.dll")
    local $dcbs="long DCBlength;long BaudRate; long fBitFields;short wReserved;" & _
    "short XonLim;short XoffLim;byte Bytesize;byte parity;byte StopBits;byte XonChar; byte XoffChar;" & _
    "Byte ErrorChar;Byte EofChar;Byte EvtChar;short wReserved1"

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

    local $commtimeouts="long ReadIntervalTimeout;long ReadTotalTimeoutMultiplier;" & _
    "long ReadTotalTimeoutConstant;long WriteTotalTimeoutMultiplier;long WriteTotalTimeoutConstant"

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

    local const $GENERIC_READ_WRITE=0xC0000000
    local const $OPEN_EXISTING=3
    local const $FILE_ATTRIBUTE_NORMAL =0x80
    local const $NOPARITY=0
    local const $ONESTOPBIT=0

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

    $dcb_Struct=DllStructCreate($dcbs)
    if @error Then errpr()

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

    $commtimeout_Struct=DllStructCreate($commtimeouts)
    if @error Then errpr()

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

    $hSerialPort = DllCall($commDll, "hwnd", "CreateFile", "str", "COM" & $CommPort, _
    "int", $GENERIC_READ_WRITE, _
    "int", 0, _
    "ptr", 0, _
    "int", $OPEN_EXISTING, _
    "int", $FILE_ATTRIBUTE_NORMAL, _
    "int", 0)
    if @error Then errpr()
    If number($hserialport[0])<1 Then
    If $DEBUG = 1 Then consolewrite("Open Error" & @CRLF)
    return (-1)
    EndIf
    $commState=dllcall($commDll,"long","GetCommState","hwnd",$hSerialPort[0],"ptr",DllStructGetPtr($dcb_Struct))
    if @error Then errpr()
    DllStructSetData( $dcb_Struct,"DCBLength",DllStructGetSize($dcb_Struct))
    if @error Then errpr()
    DllStructSetData( $dcb_Struct,"BaudRate",$CommBaud)
    if @error Then errpr()
    DllStructSetData( $dcb_Struct,"Bytesize",$CommBits)
    if @error Then errpr()
    DllStructSetData( $dcb_Struct,"fBitfields",number('0x' & $CommCtrl))
    if @error Then errpr()
    DllStructSetData( $dcb_Struct,"Parity",$CommParity)
    if @error Then errpr()
    DllStructSetData( $dcb_Struct,"StopBits",'0x' & $CommStop)
    if @error Then errpr()
    DllStructSetData( $dcb_Struct,"XonLim",2048)
    if @error Then errpr()
    DllStructSetData( $dcb_Struct,"XoffLim",512)
    if @error Then errpr()
    $commState=dllcall($commDll,"short","SetCommState","hwnd",$hSerialPort[0],"ptr",DllStructGetPtr($dcb_Struct))
    if @error Then errpr()
    If $DEBUG = 1 Then consolewrite("CommState: "& $commstate[0] & @CRLF)
    if $commstate[0]=0 Then
    If $DEBUG = 1 Then consolewrite("SetCommState Error" & @CRLF)
    return (-1)
    EndIf
    DllStructSetData( $commtimeout_Struct,"ReadIntervalTimeout",-1)
    $commtimeout=dllcall($commDll,"long","SetCommTimeouts","hwnd",$hSerialPort[0],"ptr",DllStructGetPtr($commtimeout_Struct))
    if @error Then errpr()
    return number($hSerialPort[0])
    EndFunc

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

    ;====================================================================================
    ; Function Name: _CloseComm($CommSerialPort)
    ; Description: Closes serial port
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _CloseComm($CommSerialPort, $DEBUG = 0)
    local $closeerr=DllCall($commDll, "int", "CloseHandle", "hwnd", $CommSerialPort)
    if @error Then
    errpr()
    Exit
    EndIf
    If $DEBUG = 1 Then ConsoleWrite("Close " & $closeerr[0] & @crlf)
    return($closeerr[0])
    EndFunc

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

    Func _tx($CommSerialPort, $t, $DEBUG = 0)
    if $DEBUG=1 then FileWriteLine("debug.txt","Send " &c2s($t))
    local $lptr0=dllstructcreate("long_ptr")
    local $tbuf=$t
    local $txr=dllcall($commDll,"int","WriteFile","hwnd",$CommSerialPort, _
    "str",$tbuf, _
    "int",stringlen($tbuf), _
    "long_ptr", DllStructGetPtr($lptr0), _
    "ptr", 0)
    if @error Then errpr()
    EndFunc

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

    ;====================================================================================
    ; Function Name: _rxwait($CommSerialPort, $MinBufferSize, $MaxWaitTime, $DEBUG = 0)
    ; Description: Recieves data
    ; Parameters: $CommSerialPort - value returned by _OpenComm
    ; $MinBufferSize - Buffer size to wait for
    ; $MaxWaitTime - Maximum time to wait before failing
    ; $DEBUG - Show debug messages
    ; Returns: on success, returns 1
    ; on failure returns -1 and sets @error to 1
    ; Note:
    ;====================================================================================
    Func _rxwait($CommSerialPort, $MinBufferSize, $MaxWaitTime, $DEBUG = 0)
    if $DEBUG=1 then ConsoleWrite("Wait " & $MinBufferSize & " " & $MaxWaitTime & @CRLF)
    Local $rxbuf
    local $jetza=TimerInit()
    local $lptr0=dllstructcreate("long_ptr")

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

    local $rxr, $rxl, $to
    Do
    $rxr=dllcall($commDll,"int","ReadFile","hwnd",$CommSerialPort, _
    "str"," ", _
    "int",1, _
    "long_ptr", DllStructGetPtr($lptr0), _
    "ptr", 0)
    if @error Then errpr()
    $rxl=DllStructGetData($lptr0,1)
    if $DEBUG=1 then ConsoleWrite("R0:" & $rxr[0] & " |R1:" & $rxr[1] & " |R2:" & $rxr[2] & " |rxl:" & $rxl & " |R4:" & $rxr[4] & @CRLF)
    if $rxl>=1 then
    $rxbuf&=$rxr[2]
    EndIf
    $to=TimerDiff($jetza)
    Until stringlen($rxbuf) >= $MinBufferSize OR $to > $MaxWaitTime
    Return($rxbuf)
    EndFunc

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

    Func _rx($rxbuf, $n=0, $DEBUG = 0)
    local $r
    if StringLen($rxbuf)<$n then
    $rxbuf=""
    Return("")
    EndIf
    If $n=0 Then
    $r=$rxbuf
    $rxbuf=""
    Return($r)
    EndIf
    If $n<0 then
    $rxbuf=""
    Return("")
    EndIf
    $r=Stringleft($rxbuf,$n)
    local $rl=Stringlen($rxbuf)
    $rxbuf=StringRight($rxbuf,$rl-$n)
    if $DEBUG=1 then FileWriteLine("debug.txt","Read " & c2s($r))
    return($r)
    EndFunc

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

    Func c2s($t)
    local $tc
    local $ts=""
    For $ii= 1 To StringLen($t)
    $tc=StringMid($t,$ii,1)
    if Asc($tc)<32 Then
    $ts&="<" & asc($tc) & ">"
    Else
    $ts&=$tc
    EndIf
    Next
    return $ts
    EndFunc

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

    func errpr()
    consolewrite ("Error " & @error & @CRLF)
    EndFunc

    [/autoit]

    Das ist übrigens die aktuellste UDF in dem genannten Link!

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 09:33

    Hallo Funkey,
    vielen Dank. Das sieht in der Tat sehr vielversprechend aus. Bloss kann ich damit kein RTS setzen und DCD abfragen.
    Dies ist für mich leider zwingend.
    Könnte man solche Funktionen noch einbauen?

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 26. April 2011 um 06:18

    Ganz ohne externe DLL wäre mir am liebsten.
    1. Weil ich den DLL Inhalt nicht kenne und meine Auftraggeber sehr kritisch gegenüber fremden DLLs eingestellt sind
    2. Die bestehenden DLLs meistens (immer?) 32bit sind. Meine Auftraggeber möchten das ganze Programm als 64Bit kompiliert haben => und nein: das hat keinen bestimmten Grund...

    Es muss nicht ein COM Objekt sein. Sollte halt etwas mit internen Windows DLLs sein, da die Auftraggeber diese als "vertrauenswürdig" eingestuft haben!

    Wie geht es denn????? (Irgendwas mit Filewrite in einer DLL, aber wie kann ich dann Ports definieren, senden, empfangen, RTS setzten, DCD abfragen?)
    Lg Veronesi

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 25. April 2011 um 18:42

    Hat niemand eine gute Idee?

  • COM Objekt für RS232 Kommunikation

    • veronesi
    • 22. April 2011 um 09:41

    Hallo zusammen

    Ich bin nun schon seit geraumer Zeit auf der Suche nach einem COM Objekt für RS232 Kommunikation welches ich mit AutoIt ansteuern kann.
    Klar, ich kenne diesen Thread. Dies funktioniert auch einwandfrei (32bit), ist aber leider mit externer und mir unbekannter DLL!
    Auch die Threads mit der MSCOMM32.ocx kenne ich. (z.B. hier). Dies funktioniert auch - teilweise. Dort kommt es auf den Computer drauf an und welche Komponenten / Entwicklungsumgebungen sonst noch installiert sind.
    Auch die NETComm.ocxThreads kenne ich, welche aber augenscheinlich wieder auf der MSCOMM32.ocx basieren.

    Was ich suche ist eine Lösung ohne externe (fremde) DLL welche zuerst noch ins System gebracht und registriert werden muss.

    M$ kann seit vielen Jahren auch aus der DOS-Box auf die serielle Schnittstelle schreiben. Da sollte es doch möglich sein, ein COM Objekt dafür zu finden. Von mir aus kann .NET eine Voraussetzung dafür sein.
    Es sollte einfach im System vorhanden sein und zuverlässig sowohl mit 32Bit als auch mit 64bit kompillierten AutoIt Scripten funktionieren.

    Ich muss "bloss" die serielle Schnittstelle konfigurieren können, Strings senden und empfangen, sowie das RTS setzen und das DCD Abfragen.

    Es wäre toll, wenn jemand eine Lösung findet. Mit dem OLEViewer 2.0 habe ich schon erfolglos gesucht.

    Danke!
    Veronesi

  • Kommunikation zwischen 2 Scripten

    • veronesi
    • 18. April 2011 um 09:44

    Ja, das könnte ich verwenden. Aber der Ehrgeiz lässt mir keine ruhe.
    Jetzt wo ich die DllStructCreate und DllStructGetData / SetData einigermassen verstanden habe ;)

    Wo liegt der Fehler?
    Ich habe ja extra eine eigene Message (WM_APP) verwendet!

  • Kommunikation zwischen 2 Scripten

    • veronesi
    • 18. April 2011 um 08:39

    Hallo nochmals ich :)

    Ich habe mir den Scriptteil von Prog@ndy nochmals angeschaut.
    Warum kann man das nicht einfacher lösen, so wie ich das gemacht habe?
    Klar meine Lösung funktioniert nicht :) Aber wieso?

    Danke für Eure Input

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    Global $WM_APP = 0x8000

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

    $hGui = GUICreate("Script 1", 200, 225, @DesktopWidth / 2 - 250, @DesktopHeight / 2 - 112)
    $hEdit = GUICtrlCreateEdit("Script 1 Test Text", 10, 10, 180, 175)
    $hButtonSend = GUICtrlCreateButton("Send Edit to Script 2", 10, 195, 180, 20)
    GUISetState()

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

    GUIRegisterMsg($WM_APP, '_MY_WM_APP')

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

    While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $hButtonSend
    _SendCopyDataString($hGui, WinGetHandle("Script 2"), GUICtrlRead($hEdit))
    EndSwitch
    WEnd

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

    Func _SendCopyDataString($My_Hwnd, $Scite_hwnd, $sCmd) ; by Prog@ndy
    ;~ Local $CmdStruct = DllStructCreate('Char[' & StringLen($sCmd) + 1 & ']')
    ;~ DllStructSetData($CmdStruct, 1, $sCmd)
    ;~ Local $COPYDATA = DllStructCreate('Ptr;DWord;Ptr')
    ;~ DllStructSetData($COPYDATA, 1, 1)
    ;~ DllStructSetData($COPYDATA, 2, StringLen($sCmd) + 1)
    ;~ DllStructSetData($COPYDATA, 3, DllStructGetPtr($CmdStruct))
    ;~ DllCall('User32.dll', 'None', 'SendMessage', 'HWnd', $Scite_hwnd, 'Int', $WM_COPYDATA, 'HWnd', $My_Hwnd, 'Ptr', DllStructGetPtr($COPYDATA))

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

    Local $Struct = DllStructCreate("CHAR[" & StringLen($sCmd) + 1 & "]")
    DllStructSetData($Struct, 1, $sCmd)
    DllCall("User32.dll", "None", "SendMessage", "HWnd", $Scite_hwnd, "INT", $WM_APP, "INT", StringLen($sCmd), "Ptr", DllStructGetPtr($Struct))
    EndFunc ;==>_SendCopyDataString

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

    Func _MY_WM_APP($hWnd, $msg, $wParam, $lParam) ; by Prog@ndy
    ;~ Local $Temp = ""
    ;~ Local $COPYDATA = DllStructCreate('Ptr;DWord;Ptr', $lParam)
    ;~ Local $COPYDATA_StringLen = DllStructGetData($COPYDATA, 2)
    ;~ Local $CmdStruct = DllStructCreate('Char[' & $COPYDATA_StringLen & ']', DllStructGetData($COPYDATA, 3))
    ;~ $COPYDATA_String = StringLeft(DllStructGetData($CmdStruct, 1), $COPYDATA_StringLen)
    ;~ GUICtrlSetData($hEdit, $COPYDATA_String)

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

    Local $Struct = DllStructCreate("CHAR[" & $wParam & "]", $lParam)
    $COPYDATA_String = DllStructGetData($Struct, 1)
    GUICtrlSetData($hEdit, $COPYDATA_String)
    EndFunc ;==>_MY_WM_COPYDATA

    [/autoit]
  • Kommunikation zwischen 2 Scripten

    • veronesi
    • 17. April 2011 um 18:20

    Hmmm, dann ist das wohl eher nichts für mich. Dachte, dass sei etwas einfacher!
    Naja, dann lassen wir das!

  • Kommunikation zwischen 2 Scripten

    • veronesi
    • 17. April 2011 um 16:40
    Zitat von war10ck

    Es ginge auch noch über den Arbeitsspeicher, eins schreibt was rein, das andere liest wieder

    mfg

    Edit1: Juhu 500. Beitrag xD


    Sorry, dass ich den Thread wieder ausgrabe, doch mich würde dazu ein (einfaches) Beispiel wundernehmen.
    Ist jemand in der Lage übers RAM Daten zwischen zwei unabhängig voneinander gestarteten Programmen auszutauschen?

    Lg, Veronesi

  • TaskScheduler / Aufgabenplaner mittels ObjCreate("Schedule.Service") ansteuern

    • veronesi
    • 15. April 2011 um 14:16

    So, nun habe ich noch einen kleinen Fehler beseitigt.
    Wenn man zwei Tasks im gleichen Script hintereinander erstellen wollte, wurde der 2. Task zwar erstellt, jedoch gab es immer eine Fehlermeldung zurück.

    Man muss zwei Variablen am Ende auf 0 setzen.
    Hier die aktuelle UDF:

    Spoiler anzeigen
    [autoit]

    #include-once
    Dim $oMyError, $ErrorFound = False

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

    ;==================================================================================
    ; Function: _CreateTask($TaskName, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $DaysInterval, $Interval, $RepetitionEnabled, $LogonType, $RunLevel, $Username, $Password, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True)
    ; Description: Creates a scheduled task
    ; Parameter(s): $h_Window - Handle to the Window to update
    ; $i_Mode - Update Mode; 0=Small Update; 1=Normal Update.
    ; Return Value(s): On Success - Return 1, @ERROR = 0
    ; On Failure - Sets @ERROR = 1, Return 0
    ; - Wrong OS (Needs Vista or higher) @Error = 2 Return 0
    ; Author(s): Unknown (http://www.autoit.de/index.php?page…4517#post214517)
    ; Changes: Veronesi
    ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!)
    ; Example: _CreateTask("Testname", "Description of this task", 3, "2011-03-30T08:00:00", "", 2, 1, 1, 1, 1, "PT1H", False, 3, 0, "", "", '"c:\windows\system32\notepad.exe"', "", "", True)
    ;==================================================================================

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

    #cs
    $TaskName => String, Free text

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

    $TaskDescription => String, Free text

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

    $TriggerEvent => http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    (0: TASK_TRIGGER_EVENT; Triggers the task when a specific event occurs.) => Not working yet
    1: TASK_TRIGGER_TIME; Triggers the task at a specific time of day.
    2: TASK_TRIGGER_DAILY; Triggers the task on a daily schedule. For example, the task starts at a specific time every day, every-other day, every third day, and so on.
    3: TASK_TRIGGER_WEEKLY; Triggers the task on a weekly schedule. For example, the task starts at 8:00 AM on a specific day every week or other week.
    4: TASK_TRIGGER_MONTHLY; Triggers the task on a monthly schedule. For example, the task starts on specific days of specific months.
    5: TASK_TRIGGER_MONTHLYDOW; Triggers the task on a monthly day-of-week schedule. For example, the task starts on a specific days of the week, weeks of the month, and months of the year.
    6: TASK_TRIGGER_IDLE; Triggers the task when the computer goes into an idle state.
    7: TASK_TRIGGER_REGISTRATION; Triggers the task when the task is registered.
    8: TASK_TRIGGER_BOOT; Triggers the task when the computer boots. => Needs Admin-Rights!
    9: TASK_TRIGGER_LOGON; Triggers the task when a specific user logs on. => Needs Admin-Rights!
    11:TASK_TRIGGER_SESSION_STATE_CHANGE; Triggers the task when a specific session state changes. => Needs Admin-Rights!

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

    $StartTrigger => String with Start time / date (Year-Month-DayTHour:Min:Sec) E.g. "2011-03-30T08:00:00"

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

    $EndTrigger => String with End time / date (Year-Month-DayTHour:Min:Sec) E.g. "2011-03-30T08:00:00"

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

    $DaysOfWeek => Integer; 1 = Sunday / 2 = Monday / 4 = Tuesday / 8 = Wednesday / 16 = Thursday / 32 = Friday / 64 = Saturday (http://msdn.microsoft.com/en-us/library/…4(v=VS.85).aspx)
    3 = Sunday AND Monday!
    This value works only in TriggerEvent 3 or 5

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

    $DaysOfMonth => Integer http://msdn.microsoft.com/en-us/library/aa380735(VS.85).aspx
    This value is only noted when $TriggerEvent = 4
    Day 1; 1
    Day 2; 2
    Day 3; 4
    Day 4; 8
    Day 5; 16
    Day 6; 32
    Day 7; 64
    Day 8; 128
    Day 9; 256
    Day 10; 512
    Day 11; 1024
    Day 12; 2048
    Day 13; 4096
    Day 14; 8192
    Day 15; 16384
    Day 16; 32768
    Day 17; 65536
    Day 18; 131072
    Day 19; 262144
    Day 20; 524288
    Day 21; 1048576
    Day 22; 2097152
    Day 23; 4194304
    Day 24; 8388608
    Day 25; 16777216
    Day 26; 33554432
    Day 27; 67108864
    Day 28; 134217728
    Day 29; 268435456
    Day 30; 536870912
    Day 31; 1073741824
    Last Day; 2147483648

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

    $MonthOfYear => http://msdn.microsoft.com/en-us/library/…6(v=VS.85).aspx
    This value is only noted when $TriggerEvent = 4
    January; 1
    February; 2
    March; 4
    April; 8
    May; 16
    June; 32
    July; 64
    August; 128
    September; 256
    October; 512
    November; 1024
    December; 2048
    January + February = 3...

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

    $WeeksOfMonth => http://msdn.microsoft.com/en-us/library/…3(v=VS.85).aspx
    This value is only noted when $TriggerEvent = 5
    First; 1
    Second; 2
    Third; 4
    Fourth; 8
    Fifth; 16
    Last; 32

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

    $DaysInterval => Integer of Days Interval; http://msdn.microsoft.com/en-us/library/…0(v=VS.85).aspx
    This value is only noted when $TriggerEvent = 2

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

    $Interval => String for Interval; http://msdn.microsoft.com/en-us/library/…8(v=VS.85).aspx
    P<days>DT<hours>H<minutes>M<seconds>S (for example, "PT5M" is 5 minutes, "PT1H" is 1 hour, and "PT20M" is 20 minutes). The maximum time allowed is 31 days, and the minimum time allowed is 1 minute.

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

    $RepetitionEnabled => True = Interval Trigger enabled / False = Interval Trigger disabled

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

    $LogonType => 2 = TASK_LOGON_S4U / Independant from an userlogin
    3 = User must already be logged in
    ;http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx

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

    $RunLevel => 0 = lowest, 1 = highest; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    Highest Need Admin-Rights!

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

    $Username => String with domainname "\" and Username. Use blank string ("") for actual user

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

    $Password => Password for the specified user

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

    $Program => String, Full Path and Programname to run

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

    $WorkingDirectory => Optional String

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

    $Arguments => Optional String

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

    $RunOnlyIfNetworkAvailable => Optional Boolean (Default = True)

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

    #ce

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

    Func _CreateTask($TaskName, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $DaysInterval, $Interval, $RepetitionEnabled, $LogonType, $RunLevel, $Username, $Password, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True)
    If @OSVersion = "WIN_XP" Or @OSVersion = "WIN_XPe" Or @OSVersion = "WIN_2000" Then Return SetError (2, 2, 0)

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

    Local $oService, $oFolder, $oTaskDefinition, $oRegistrationInfo, $oSettings
    Local $oPrincipal, $oColActions, $oAction, $oTrigger, $oColTriggers, $oTriggerRepetition

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

    If $TaskName = "" Or $Program = "" Then Return SetError (1, 1, 0)

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

    ;http://www.scriptinganswers.com/forum2/forum_posts.asp?TID=3282
    ;http://www.autoit.de/index.php?page…4517#post214517
    ;http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    $oMyError = ObjEvent("AutoIt.Error", "MyCreateTaskErrFunc") ; Initialize a COM error handler
    $oService = ObjCreate("Schedule.Service")
    $oService.Connect()

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

    $oFolder = $oService.GetFolder("\")
    $oTaskDefinition = $oService.NewTask(0)

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

    $oRegistrationInfo = $oTaskDefinition.RegistrationInfo()
    $oRegistrationInfo.Description() = $TaskDescription
    $oRegistrationInfo.Author() = @LogonDomain & "\" & @UserName

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

    $oSettings = $oTaskDefinition.Settings()
    $oSettings.MultipleInstances() = 0 ;Starts a new instance while an existing instance of the task is running.
    $oSettings.DisallowStartIfOnBatteries() = False
    $oSettings.StopIfGoingOnBatteries() = False
    $oSettings.AllowHardTerminate() = True
    $oSettings.StartWhenAvailable() = True ;Start task as soon as possible when task missed
    $oSettings.RunOnlyIfNetworkAvailable() = $RunOnlyIfNetworkAvailable

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

    $oSettings.Enabled() = True ;True ; The task can be performed only when this setting is True.
    $oSettings.Hidden() = False
    $oSettings.RunOnlyIfIdle() = False
    $oSettings.WakeToRun() = False
    $oSettings.ExecutionTimeLimit() = "P1D" ; When this parameter is set to Nothing, the execution time limit is infinite.
    $oSettings.Priority() = 5 ;http://msdn.microsoft.com/en-us/library/…0(v=VS.85).aspx 0 = Realtime, 10 = Idle

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

    $oPrincipal = $oTaskDefinition.Principal()
    $oPrincipal.Id() = @UserName
    $oPrincipal.DisplayName() = @UserName
    $oPrincipal.LogonType() = $LogonType
    $oPrincipal.RunLevel() = $RunLevel

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

    $oColTriggers = $oTaskDefinition.Triggers()
    $oTrigger = $oColTriggers.Create($TriggerEvent)

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

    $oTrigger.Id() = "TriggerID"
    $oTrigger.Enabled() = True
    $oTrigger.StartBoundary() = $StartTrigger
    $oTrigger.EndBoundary() = $EndTrigger
    If $TriggerEvent = 3 Or $TriggerEvent = 5 Then
    $oTrigger.DaysOfWeek() = $DaysOfWeek
    EndIf

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

    If $TriggerEvent = 4 Then
    $oTrigger.DaysOfMonth() = $DaysOfMonth
    $oTrigger.MonthsOfYear() = $MonthOfYear
    EndIf

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

    If $TriggerEvent = 5 Then
    $oTrigger.WeeksOfMonth() = $WeeksOfMonth
    EndIf

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

    If $TriggerEvent = 2 Then
    $oTrigger.DaysInterval() = $DaysInterval
    EndIf

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

    If $RepetitionEnabled Then
    $oTriggerRepetition = $oTrigger.Repetition()
    $oTriggerRepetition.Interval() = $Interval
    EndIf

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

    $oColActions = $oTaskDefinition.Actions()
    $oColActions.Context() = @UserName
    $oAction = $oTaskDefinition.Actions.Create(0)
    $oAction.Path() = $Program
    $oAction.WorkingDirectory() = $WorkingDirectory
    $oAction.Arguments() = $Arguments

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

    ; call rootFolder.RegisterTaskDefinition(strTaskName, taskDefinition, FlagTaskCreate, , , LogonTypeInteractive)
    $oFolder.RegisterTaskDefinition($TaskName, $oTaskDefinition, 6, $Username, $Password, $LogonType, "")

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

    If $ErrorFound Then Return SetError(3, 3, 0)
    $oService = 0
    $oMyError = 0
    Return 1
    EndFunc ;==>_CreateTask

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

    Func MyCreateTaskErrFunc()
    $ErrorFound = True
    Return 0

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

    ;~ MsgBox(0, "AutoItCOM Test", "We intercepted a COM Error !" & @CRLF & @CRLF & _
    ;~ "err.description is: " & @TAB & $oMyError.description & @CRLF & _
    ;~ "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
    ;~ "err.number is: " & @TAB & Hex($oMyError.number, 8) & @CRLF & _
    ;~ "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
    ;~ "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
    ;~ "err.source is: " & @TAB & $oMyError.source & @CRLF & _
    ;~ "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
    ;~ "err.helpcontext is: " & @TAB & $oMyError.helpcontext _
    ;~ )

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

    ;~ Local $err = $oMyError.number
    ;~ Local $g_eventerror
    ;~ If $err = 0 Then $err = -1

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

    ;~ $g_eventerror = $err ; to check for after this function returns
    EndFunc ;==>MyCreateTaskErrFunc

    [/autoit]

    Grüsse Veronesi

  • Zeilennummer in EXE suchen

    • veronesi
    • 12. April 2011 um 14:13

    Ich habe nun soeben noch OI "installier" leider ändert das nichts daran, dass ich im Tidy immer noch Fehler bekomme!

Spenden

Jeder Euro hilft uns, Euch zu helfen.

Download

AutoIt Tutorial
AutoIt Buch
Onlinehilfe
AutoIt Entwickler
  1. Datenschutzerklärung
  2. Impressum
  3. Shoutbox-Archiv
Community-Software: WoltLab Suite™