jacoporicci Posted September 9, 2017 Posted September 9, 2017 (edited) Hello, I'm almost a newbie with Autoit because I'm not a programmer but I use it to solve problems in my job (Lighting Designer/Programmer). I've a problem compiling this script, it says that there's no EndFunc declaration for the function at line 205, if I delete that section I've the same problem with the function at line 213. Can someone help me figuring out where's the problem? Thank you! P.s. The code is not entirely mine, is based on a MIDI client and a Telnet Server I've found on the internet and modified them to suit my needs. The two "big" codes, the midi part and the telnet part work on their own but I wanted to make everything inside the same script to be able to choose which one I want to use. P.s2. I've the same problem with every function inside the two big functions "_midi" and "_telnet" even if they're ended with EndFunc. expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <StaticConstants.au3> ;Includes for the MIDI receiver #include <WinAPI.au3> #include <MIDI.au3> ;Misc declarations Global $software_title = "" Global $atem_window_title = "" ;SET HERE BLACKMAGIC ATEM CONTROL SOFTWARE WINDOW NAME Opt("WinTitleMatchMode", 2) ;USER INTERFACE Global $main_gui = GUICreate($software_title, 364, 92, 287, 261, BitOR($WS_SYSMENU, $WS_CAPTION)) $Button1 = GUICtrlCreateButton("Telnet Input", 32, 16, 121, 49) $Button2 = GUICtrlCreateButton("Midi Input", 201, 18, 121, 49) GUISetState(@SW_SHOW) ;END USER INTERFACE While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 GUIDelete() Call("telnet") Case $Button2 GUIDelete() Call ("midi") EndSwitch WEnd ;----------------------------------------------------------------------------------------------- Func _telnet() ;USER INTERFACE $telnet_gui = GUICreate($software_title, 399, 174, 192, 124) $telnet_status_edit = GUICtrlCreateEdit("", 24, 40, 353, 121) GUICtrlSetData(-1, "") $Label1 = GUICtrlCreateLabel("TELNET CONTROL", 144, 8, 101, 17) GUISetState(@SW_SHOW) ;END USER INTERFACE ;; Do not touch these "settings" / global variables Global Const $sAppName = "" Global Const $nVersion = 1.0 Global Const $sWelcomeMessage = $sAppName & " [Version " & $nVersion & "]" & @CRLF & "" GUICtrlSetData($telnet_status_edit, $sWelcomeMessage & @CRLF, 1) ;; TCP Variables Dim $sMaxConnections = 10 Dim $sSocket[$sMaxConnections], $sBuffer[$sMaxConnections], $iAuth[$sMaxConnections] ;; TCP Options Dim $sIPAddress = @IPAddress1, $nPort = 23 TCPStartup() GUICtrlSetData($telnet_status_edit, "Starting TCP" & @CRLF, 1) $sMainSocket = TCPListen($sIPAddress, $nPort, 5) If @error Then Switch @error Case 1 _FatalError("The listening address was incorrect (Possibly another server was already running): " & $sIPAddress) GUICtrlSetData($telnet_status_edit, "The listening address was incorrect (Possibly another server was already running): " & $sIPAddress & @CRLF, 1) Case 2 _FatalError("The listening port was incorrect (Possibly another server was already running): " & $nPort) GUICtrlSetData($telnet_status_edit, "The listening port was incorrect (Possibly another server was already running): " & $nPort & @CRLF, 1) Case Else _FatalError("Unable to set up a listening server on " & $sIPAddress & ":" & $nPort) GUICtrlSetData($telnet_status_edit, "Unable to set up a listening server on " & $sIPAddress & ":" & $nPort & @CRLF, 1) EndSwitch EndIf While 1 ;; Accept new incoming clients, and ask them to authorise. $sNewSocket = TCPAccept($sMainSocket) If $sNewSocket > -1 Then For $x = 0 To UBound($sSocket) - 1 If Not $sSocket[$x] Then $sSocket[$x] = $sNewSocket $iAuth[$x] = 0 TCPSend($sSocket[$x], $sWelcomeMessage & @CRLF & @CRLF) GUICtrlSetData($telnet_status_edit, "Connection established. Server ip and port: " & $sIPAddress & ":" & $nPort & @CRLF, 1) ExitLoop EndIf Next EndIf ;; Loop through existing connections, check if they sent us any data For $x = 0 To UBound($sSocket) - 1 If $sSocket[$x] Then ;; Handle incoming data $sData = TCPRecv($sSocket[$x], 100) $sBuffer[$x] &= $sData If @error Then TCPCloseSocket($sSocket[$x]) $sSocket[$x] = "" $sBuffer[$x] = "" $iAuth[$x] = 0 ElseIf Asc($sData) = 0x8 Then ;backspace received $len = StringLen($sBuffer[$x]) $sBuffer[$x] = StringTrimRight($sBuffer[$x], 2) ; trim the buffer If $len = 1 Then TCPSend($sSocket[$x], ">") Else TCPSend($sSocket[$x], " " & Chr(0x8)) EndIf EndIf ;; Handle data, in case data is complete: ended with newline If StringInStr($sBuffer[$x], @CRLF) Then $sBuffer[$x] = StringTrimRight($sBuffer[$x], 2) ;; Is authorised with password, do some commands Switch $sBuffer[$x] Case "quit", "q", "exit" ; Closes the server on the remote client TCPCloseSocket($sSocket[$x]) $sSocket[$x] = "" $sBuffer[$x] = "" $iAuth[$x] = 0 Case "cam1" SendKeepActive($atem_window_title) Send("{CTRLDOWN}1{CTRLUP}") GUICtrlSetData($telnet_status_edit, "Switching to Camera 1" & @CRLF, 1) Case "cam2" SendKeepActive($atem_window_title) Send("{CTRLDOWN}2{CTRLUP}") GUICtrlSetData($telnet_status_edit, "Switching to Camera 2" & @CRLF, 1) Case "cam3" SendKeepActive($atem_window_title) Send("{CTRLDOWN}3{CTRLUP}") GUICtrlSetData($telnet_status_edit, "Switching to Camera 3" & @CRLF, 1) Case "cam4" SendKeepActive($atem_window_title) Send("{CTRLDOWN}4{CTRLUP}") GUICtrlSetData($telnet_status_edit, "Switching to Camera 4" & @CRLF, 1) Case "cam5" SendKeepActive($atem_window_title) Send("{CTRLDOWN}5{CTRLUP}") GUICtrlSetData($telnet_status_edit, "Switching to Camera 5" & @CRLF, 1) Case "cam6" SendKeepActive($atem_window_title) Send("{CTRLDOWN}6{CTRLUP}") GUICtrlSetData($telnet_status_edit, "Switching to Camera 6" & @CRLF, 1) Case "cam7" SendKeepActive($atem_window_title) Send("{CTRLDOWN}7{CTRLUP}") GUICtrlSetData($telnet_status_edit, "Switching to Camera 7" & @CRLF, 1) Case "cam8" SendKeepActive($atem_window_title) Send("{CTRLDOWN}8{CTRLUP}") GUICtrlSetData($telnet_status_edit, "Switching to Camera 8" & @CRLF, 1) Case "cam9" SendKeepActive($atem_window_title) Send("{CTRLDOWN}9{CTRLUP}") GUICtrlSetData($telnet_status_edit, "Switching to Camera 9" & @CRLF, 1) Case "cam10" SendKeepActive($atem_window_title) Send("{CTRLDOWN}0{CTRLUP}") GUICtrlSetData($telnet_status_edit, "Switching to Camera 9" & @CRLF, 1) Case "?", "help" GUICtrlSetData($telnet_status_edit, "Sending help" & @CRLF, 1) TCPSend($sSocket[$x], @CRLF & "quit" & @TAB & @TAB & "Closes connection to the Server" & @CRLF & _ "Cam+numberofcamera" & @TAB & @TAB & "Switches to selected camera 1-10" & @CRLF & _ "help" & @TAB & "View this help message again") EndSwitch TCPSend($sSocket[$x], @CRLF & ">") EndIf $sBuffer[$x] = "" EndIf Next WEnd Func MessageBroadcast($sMsg) For $n = 0 To UBound($sSocket) - 1 If $sSocket[$n] AND $iAuth[$n] == 1 Then TCPSend($sSocket[$n], $sMsg) EndIf Next EndFunc ;==>MessageBroadcast Func _FatalError($msg) ConsoleWrite(@CRLF & "! " & $msg & @CRLF) MsgBox(0, $sAppName & $nVersion, "A fatal error has occured and " & $sAppName & " has to be closed with the error message: " & @CRLF & $msg) Exit EndFunc EndFunc ;---------------------------------------------------------------------------------------------- Func _midi() Opt("MustDeclareVars", 1) Opt("GUIOnEventMode", 1) Opt("WinTitleMatchMode", 2) Global $hGui = GUICreate($software_title, 1000, 420) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") Global $cCombo = GUICtrlCreateCombo("", 10, 10, 400, 20) Global $cButton_Start = GUICtrlCreateButton("Start", 420, 10, 80, 20) GUICtrlSetOnEvent(-1, "_Midi_Start") Global $cButton_Stop = GUICtrlCreateButton("Stop", 510, 10, 80, 20) GUICtrlSetOnEvent(-1, "_Midi_Stop") GUICtrlCreateLabel("MIM_DATA", 10, 40, 100, 20) Global $cEdit_Data = GUICtrlCreateEdit("", 10, 60, 485, 350) GUICtrlCreateLabel("MIM_LONGDATA", 505, 40, 100, 20) Global $cEdit_LongData = GUICtrlCreateEdit("", 505, 60, 485, 350) GUISetState() _Midi_Startup() Global $tBuffer1 = _CreateBuffer() Global $tBuffer2 = _CreateBuffer() _GetDevices() Global $hMidi = 0 Global $bRelease = False Global $iDeviceCurrent = -1 GUIRegisterMsg($MM_MIM_CLOSE, "_MidiWinProc") GUIRegisterMsg($MM_MIM_DATA, "_MidiWinProc") GUIRegisterMsg($MM_MIM_ERROR, "_MidiWinProc") GUIRegisterMsg($MM_MIM_LONGDATA, "_MidiWinProc") GUIRegisterMsg($MM_MIM_LONGERROR, "_MidiWinProc") GUIRegisterMsg($MM_MIM_MOREDATA, "_MidiWinProc") GUIRegisterMsg($MM_MIM_OPEN, "_MidiWinProc") While Sleep(100) WEnd Func _ProcessLongData($pData, $iBytes) Local $tGetData = DllStructCreate("byte[" & $iBytes & "];", $pData) GUICtrlSetData($cEdit_LongData, DllStructGetData($tGetData, 1) & @CRLF, 1) EndFunc ;==>_ProcessLongData Func _ProcessData($iMidiMessage) Local $iLoWord = _WinAPI_LoWord($iMidiMessage) Local $iHiWord = _WinAPI_HiWord($iMidiMessage) Local $bStatus = BitAND($iLoWord, 0xFF) ;LoByte Local $bData1 = BitShift($iLoWord, 8) ;HiByte Local $bData2 = BitAND($iHiWord, 0xFF) ;LoByte Local $Chan = BitAND($bStatus, 0xF) Local $MidiMsg = BitShift($bStatus, 4) GUICtrlSetData($cEdit_Data, "StatusByte: " & $bStatus & @TAB & "DataByte1: " & $bData1 & @TAB & "DataByte2: " & $bData2 & @CRLF, 1) Switch $MidiMsg Case 0x8 ; Note Off GUICtrlSetData($cEdit_Data, @TAB & "NoteOff: MidiChannel: " & $Chan & @TAB & "NoteNumber: " & $bData1 & @TAB & "Velocity: " & $bData2c) Case 0x9 ; Note On GUICtrlSetData($cEdit_Data, @TAB & "NoteOn: MidiChannel: " & $Chan & @TAB & "NoteNumber: " & $bData1 & @TAB & "Velocity: " & $bData2 & @CRLF, 1) Case 0xA ; Polyphonic aftertouch GUICtrlSetData($cEdit_Data, @TAB & "Polyphonic aftertouch: MidiChannel: " & $Chan & @TAB & "NoteNumber: " & $bData1 & @TAB & "Poly Pressure: " & $bData2 & @CRLF, 1) Case 0xB ; Control Change GUICtrlSetData($cEdit_Data, @TAB & "Control Change: MidiChannel: " & $Chan & @TAB & "Controller: " & $bData1 & @TAB & "Value: " & $bData2 & @CRLF, 1) Case 0xC ; Program Change GUICtrlSetData($cEdit_Data, @TAB & "Program Change: MidiChannel: " & $Chan & @TAB & "Programm: " & $bData1 & @CRLF, 1) Case 0xD ; Channel aftertouch GUICtrlSetData($cEdit_Data, @TAB & "Channel aftertouch: MidiChannel: " & $Chan & @TAB & "Aftertouch : " & $bData1 & @CRLF, 1) Case 0xE ; Pitch Bend GUICtrlSetData($cEdit_Data, @TAB & "Pitch Bend: MidiChannel: " & $Chan & @TAB & "LSB: " & $bData1 & @TAB & "MSB: " & $bData2 & @CRLF, 1) EndSwitch If $bData1 == 1 Then If $bData2 == 127 Then SendKeepActive($atem_window_title) Send("{CTRLDOWN}1{CTRLUP}") EndIf EndIf If $bData1 == 2 Then If $bData2 == 127 Then SendKeepActive($atem_window_title) Send("{CTRLDOWN}2{CTRLUP}") EndIf EndIf If $bData1 == 3 Then If $bData2 == 127 Then SendKeepActive($atem_window_title) Send("{CTRLDOWN}3{CTRLUP}") EndIf EndIf If $bData1 == 4 Then If $bData2 == 127 Then SendKeepActive($atem_window_title) Send("{CTRLDOWN}4{CTRLUP}") EndIf EndIf If $bData1 == 5 Then If $bData2 == 127 Then SendKeepActive($atem_window_title) Send("{CTRLDOWN}5{CTRLUP}") EndIf EndIf If $bData1 == 6 Then If $bData2 == 127 Then SendKeepActive($atem_window_title) Send("{CTRLDOWN}6{CTRLUP}") EndIf EndIf If $bData1 == 7 Then If $bData2 == 127 Then SendKeepActive($atem_window_title) Send("{CTRLDOWN}7{CTRLUP}") EndIf EndIf If $bData1 == 8 Then If $bData2 == 127 Then SendKeepActive($atem_window_title) Send("{CTRLDOWN}8{CTRLUP}") EndIf EndIf If $bData1 == 9 Then If $bData2 == 127 Then SendKeepActive($atem_window_title) Send("{CTRLDOWN}9{CTRLUP}") EndIf EndIf If $bData1 == 10 Then If $bData2 == 127 Then SendKeepActive($atem_window_title) Send("{CTRLDOWN}0{CTRLUP}") EndIf EndIf EndFunc ;==>_ProcessData Func _MidiWinProc($hWnd, $iMsg, $wParam, $lParam) Switch $iMsg Case $MIM_OPEN ConsoleWrite("> MIM_OPEN" & @CRLF) Case $MIM_CLOSE ;hMidi Handle ab jetzt ungültig ConsoleWrite("> MIM_CLOSE" & @CRLF) $hMidi = 0 Case $MIM_DATA ; Normale Midi MSG ConsoleWrite("> MIM_DATA" & @CRLF) _ProcessData($lParam) Case $MIM_MOREDATA ConsoleWrite("> MIM_MOREDATA" & @CRLF) Case $MIM_LONGDATA ConsoleWrite("> MIM_LONGDATA" & @CRLF) Switch $bRelease Case False ; SysEx Daten kommen rein Local $tMidiHDR = DllStructCreate($MIDIHDR, $lParam) Local $iBytesRecorded = DllStructGetData($tMidiHDR, "dwBytesRecorded") ConsoleWrite("+ LONGDATA IN: " & $iBytesRecorded & " bytes" & @CRLF) _ProcessLongData(DllStructGetData($tMidiHDR, "lpData"), $iBytesRecorded) _MidiIn_AddBuffer($hMidi, $lParam, 48) Case Else ; Druch _MidiIn_Reset wird hier der Buffer zurückgegeben und muss hier "unprepared" werden ConsoleWrite("! Unprepare Buffer" & @CRLF) _MidiIn_UnprepareHeader($hMidi, $lParam, 48) EndSwitch Case $MIM_ERROR ConsoleWrite("> MIM_ERROR" & @CRLF) Case $MIM_LONGERROR ConsoleWrite("> MIM_LONGERROR" & @CRLF) _MidiIn_AddBuffer($hMidi, $lParam, 48) EndSwitch EndFunc ;==>_MidiWinProc Func _Midi_Start() ConsoleWrite(@CRLF & "!=============================" & @CRLF) ConsoleWrite("! starting..." & @CRLF & @CRLF) Local $aRegExp = StringRegExp(GUICtrlRead($cCombo), "<(\d+)>", 3) If @error Or Not IsArray($aRegExp) Then Return Local $iDevice = $aRegExp[0] If $iDevice = $iDeviceCurrent Then ConsoleWrite("- already running" & @CRLF) Return EndIf $bRelease = False Local $iRet If $hMidi <> 0 Then _Midi_Stop() $hMidi = _MidiIn_Open($iDevice, $hGui, 0, BitOR($CALLBACK_WINDOW, $MIDI_IO_STATUS)) ConsoleWrite("! MidiIn Open: " & $hMidi & " error: " & @error & @CRLF) $iDeviceCurrent = $iDevice $iRet = _MidiIn_PrepareHeader($hMidi, DllStructGetPtr($tBuffer1, "lpData"), 48) ConsoleWrite("! Prepare Header: " & $iRet & " error: " & @error & @CRLF) $iRet = _MidiIn_PrepareHeader($hMidi, DllStructGetPtr($tBuffer2, "lpData"), 48) ConsoleWrite("! Prepare Header: " & $iRet & " error: " & @error & @CRLF) $iRet = _MidiIn_AddBuffer($hMidi, DllStructGetPtr($tBuffer2, "lpData"), 48) ConsoleWrite("! Add Buffer: " & $iRet & " error: " & @error & @CRLF) $iRet = _MidiIn_AddBuffer($hMidi, DllStructGetPtr($tBuffer1, "lpData"), 48) ConsoleWrite("! Add Buffer: " & $iRet & " error: " & @error & @CRLF) $iRet = _MidiIn_Start($hMidi) ConsoleWrite("! MidiIn Start: " & $iRet & " error: " & @error & @CRLF) EndFunc ;==>_Midi_Start Func _Midi_Stop() ConsoleWrite(@CRLF & "!=============================" & @CRLF) ConsoleWrite("! stopping..." & @CRLF & @CRLF) $bRelease = True Local $iRet $iRet = _MidiIn_Reset($hMidi) ConsoleWrite("! MidiIn Reset: " & $iRet & " error: " & @error & @CRLF) $iRet = _MidiIn_Stop($hMidi) ConsoleWrite("! MidiIn Stop: " & $iRet & " error: " & @error & @CRLF) $iRet = _MidiIn_Close($hMidi) ConsoleWrite("! MidiIn Close: " & $iRet & " error: " & @error & @CRLF) $iDeviceCurrent = -1 EndFunc ;==>_Midi_Stop Func _GetDevices() Local $tMidiInCaps = DllStructCreate($MIDIINCAPS) Local $pMidiInCaps = DllStructGetPtr($tMidiInCaps) Local $iMidiInCaps = DllStructGetSize($tMidiInCaps) Local $iDeviceCnt = _MidiIn_GetNumDevs() Local $sDevice = "" Local $sDefault = "" For $i = 0 To ($iDeviceCnt - 1) _MidiIn_GetDevCaps($i, $pMidiInCaps, $iMidiInCaps) $sDevice &= "<" & $i & "> " & DllStructGetData($tMidiInCaps, "szPname") & "|" If $i = 0 Then $sDefault = "<" & $i & "> " & DllStructGetData($tMidiInCaps, "szPname") Next GUICtrlSetData($cCombo, $sDevice, $sDefault) EndFunc ;==>_GetDevices Func _CreateBuffer($iSize = 256, $iID = 0) Local $tBuffer = DllStructCreate("uint BufferSize; byte BufferData[" & $iSize & "];" & $MIDIHDR) DllStructSetData($tBuffer, "BufferSize", $iSize) DllStructSetData($tBuffer, "lpData", DllStructGetPtr($tBuffer, "BufferData")) DllStructSetData($tBuffer, "dwBufferLength", $iSize) DllStructSetData($tBuffer, "dwUser", $iID) Return $tBuffer EndFunc ;==>_CreateBuffer Func _Exit() Exit EndFunc ;==>_Exit EndFunc Edited September 9, 2017 by jacoporicci
Simpel Posted September 9, 2017 Posted September 9, 2017 Hi. There is a second EndFunc below EndFunc of _FatalError(). Take this and put it above Func MessageBroadcast(). You nested these two Func inside (at the end of a) Func. Give it a try. I couldn't test anything. Just watching the code. Conrad jacoporicci 1 SciTE4AutoIt = 3.7.3.0 AutoIt = 3.3.14.2 AutoItX64 = 0 OS = Win_10 Build = 19044 OSArch = X64 Language = 0407/german H:\...\AutoIt3\SciTE H:\...\AutoIt3 H:\...\AutoIt3\Include (H:\ = Network Drive) Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.
ripdad Posted September 9, 2017 Posted September 9, 2017 Functions can not be nested. They must exist on their own. jacoporicci 1 "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward
jacoporicci Posted September 9, 2017 Author Posted September 9, 2017 So how can I include more scripts into just one?
Simpel Posted September 9, 2017 Posted September 9, 2017 P.S. Your functions are named _midi() and _telnet() but you call them w/o leading underscore. I think you have to Call("_midi") and Call("_telnet") or rename the functions. SciTE4AutoIt = 3.7.3.0 AutoIt = 3.3.14.2 AutoItX64 = 0 OS = Win_10 Build = 19044 OSArch = X64 Language = 0407/german H:\...\AutoIt3\SciTE H:\...\AutoIt3 H:\...\AutoIt3\Include (H:\ = Network Drive) Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.
jacoporicci Posted September 9, 2017 Author Posted September 9, 2017 Just now, Simpel said: P.S. Your functions are named _midi() and _telnet() but you call them w/o leading underscore. I think you have to Call("_midi") and Call("_telnet") or rename the functions. I'm afraid the problem is that I can't nest functions as ripdad pointed out. What options do I have instead of having functions to include other scripts in one big script?
Simpel Posted September 9, 2017 Posted September 9, 2017 You can call a function inside another one. You are only not allowed to write a functions text inside the text of another function. SciTE4AutoIt = 3.7.3.0 AutoIt = 3.3.14.2 AutoItX64 = 0 OS = Win_10 Build = 19044 OSArch = X64 Language = 0407/german H:\...\AutoIt3\SciTE H:\...\AutoIt3 H:\...\AutoIt3\Include (H:\ = Network Drive) Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.
HurleyShanabarger Posted September 9, 2017 Posted September 9, 2017 Why do you want to nest functions? Just un-nest the function and call the function you wanted to nest inside the function. So instead of: Func _Main() Sleep(250) Func _Nested() MsgBox(0,"", "") EndFunc EndFunc do this: Func _Main() Sleep(250) _Nested() EndFunc Func _Nested() MsgBox(0, "", "") EndFunc
jacoporicci Posted September 9, 2017 Author Posted September 9, 2017 Well, that was a very easy fix. Thank everyone for the prompt answers, will keep you posted. Have a good weekend
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now