Attribute VB_Name = "DMCCOM"
' Windows interface to Galil Motion Controllers

' For Visual Basic 4.0 and Higher Only!

' All functions return an error code. 0 is function completed successfully.
' Any error code < 0 is a local error (see the error codes belong).
' Any error code > 0 is an Win32 API error.
' These are documented in the Win32 Programming Reference.

' Constant values

' Controller models
Public Const DMC400 = "DMC-400"
Public Const DMC600 = "DMC-600"
Public Const DMC700 = "DMC-700"
Public Const DMC1000 = "DMC-1000"
Public Const DMC1200 = "DMC-1200"
Public Const DMC1410 = "DMC-1410"
Public Const DMC1411 = "DMC-1411"
Public Const DMC1412 = "DMC-1412"
Public Const DMC1500 = "DMC-1500"
Public Const DMC1600 = "DMC-1600"
Public Const DMC1700 = "DMC-1700"
Public Const DMC2000 = "DMC-2000"
 
' Error Codes
Public Const DMCNOERROR = 0
Public Const DMCERROR_TIMEOUT = -1
Public Const DMCERROR_COMMAND = -2
Public Const DMCERROR_CONTROLLER = -3
Public Const DMCERROR_FILE = -4
Public Const DMCERROR_DRIVER = -5
Public Const DMCERROR_HANDLE = -6
Public Const DMCERROR_HMODULE = -7
Public Const DMCERROR_MEMORY = -8
Public Const DMCERROR_BUFFERFULL = -9
Public Const DMCERROR_RESPONSEDATA = -10
Public Const DMCERROR_DMA = -11
Public Const DMCERROR_ARGUMENT = -12
Public Const DMCERROR_DATARECORD = -13
Public Const DMCERROR_DOWNLOAD = -14
Public Const DMCERROR_FIRMWARE = -15
Public Const DMCERROR_CONVERSION = -16
Public Const DMCERROR_RESOURCE = -17
Public Const DMCERROR_REGISTRY = -18
Public Const DMCERROR_BUSY = -19
Public Const DMCERROR_DEVICE_DISCONNECTED = -20

' Constant values for data record data types
Public Const DRTypeUnknown = 0
Public Const DRTypeCHAR = 1
Public Const DRTypeUCHAR = 2
Public Const DRTypeSHORT = 3
Public Const DRTypeUSHORT = 4
Public Const DRTypeLONG = 5
Public Const DRTypeULONG = 6

' Constant values for data record general data item offsets
' IMPORTANT - Values can change between revisions

' General offsets for firmware without coordinated motion T axis
Public Const DRGenOffsetsSampleNumber = 0
Public Const DRGenOffsetsGeneralInput0 = 2
Public Const DRGenOffsetsGeneralInput1 = 3
Public Const DRGenOffsetsGeneralInput2 = 4
Public Const DRGenOffsetsGeneralInput3 = 5
Public Const DRGenOffsetsGeneralInput4 = 6
Public Const DRGenOffsetsGeneralInput5 = 7
Public Const DRGenOffsetsGeneralInput6 = 8
Public Const DRGenOffsetsGeneralInput7 = 9
Public Const DRGenOffsetsGeneralInput8 = 10
Public Const DRGenOffsetsGeneralInput9 = 11
Public Const DRGenOffsetsGeneralOutput0 = 12
Public Const DRGenOffsetsGeneralOutput1 = 13
Public Const DRGenOffsetsGeneralOutput2 = 14
Public Const DRGenOffsetsGeneralOutput3 = 15
Public Const DRGenOffsetsGeneralOutput4 = 16
Public Const DRGenOffsetsGeneralOutput5 = 17
Public Const DRGenOffsetsGeneralOutput6 = 18
Public Const DRGenOffsetsGeneralOutput7 = 19
Public Const DRGenOffsetsGeneralOutput8 = 20
Public Const DRGenOffsetsGeneralOutput9 = 21
Public Const DRGenOffsetsErrorCode = 22
Public Const DRGenOffsetsGeneralStatus = 23
Public Const DRGenOffsetsSegmentCount = 24
Public Const DRGenOffsetsCoordinatedMoveStatus = 26
Public Const DRGenOffsetsCoordinatedMoveDistance = 28
Public Const DRGenOffsetsAxis1 = 32
Public Const DRGenOffsetsAxis2 = 58
Public Const DRGenOffsetsAxis3 = 84
Public Const DRGenOffsetsAxis4 = 110
Public Const DRGenOffsetsAxis5 = 136
Public Const DRGenOffsetsAxis6 = 162
Public Const DRGenOffsetsAxis7 = 188
Public Const DRGenOffsetsAxis8 = 214
Public Const DRGenOffsetsEnd = 240

' General offsets for firmware with coordinated motion T axis
Public Const wTDRGenOffsetsSampleNumber = 0
Public Const wTDRGenOffsetsGeneralInput0 = 2
Public Const wTDRGenOffsetsGeneralInput1 = 3
Public Const wTDRGenOffsetsGeneralInput2 = 4
Public Const wTDRGenOffsetsGeneralInput3 = 5
Public Const wTDRGenOffsetsGeneralInput4 = 6
Public Const wTDRGenOffsetsGeneralInput5 = 7
Public Const wTDRGenOffsetsGeneralInput6 = 8
Public Const wTDRGenOffsetsGeneralInput7 = 9
Public Const wTDRGenOffsetsGeneralInput8 = 10
Public Const wTDRGenOffsetsGeneralInput9 = 11
Public Const wTDRGenOffsetsGeneralOutput0 = 12
Public Const wTDRGenOffsetsGeneralOutput1 = 13
Public Const wTDRGenOffsetsGeneralOutput2 = 14
Public Const wTDRGenOffsetsGeneralOutput3 = 15
Public Const wTDRGenOffsetsGeneralOutput4 = 16
Public Const wTDRGenOffsetsGeneralOutput5 = 17
Public Const wTDRGenOffsetsGeneralOutput6 = 18
Public Const wTDRGenOffsetsGeneralOutput7 = 19
Public Const wTDRGenOffsetsGeneralOutput8 = 20
Public Const wTDRGenOffsetsGeneralOutput9 = 21
Public Const wTDRGenOffsetsErrorCode = 22
Public Const wTDRGenOffsetsGeneralStatus = 23
Public Const wTDRGenOffsetsSegmentCountS = 24
Public Const wTDRGenOffsetsCoordinatedMoveStatusS = 26
Public Const wTDRGenOffsetsCoordinatedMoveDistanceS = 28
Public Const wTDRGenOffsetsSegmentCountT = 32
Public Const wTDRGenOffsetsCoordinatedMoveStatusT = 34
Public Const wTDRGenOffsetsCoordinatedMoveDistanceT = 36
Public Const wTDRGenOffsetsAxis1 = 40
Public Const wTDRGenOffsetsAxis2 = 66
Public Const wTDRGenOffsetsAxis3 = 92
Public Const wTDRGenOffsetsAxis4 = 118
Public Const wTDRGenOffsetsAxis5 = 144
Public Const wTDRGenOffsetsAxis6 = 170
Public Const wTDRGenOffsetsAxis7 = 196
Public Const wTDRGenOffsetsAxis8 = 222
Public Const wTDRGenOffsetsEnd = 248

' Constant values for data record axis data item offsets
' IMPORTANT - Values can change between revisions
Public Const DRAxisOffsetsNoAxis = 0
Public Const DRAxisOffsetsAxisStatus = 0
Public Const DRAxisOffsetsAxisSwitches = 2
Public Const DRAxisOffsetsAxisStopCode = 3
Public Const DRAxisOffsetsAxisReferencePosition = 4
Public Const DRAxisOffsetsAxisMotorPosition = 8
Public Const DRAxisOffsetsAxisPositionError = 12
Public Const DRAxisOffsetsAxisAuxillaryPosition = 16
Public Const DRAxisOffsetsAxisVelocity = 20
Public Const DRAxisOffsetsAxisTorque = 24
Public Const DRAxisOffsetsEnd = 26

' Constant values for GALILREGISTRY structure

' Controller Type
Public Const ControllerTypeISABus = 0
Public Const ControllerTypeSerial = 1
Public Const ControllerTypePCIBus = 2
Public Const ControllerTypeUSB = 3

' Device Drivers
Public Const DeviceDriverWinRT = 0
Public Const DeviceDriverGalil = 1

' Serial Handshake
Public Const SerialHandshakeHardware = 0
Public Const SerialHandshakeSoftware = 1

' Data Record Access
Public Const DataRecordAccessNone = 0
Public Const DataRecordAccessDMA = 1
Public Const DataRecordAccessFIFO = 2

Public moving% 'flag di movimento assi X Y Z W (laser+ tavola)
Public hDmc As Long 'handler Galil controller

'Jog
'odt&: valore timer precedente
'dt&: valore timer attuale
'thexx&: valore x iniziale durante il jogging
'theyy&: valore y iniziale durante il jogging
'spx&: velocita' asse x calcolata volta per volta
'spy&: velocita' asse y calcolata volta per volta
'otcmd$: comando Galil precedente

Public odt&, thexx&, theyy&, spx&, spy&, otcmd$

' Structures

' To add/change/delete registry information
Type GALILREGISTRY
        Model As String * 16
        DeviceNumber As Integer
        DeviceDriver As Integer
        Timeout As Long
        Delay As Long
        ControllerType As Integer
        CommPort As Integer
        CommSpeed As Long
        Handshake As Integer
        Address As Integer
        Interrupt As Integer
        DataRecordAccess As Integer
        dmachannel As Integer
        DataRecordSize As Integer
        RefreshRate As Integer
        SerialNumber As Integer
        PNPHardwareKey As String * 64
End Type

' Function prototypes

#If Win32 Then
Public Declare Function DMCOpen Lib "dmc32.dll" (ByVal Controller As Integer, ByVal hwnd As Long, phDmc As Long) As Long
' Open communications with the Galil controller.

' Controller       A number between 1 and 16. Up to 16 Galil controllers may be
'                  addressed per process.
' hWnd             The window handle to use for notifying the application
'                  program of an interrupt.
' phDmc            Handle to the Galil controller to be use for all subsequent
'                  API calls.

Public Declare Function DMCOpen2 Lib "dmc32.dll" (ByVal Controller As Integer, ByVal ThreadID As Long, phDmc As Long) As Long
' Open communications with the Galil controller.

' Controller       A number between 1 and 16. Up to 16 Galil controllers may be
'                  addressed per process.
' ThreadID         The thread id to use for notifying the application
'                  program of an interrupt.
' phDmc            Handle to the Galil controller to be use for all subsequent
'                  API calls.

Public Declare Function DMCGetHandle Lib "dmc32.dll" (ByVal Controller As Integer, phDmc As Long) As Long
' Get the handle associated with a particular Galil controller.

' Controller       A number between 1 and 16. Up to 16 Galil controllers may be
'                  addressed per process.
' phDmc            Handle to the Galil controller to be use for all subsequent
'                  API calls.

Public Declare Function DMCClose Lib "dmc32.dll" (ByVal hDmc As Long) As Long
' Close communications with the Galil controller.

' hDmc             Handle to the Galil controller.

Public Declare Function DMCCommand Lib "dmc32.dll" (ByVal hDmc As Long, ByVal CommandString As String, ByVal Resp As String, ByVal ResponseLength As Long) As Long
' Send a command to the Galil controller.
' NOTE: This function can only send commands or groups of commands up to
' 1024 bytes long.

' hDmc             Handle to the Galil controller.
' CommandString    The command to send to the Galil controller.
' Response         Buffer to receive the response data.
' ResponseLength   Length of the buffer.

Public Declare Function DMCFastCommand Lib "dmc32.dll" (ByVal hDmc As Long, ByVal CommandString As String) As Long
' Send a command to the Galil controller without the overhead of waiting for a response. Use this function with
' caution as command errors will not be reported and the out-going FIFO or communciations buffer
' may fill up. This function is intended to be used in routines which provide data records for the Galil
' DL and QD commands which do not return a response. Other uses may be to send contour data.
' NOTE: This function can only send commands or groups of commands up to
' 1024 bytes long.

' hDmc             Handle to the Galil controller.
' CommandString    The command to send to the Galil controller.

Public Declare Function DMCGetUnsolicitedResponse Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Response As String, ByVal ResponseLength As Long) As Long
' Query the Galil controller for unsolicited responses. These are messages
' output from programs running in the background in the Galil controller.

' hDmc             Handle to the Galil controller.
' Response         Buffer to receive the response data.
' ResponseLength   Length of the buffer.

Public Declare Function DMCWriteData Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Buffer As String, ByVal BufferLength As Long, BytesWritten As Long) As Long
' Low-level I/O routine to write data to the Galil controller. Data is written
' to the Galil controller only if it is "ready" to receive it. The function
' will attempt to write exactly cbBuffer characters to the controller.
' NOTE: For Win32 and WinRT driver the maximum number of bytes which can be written
' each time is 64. There are no restrictions with the Galil driver.

' hDmc             Handle to the Galil controller.
' Buffer           Buffer to write the data from. Data does not need to be
'                  NULL terminated.
' BufferLength     Length of the data in the buffer.
' BytesWritten     Number of bytes written.

Public Declare Function DMCReadData Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Buffer As String, ByVal BufferLength As Long, BytesRead As Long) As Long
' Low-level I/O routine to read data from the Galil controller. The routine
' will read what ever is currently in the FIFO (bus controller) or
' communications port input queue (serial controller). The function will read
' up to cbBuffer characters from the controller. The data placed in the user
' buffer (pchBuffer) is NOT NULL terminated. The data returned is not guaranteed
' to be a complete response - you may have to call this function repeatedly to
' get a complete response.
' NOTE: For Win32 and WinRT driver the maximum number of bytes which can be read
' each time is 64. There are no restrictions with the Galil driver.

' hDmc             Handle to the Galil controller.
' Buffer           Buffer to read the data into. Data will not be NULL
'                  terminated.
' BufferLength     Length of the buffer.
' BytesRead        Number of bytes read.

Public Declare Function DMCGetAdditionalResponseLen Lib "dmc32.dll" (ByVal hDmc As Long, ResponseLength As Long) As Long
' Query the Galil controller for the length of the additional response data. There will be more
' response data available if DMCCommand returned DMCERROR_BUFFERFULL.

' hDmc             Handle to the Galil controller.
' ResponseLength   Length of the additional response data.

Public Declare Function DMCGetAdditionalResponse Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Response As String, ByVal ResponseLength As Long) As Long
' Query the Galil controller for more response data. There will be more
' response data available if DMCCommand returned DMCERROR_BUFFERFULL.

' hDmc             Handle to the Galil controller.
' Response         Buffer to receive the response data.
' ResponseLength   Length of the buffer.

Public Declare Function DMCError Lib "dmc32.dll" (ByVal hDmc As Long, ByVal errorcode As Long, ByVal Message As String, ByVal MessageLength As Long) As Long
' Retrieve the error message text from a DMCERROR_COMMAND error.

' hDmc             Handle to the Galil controller.
' ErrorCode        Error returned from API function.
' Message          Buffer to receive the error message text.
' MessageLength    Length of the buffer.

Public Declare Function DMCClear Lib "dmc32.dll" (ByVal hDmc As Long) As Long
' Clear the Galil controller FIFO.

' hDmc             Handle to the Galil controller.

Public Declare Function DMCReset Lib "dmc32.dll" (ByVal hDmc As Long) As Long
' Reset the Galil controller.

' hDmc             Handle to the Galil controller.

Public Declare Function DMCMasterReset Lib "dmc32.dll" (ByVal hDmc As Long) As Long
' Master reset the Galil controller.

' hDmc             Handle to the Galil controller.

Public Declare Function DMCVersion Lib "dmc32.dll" (ByVal hDmc As Long, ByVal version As String, ByVal VersionLength As Long) As Long
' Get the version of the Galil controller.

' hDmc             Handle to the Galil controller.
' Version          Buffer to receive the version information.
' VersionLength    Length of the buffer.

Public Declare Function DMCDownloadFile Lib "dmc32.dll" (ByVal hDmc As Long, ByVal FileName As String, ByVal Label As String) As Long
' Download a file to the Galil controller.

' hDmc             Handle to the Galil controller.
' FileName         File name to download to the Galil controller.
' Label            Program label to download to. This argument is ignored if
'                  NULL.

Public Declare Function DMCDownloadFromBuffer Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Buffer As String, ByVal Label As String) As Long
' Download a file to the Galil controller.

' hDmc             Handle to the Galil controller.
' Buffer           Buffer of DMC commands to download to the Galil controller.
' Label            Program label to download to. This argument is ignored if
'                  NULL.

Public Declare Function DMCUploadFile Lib "dmc32.dll" (ByVal hDmc As Long, ByVal FileName As String) As Long
' Upload a file from the Galil controller.

' FileName         File name to upload from the Galil controller.

Public Declare Function DMCUploadToBuffer Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Buffer As String, ByVal BufferLength As Long) As Long
' Upload a file from the Galil controller.

' Buffer           Buffer of DMC commands to upload from the Galil controller.
' BufferLength     Length of the buffer.

Public Declare Function DMCSendFile Lib "dmc32.dll" (ByVal hDmc As Long, ByVal FileName As String) As Long
' Send a file to the Galil controller.

' hDmc             Handle to the Galil controller.
' FileName         File name to send to the Galil controller.

Public Declare Function DMCArrayDownload Lib "dmc32.dll" (ByVal hDmc As Long, ByVal ArrayName As String, ByVal FirstElement As Integer, ByVal LastElement As Integer, ByVal Data As String, ByVal DataLength As Long, BytesWritten As Long) As Long
' Download an array to the Galil controller. The array must exist. Array data can be
' delimited by a comma or CR (0x0D) or CR/LF (0x0D0A).
' NOTE: The firmware on the controller must be recent enough to support the QD command.

' hDmc             Handle to the Galil controller.
' ArrayName        Array name to download to the Galil controller.
' FirstElement     First array element.
' LastElement      Last array element.
' Data             Buffer to write the array data from. Data does not need to be
'                  NULL terminated.
' DataLength       Length of the array data in the buffer.
' BytesWritten     Number of bytes written.

Public Declare Function DMCArrayUpload Lib "dmc32.dll" (ByVal hDmc As Long, ByVal ArrayName As String, ByVal FirstElement As Integer, ByVal LastElement As Integer, ByVal Buffer As String, ByVal BufferLength As Long, BytesRead As Long, ByVal Comma As Integer) As Long
' Upload an array from the Galil controller. The array must exist. Array data will be
' delimited by a comma or CR (0x0D) depending of the value of fComma.
' NOTE: The firmware on the controller must be recent enough to support the QU command.

' hDmc             Handle to the Galil controller.
' ArrayName        Array name to upload from the Galil controller.
' FirstElement     First array element.
' LastElement      Last array element.
' Buffer           Buffer to read the array data into. Array data will not be
'                  NULL terminated.
' BufferLength     Length of the buffer.
' BytesRead        Number of bytes read.

Public Declare Function DMCRefreshDataRecord Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Length As Long) As Long
' Refresh the data record used for fast polling.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc             Handle to the Galil controller.
' Length           Refresh size in bytes. Set to 0 unless you do not want a full-buffer
'                  refresh.

Public Declare Function DMCGetDataRecord Lib "dmc32.dll" (ByVal hDmc As Long, ByVal GeneralOffset As Integer, ByVal AxisInfoOffset As Integer, DataType As Integer, Data As Long) As Long
' Get a data item from the data record used for fast polling.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc             Handle to the Galil controller.
' GeneralOffset    Data record offset for general data item.
' AxisInfoOffset   Additional data record offset for axis data item.
' DataType         Data type of the data item. If you are using the standard,
'                  pre-defined offsets, set this argument to zero before calling this
'                  function. The actual data type of the data item is returned on output.
' Data             Buffer to receive the data record data. Output only.

Public Declare Function DMCDiagnosticsOn Lib "dmc32.dll" (ByVal hDmc As Long, ByVal FileName As String, ByVal AppendFile As Integer) As Long
' Turn on diagnostics.

' hDmc             Handle to the Galil controller.
' FileName         File name for the diagnostic file.
' AppendFile       True if the file will open for append, otherwise False.

Public Declare Function DMCDiagnosticsOff Lib "dmc32.dll" (ByVal hDmc As Long) As Long
' Turn off diagnostics.

' hDmc             Handle to the Galil controller.

Public Declare Function DMCGetTimeout Lib "dmc32.dll" (ByVal hDmc As Long, Timeout As Long) As Long
' Get current timeout value.

' hDmc             Handle to the Galil controller.
' Timeout          Current timeout value in milliseconds.

Public Declare Function DMCSetTimeout Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Timeout As Long) As Long
' Set timeout value.

' hDmc             Handle to the Galil controller.
' Timeout          Timeout value in milliseconds.

Public Declare Function DMCGetDelay Lib "dmc32.dll" (ByVal hDmc As Long, Delay As Long) As Long
' Get current delay value.
' *** THIS FUNCTION IS OBSOLETE. DELAY IS NO LONGER USED ***

' hDmc             Handle to the Galil controller.
' Delay            Current delay value.

Public Declare Function DMCSetDelay Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Delay As Long) As Long
' Set delay value.
' *** THIS FUNCTION IS OBSOLETE. DELAY IS NO LONGER USED ***

' hDmc             Handle to the Galil controller.
' Delay            Delay value.

Public Declare Function DMCBinaryCommand Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Command As String, ByVal CommandLength As Long, ByVal Response As String, ByVal ResponseLength As Long) As Long
' Send a DMC command in binary format to the Galil controller.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc             Handle to the Galil controller.
' Command          The command to send to the Galil controller.
' CommandLength    The length of the command (binary commands are not null-terminated).
' Response         Buffer to receive the response data. If the buffer is too
'                  small to recieve all the response data from the controller,
'                  the error code DMCERROR_BUFFERFULL will be returned. The
'                  user may get additional response data by calling the
'                  function DMCGetAdditionalResponse. The length of the
'                  additonal response data may ascertained by call the
'                  function DMCGetAdditionalResponseLen. If the response
'                  data from the controller is too large for the internal
'                  additional response buffer, the error code
'                  DMCERROR_RESPONSEDATA will be returned. Output only.
' ResponseLength   Length of the buffer.

Public Declare Function DMCSendBinaryFile Lib "dmc32.dll" (ByVal hDmc As Long, ByVal FileName As String) As Long
' Send a file consisting of DMC commands in binary format to the Galil controller.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc             Handle to the Galil controller.
' FileName         File name to send to the Galil controller.

Public Declare Function DMCCommand_BinaryToAscii Lib "dmc32.dll" (ByVal hDmc As Long, ByVal BinCommand As String, ByVal BinCommandLength As Long, ByVal AscResult As String, ByVal AscResultLength As Long, AscResultReturnedLength As Long) As Long
' Convert a binary DMC command to an ascii DMC command.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc                     Handle to the Galil controller.
' BinCommand               Binary DMC command(s) to be converted.
' BinCommandLength         Length of DMC command(s).
' AscResult                Buffer to receive the translated DMC command.
' AscResultLength          Length of the buffer.
' AscResultReturnedLength  Length of the translated DMC command.

Public Declare Function DMCCommand_AsciiToBinary Lib "dmc32.dll" (ByVal hDmc As Long, ByVal AscCommand As String, ByVal AscCommandLength As Long, ByVal BinResult As String, ByVal BinaryResult As Long, BinResultReturnedLength As Long) As Long
' Convert an ascii DMC command to a binary DMC command.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc                     Handle to the Galil controller.
' AscCommand               Ascii DMC command(s) to be converted.
' AscCommandLength         Length of DMC command(s).
' BinResult                Buffer to receive the translated DMC command.
' BinResultLength          Length of the buffer.
' BinResultReturnedLength  Length of the translated DMC command.

Public Declare Function DMCFile_AsciiToBinary Lib "dmc32.dll" (ByVal hDmc As Long, ByVal InputFileName As String, ByVal OutputFileName As String) As Long
' Convert a file consisting of ascii commands to a file consisting of binary commands.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc              Handle to the Galil controller.
' InputFileName     File name for the input ascii file.
' OutputFileName    File name for the output binary file.

Public Declare Function DMCFile_BinaryToAscii Lib "dmc32.dll" (ByVal hDmc As Long, ByVal InputFileName As String, ByVal OutputFileName As String) As Long
' Convert a file consisting of binary commands to a file consisting of ascii commands.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc              Handle to the Galil controller.
' InputFileName     File name for the input binary file.
' OutputFileName    File name for the output ascii file.

Public Declare Function DMCReadSpecialConversionFile Lib "dmc32.dll" (ByVal hDmc As Long, ByVal FileName As String) As Long
' Read into memory a special BinaryToAscii/AsciiToBinary conversion table.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc              Handle to the Galil controller.
' FileName          File name for the special conversion file.

Public Declare Function DMCAddGalilRegistry Lib "dmc32.dll" (GALILREGISTRY As GALILREGISTRY, Controller As Integer) As Long
' Add a Galil controller to the Windows registry.

' galilregistry    Pointer to a GALILREGISTRY struct.
' Controller       Galil controller number assigned by the successful completion of this function.

Public Declare Function DMCModifyGalilRegistry Lib "dmc32.dll" (ByVal Controller As Integer, GALILREGISTRY As GALILREGISTRY) As Long
' Change a Galil controller in the Windows registry.

' Controller       Galil controller number.
' galilregistry    Pointer to a GALILREGISTRY struct.

Public Declare Function DMCDeleteGalilRegistry Lib "dmc32.dll" (ByVal Controller As Integer) As Long
' Delete a Galil controller in the Windows registry.

' Controller       Galil controller number. Use -1 to delete all Galil controllers.

Public Declare Function DMCGetGalilRegistryInfo Lib "dmc32.dll" (ByVal Controller As Integer, GALILREGISTRY As GALILREGISTRY) As Long
' Get Windows registry information for a given Galil controller.

' Controller       Galil controller number.
' galilregistry    Pointer to a GALILREGISTRY struct.

Public Declare Function DMCRegisterPnpControllers Lib "dmc32.dll" (count As Integer) As Long
' Update Windows registry for all Galil Plug-and-Play (PnP) controllers. This function
' will add new controllers to the registry or update existing ones.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' Count             Pointer to the number of Galil PnP controllers registered (and/or updated).

Public Declare Function DMCSelectController Lib "dmc32.dll" (ByVal hwnd As Long) As Integer
' Select a Galil motion controller from a list of registered controllers. Returns the
' selected controller number or -1 if no controller was selected.
' NOTE: This function invokes a dialog window.

' hwnd              The window handle of the calling application. If NULL, the
'                   window with the current input focus is used.

Public Declare Sub DMCEditRegistry Lib "dmc32.dll" (ByVal hwnd As Integer)
' Edit the Windows registry: add, change, or delete Galil motion controllers.
' NOTE: This function invokes a dialog window.

' hwnd              The window handle of the calling application. If NULL, the
'                   window with the current input focus is used.

Public Declare Function DMCWaitForMotionComplete Lib "dmc32.dll" (ByVal hDmc As Long, ByVal Axes As String, ByVal DispatchMsgs As Integer) As Long
' Wait for motion complete by creating a thread to query the controller. The function returns
' when motion is complete.

' hDmc              Handle to the Galil controller.
' Axes              Which axes to wait for: X, Y, Z, W, E, F, G, H, or S for
'                   coordinated motion. To wait for more than one axis (other than
'                   coordinated motion), simply concatenate the axis letters in the string.
' DispatchMsgs      Set to TRUE if you want to get and dispatch Windows messages
'                   while waiting for motion complete. This flag is always TRUE for Win16.

Public Declare Function DMCDownloadFirmwareFile Lib "dmc32.dll" (ByVal hDmc As Long, ByVal FileName As String, ByVal DisplayDialog As Integer) As Long
' Update the controller's firmware. This function will open a binary firmware file and refresh
' the flash EEPROM of the controller.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc              Handle to the Galil controller.
' FileName          File name to download to the Galil controller.
' DisplayDialog     Display a progress dialog to the user.

#Else

Public Declare Function DMCOpen Lib "dmc16.dll" (ByVal Controller As Integer, ByVal hwnd As Integer, phDmc As Long) As Long
' Open communications with the Galil controller.

' Controller       A number between 1 and 16. Up to 16 Galil controllers may be
'                  addressed per process.
' hWnd             The window handle to use for notifying the application
'                  program of an interrupt.
' phDmc            Handle to the Galil controller to be use for all subsequent
'                  API calls.

Public Declare Function DMCOpen2 Lib "dmc16.dll" (ByVal Controller As Integer, ByVal ThreadID As Long, phDmc As Long) As Long
' Open communications with the Galil controller.

' Controller       A number between 1 and 16. Up to 16 Galil controllers may be
'                  addressed per process.
' ThreadID         The thread id to use for notifying the application
'                  program of an interrupt.
' phDmc            Handle to the Galil controller to be use for all subsequent
'                  API calls.

Public Declare Function DMCGetHandle Lib "dmc16.dll" (ByVal Controller As Integer, phDmc As Long) As Long
' Get the handle associated with a particular Galil controller.

' Controller       A number between 1 and 16. Up to 16 Galil controllers may be
'                  addressed per process.
' phDmc            Handle to the Galil controller to be use for all subsequent
'                  API calls.

Public Declare Function DMCClose Lib "dmc16.dll" (ByVal hDmc As Long) As Long
' Close communications with the Galil controller.

' hDmc             Handle to the Galil controller.

Public Declare Function DMCCommand Lib "dmc16.dll" (ByVal hDmc As Long, ByVal CommandString As String, ByVal Response As String, ByVal ResponseLength As Long) As Long
' Send a command to the Galil controller.
' NOTE: This function can only send commands or groups of commands up to
' 1024 bytes long.

' hDmc             Handle to the Galil controller.
' CommandString    The command to send to the Galil controller.
' Response         Buffer to receive the response data.
' ResponseLength   Length of the buffer.

Public Declare Function DMCFastCommand Lib "dmc16.dll" (ByVal hDmc As Long, ByVal CommandString As String) As Long
' Send a command to the Galil controller without the overhead of waiting for a response. Use this function with
' caution as command errors will not be reported and the out-going FIFO or communciations buffer
' may fill up. This function is intended to be used in routines which provide data records for the Galil
' DL and QD commands which do not return a response. Other uses may be to send contour data.
' NOTE: This function can only send commands or groups of commands up to
' 1024 bytes long.

' hDmc             Handle to the Galil controller.
' CommandString    The command to send to the Galil controller.

Public Declare Function DMCGetUnsolicitedResponse Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Response As String, ByVal ResponseLength As Long) As Long
' Query the Galil controller for unsolicited responses. These are messages
' output from programs running in the background in the Galil controller.

' hDmc             Handle to the Galil controller.
' Response         Buffer to receive the response data.
' ResponseLength   Length of the buffer.

Public Declare Function DMCWriteData Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Buffer As String, ByVal BufferLength As Long, BytesWritten As Long) As Long
' Low-level I/O routine to write data to the Galil controller. Data is written
' to the Galil controller only if it is "ready" to receive it. The function
' will attempt to write exactly cbBuffer characters to the controller.
' NOTE: For Win32 and WinRT driver the maximum number of bytes which can be written
' each time is 64. There are no restrictions with the Galil driver.

' hDmc             Handle to the Galil controller.
' Buffer           Buffer to write the data from. Data does not need to be
'                  NULL terminated.
' BufferLength     Length of the data in the buffer.
' BytesWritten     Number of bytes written.

Public Declare Function DMCReadData Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Buffer As String, ByVal BufferLength As Long, BytesRead As Long) As Long
' Low-level I/O routine to read data from the Galil controller. The routine
' will read what ever is currently in the FIFO (bus controller) or
' communications port input queue (serial controller). The function will read
' up to cbBuffer characters from the controller. The data placed in the user
' buffer (pchBuffer) is NOT NULL terminated. The data returned is not guaranteed
' to be a complete response - you may have to call this function repeatedly to
' get a complete response.
' NOTE: For Win32 and WinRT driver the maximum number of bytes which can be read
' each time is 64. There are no restrictions with the Galil driver.

' hDmc             Handle to the Galil controller.
' Buffer           Buffer to read the data into. Data will not be NULL
'                  terminated.
' BufferLength     Length of the buffer.
' BytesRead        Number of bytes read.

Public Declare Function DMCGetAdditionalResponseLen Lib "dmc16.dll" (ByVal hDmc As Long, ResponseLength As Long) As Long
' Query the Galil controller for the length of the additional response data. There will be more
' response data available if DMCCommand returned DMCERROR_BUFFERFULL.

' hDmc             Handle to the Galil controller.
' ResponseLength   Length of the additional response data.

Public Declare Function DMCGetAdditionalResponse Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Response As String, ByVal ResponseLength As Long) As Long
' Query the Galil controller for more response data. There will be more
' response data available if DMCCommand returned DMCERROR_BUFFERFULL.

' hDmc             Handle to the Galil controller.
' Response         Buffer to receive the response data.
' ResponseLength   Length of the buffer.

Public Declare Function DMCError Lib "dmc16.dll" (ByVal hDmc As Long, ByVal errorcode As Long, ByVal Message As String, ByVal MessageLength As Long) As Long
' Retrieve the error message text from a DMCERROR_COMMAND error.

' hDmc             Handle to the Galil controller.
' ErrorCode        Error returned from API function.
' Message          Buffer to receive the error message text.
' MessageLength    Length of the buffer.

Public Declare Function DMCClear Lib "dmc16.dll" (ByVal hDmc As Long) As Long
' Clear the Galil controller FIFO.

' hDmc             Handle to the Galil controller.

Public Declare Function DMCReset Lib "dmc16.dll" (ByVal hDmc As Long) As Long
' Reset the Galil controller.

' hDmc             Handle to the Galil controller.

Public Declare Function DMCMasterReset Lib "dmc16.dll" (ByVal hDmc As Long) As Long
' Master reset the Galil controller.

' hDmc             Handle to the Galil controller.

Public Declare Function DMCVersion Lib "dmc16.dll" (ByVal hDmc As Long, ByVal version As String, ByVal VersionLength As Long) As Long
' Get the version of the Galil controller.

' hDmc             Handle to the Galil controller.
' Version          Buffer to receive the version information.
' VersionLength    Length of the buffer.

Public Declare Function DMCDownloadFile Lib "dmc16.dll" (ByVal hDmc As Long, ByVal FileName As String, ByVal Label As String) As Long
' Download a file to the Galil controller.

' hDmc             Handle to the Galil controller.
' FileName         File name to download to the Galil controller.
' Label            Program label to download to. This argument is ignored if
'                  NULL.

Public Declare Function DMCDownloadFromBuffer Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Buffer As String, ByVal Label As String) As Long
' Download a file to the Galil controller.

' hDmc             Handle to the Galil controller.
' Buffer           Buffer of DMC commands to download to the Galil controller.
' Label            Program label to download to. This argument is ignored if
'                  NULL.

Public Declare Function DMCUploadFile Lib "dmc16.dll" (ByVal hDmc As Long, ByVal FileName As String) As Long
' Upload a file from the Galil controller.

' FileName         File name to upload from the Galil controller.

Public Declare Function DMCUploadToBuffer Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Buffer As String, ByVal BufferLength As Long) As Long
' Upload a file from the Galil controller.

' Buffer           Buffer of DMC commands to upload from the Galil controller.
' BufferLength     Length of the buffer.

Public Declare Function DMCSendFile Lib "dmc16.dll" (ByVal hDmc As Long, ByVal FileName As String) As Long
' Send a file to the Galil controller.

' hDmc             Handle to the Galil controller.
' FileName         File name to send to the Galil controller.

Public Declare Function DMCArrayDownload Lib "dmc16.dll" (ByVal hDmc As Long, ByVal ArrayName As String, ByVal FirstElement As Integer, ByVal LastElement As Integer, ByVal Data As String, ByVal DataLength As Long, BytesWritten As Long) As Long
' Download an array to the Galil controller. The array must exist. Array data can be
' delimited by a comma or CR (0x0D) or CR/LF (0x0D0A).
' NOTE: The firmware on the controller must be recent enough to support the QD command.

' hDmc             Handle to the Galil controller.
' ArrayName        Array name to download to the Galil controller.
' FirstElement     First array element.
' LastElement      Last array element.
' Data             Buffer to write the array data from. Data does not need to be
'                  NULL terminated.
' DataLength       Length of the array data in the buffer.
' BytesWritten     Number of bytes written.

Public Declare Function DMCArrayUpload Lib "dmc16.dll" (ByVal hDmc As Long, ByVal ArrayName As String, ByVal FirstElement As Integer, ByVal LastElement As Integer, ByVal Buffer As String, ByVal BufferLength As Long, BytesRead As Long, ByVal Comma As Integer) As Long
' Upload an array from the Galil controller. The array must exist. Array data will be
' delimited by a comma or CR (0x0D) depending of the value of fComma.
' NOTE: The firmware on the controller must be recent enough to support the QU command.

' hDmc             Handle to the Galil controller.
' ArrayName        Array name to upload from the Galil controller.
' FirstElement     First array element.
' LastElement      Last array element.
' Buffer           Buffer to read the array data into. Array data will not be
'                  NULL terminated.
' BufferLength     Length of the buffer.
' BytesRead        Number of bytes read.

Public Declare Function DMCRefreshDataRecord Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Length As Long) As Long
' Refresh the data record used for fast polling.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc             Handle to the Galil controller.
' Length           Refresh size in bytes. Set to 0 unless you do not want a full-buffer
'                  refresh.

Public Declare Function DMCGetDataRecord Lib "dmc16.dll" (ByVal hDmc As Long, ByVal GeneralOffset As Integer, ByVal AxisInfoOffset As Integer, DataType As Integer, Data As Long) As Long
' Get a data item from the data record used for fast polling.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc             Handle to the Galil controller.
' GeneralOffset    Data record offset for general data item.
' AxisInfoOffset   Additional data record offset for axis data item.
' DataType         Data type of the data item. If you are using the standard,
'                  pre-defined offsets, set this argument to zero before calling this
'                  function. The actual data type of the data item is returned on output.
' Data             Buffer to receive the data record data. Output only.

Public Declare Function DMCDiagnosticsOn Lib "dmc16.dll" (ByVal hDmc As Long, ByVal FileName As String, ByVal AppendFile As Integer) As Long
' Turn on diagnostics.

' hDmc             Handle to the Galil controller.
' FileName         File name for the diagnostic file.
' AppendFile       True if the file will open for append, otherwise False.

Public Declare Function DMCDiagnosticsOff Lib "dmc16.dll" (ByVal hDmc As Long) As Long
' Turn off diagnostics.

' hDmc             Handle to the Galil controller.

Public Declare Function DMCGetTimeout Lib "dmc16.dll" (ByVal hDmc As Long, Timeout As Long) As Long
' Get current timeout value.

' hDmc             Handle to the Galil controller.
' Timeout          Current timeout value in milliseconds.

Public Declare Function DMCSetTimeout Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Timeout As Long) As Long
' Set timeout value.

' hDmc             Handle to the Galil controller.
' Timeout          Timeout value in milliseconds.

Public Declare Function DMCGetDelay Lib "dmc16.dll" (ByVal hDmc As Long, Delay As Long) As Long
' Get current delay value.
' *** THIS FUNCTION IS OBSOLETE. DELAY IS NO LONGER USED ***

' hDmc             Handle to the Galil controller.
' Delay            Current delay value.

Public Declare Function DMCSetDelay Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Delay As Long) As Long
' Set delay value.
' *** THIS FUNCTION IS OBSOLETE. DELAY IS NO LONGER USED ***

' hDmc             Handle to the Galil controller.
' Delay            Delay value.

Public Declare Function DMCBinaryCommand Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Command As String, ByVal CommandLength As Long, ByVal Response As String, ByVal ResponseLength As Long) As Long
' Send a DMC command in binary format to the Galil controller.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc             Handle to the Galil controller.
' Command          The command to send to the Galil controller.
' CommandLength    The length of the command (binary commands are not null-terminated).
' Response         Buffer to receive the response data. If the buffer is too
'                  small to recieve all the response data from the controller,
'                  the error code DMCERROR_BUFFERFULL will be returned. The
'                  user may get additional response data by calling the
'                  function DMCGetAdditionalResponse. The length of the
'                  additonal response data may ascertained by call the
'                  function DMCGetAdditionalResponseLen. If the response
'                  data from the controller is too large for the internal
'                  additional response buffer, the error code
'                  DMCERROR_RESPONSEDATA will be returned. Output only.
' ResponseLength   Length of the buffer.

Public Declare Function DMCSendBinaryFile Lib "dmc16.dll" (ByVal hDmc As Long, ByVal FileName As String) As Long
' Send a file consisting of DMC commands in binary format to the Galil controller.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc             Handle to the Galil controller.
' FileName         File name to send to the Galil controller.

Public Declare Function DMCCommand_BinaryToAscii Lib "dmc16.dll" (ByVal hDmc As Long, ByVal BinCommand As String, ByVal BinCommandLength As Long, ByVal AscResult As String, ByVal AscResultLength As Long, AscResultReturnedLength As Long) As Long
' Convert a binary DMC command to an ascii DMC command.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc                     Handle to the Galil controller.
' BinCommand               Binary DMC command(s) to be converted.
' BinCommandLength         Length of DMC command(s).
' AscResult                Buffer to receive the translated DMC command.
' AscResultLength          Length of the buffer.
' AscResultReturnedLength  Length of the translated DMC command.

Public Declare Function DMCCommand_AsciiToBinary Lib "dmc16.dll" (ByVal hDmc As Long, ByVal AscCommand As String, ByVal AscCommandLength As Long, ByVal BinResult As String, ByVal BinaryResult As Long, BinResultReturnedLength As Long) As Long
' Convert an ascii DMC command to a binary DMC command.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc                     Handle to the Galil controller.
' AscCommand               Ascii DMC command(s) to be converted.
' AscCommandLength         Length of DMC command(s).
' BinResult                Buffer to receive the translated DMC command.
' BinResultLength          Length of the buffer.
' BinResultReturnedLength  Length of the translated DMC command.

Public Declare Function DMCFile_AsciiToBinary Lib "dmc16.dll" (ByVal hDmc As Long, ByVal InputFileName As String, ByVal OutputFileName As String) As Long
' Convert a file consisting of ascii commands to a file consisting of binary commands.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc              Handle to the Galil controller.
' InputFileName     File name for the input ascii file.
' OutputFileName    File name for the output binary file.

Public Declare Function DMCFile_BinaryToAscii Lib "dmc16.dll" (ByVal hDmc As Long, ByVal InputFileName As String, ByVal OutputFileName As String) As Long
' Convert a file consisting of binary commands to a file consisting of ascii commands.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc              Handle to the Galil controller.
' InputFileName     File name for the input binary file.
' OutputFileName    File name for the output ascii file.

Public Declare Function DMCReadSpecialConversionFile Lib "dmc16.dll" (ByVal hDmc As Long, ByVal FileName As String) As Long
' Read into memory a special BinaryToAscii/AsciiToBinary conversion table.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' hDmc              Handle to the Galil controller.
' FileName          File name for the special conversion file.

Public Declare Function DMCAddGalilRegistry Lib "dmc16.dll" (GALILREGISTRY As GALILREGISTRY, Controller As Integer) As Long
' Add a Galil controller to the Windows registry.

' galilregistry    Pointer to a GALILREGISTRY struct.
' Controller       Galil controller number assigned by the successful completion of this function.

Public Declare Function DMCModifyGalilRegistry Lib "dmc16.dll" (ByVal Controller As Integer, GALILREGISTRY As GALILREGISTRY) As Long
' Change a Galil controller in the Windows registry.

' Controller       Galil controller number.
' galilregistry    Pointer to a GALILREGISTRY struct.

Public Declare Function DMCDeleteGalilRegistry Lib "dmc16.dll" (ByVal Controller As Integer) As Long
' Delete a Galil controller in the Windows registry.

' Controller       Galil controller number. Use -1 to delete all Galil controllers.

Public Declare Function DMCGetGalilRegistryInfo Lib "dmc16.dll" (ByVal Controller As Integer, GALILREGISTRY As GALILREGISTRY) As Long
' Get Windows registry information for a given Galil controller.

' Controller       Galil controller number.
' galilregistry    Pointer to a GALILREGISTRY struct.

Public Declare Function DMCRegisterPnpControllers Lib "dmc16.dll" (count As Integer) As Long
' Update Windows registry for all Galil Plug-and-Play (PnP) controllers. This function
' will add new controllers to the registry or update existing ones.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.

' Count             Pointer to the number of Galil PnP controllers registered (and/or updated).

Public Declare Function DMCSelectController Lib "dmc16.dll" (ByVal hwnd As Integer) As Integer
' Select a Galil motion controller from a list of registered controllers. Returns the
' selected controller number or -1 if no controller was selected.
' NOTE: This function invokes a dialog window.

' hwnd              The window handle of the calling application. If NULL, the
'                   window with the current input focus is used.

Public Declare Sub DMCEditRegistry Lib "dmc16.dll" (ByVal hwnd As Integer)
' Edit the Windows registry: add, change, or delete Galil motion controllers.
' NOTE: This function invokes a dialog window.

' hwnd              The window handle of the calling application. If NULL, the
'                   window with the current input focus is used.

Public Declare Function DMCWaitForMotionComplete Lib "dmc16.dll" (ByVal hDmc As Long, ByVal Axes As String, ByVal DispatchMsgs As Integer) As Long
' Wait for motion complete by creating a thread to query the controller. The function returns
' when motion is complete.

' hDmc              Handle to the Galil controller.
' Axes              Which axes to wait for: X, Y, Z, W, E, F, G, H, or S for
'                   coordinated motion. To wait for more than one axis (other than
'                   coordinated motion), simply concatenate the axis letters in the string.
' DispatchMsgs      Set to TRUE if you want to get and dispatch Windows messages
'                   while waiting for motion complete. This flag is always TRUE for Win16.

Public Declare Function DMCDownloadFirmwareFile Lib "dmc16.dll" (ByVal hDmc As Long, ByVal FileName As String, ByVal DisplayDialog As Integer) As Long
' Update the controller's firmware. This function will open a binary firmware file and refresh
' the flash EEPROM of the controller.
' NOTE: This function is for the DMC-1600 and DMC-1700 only.
' hDmc              Handle to the Galil controller.
' FileName          File name to download to the Galil controller.
' DisplayDialog     Display a progress dialog to the user.
#End If

Sub gotoposxy(gotox$, gotoy$)
    If hDmc = -1 Then
        If flagdopsi% = 0 Then FrmEuro.Enabled = True
        Exit Sub
    End If
    If gotox$ <> "" And gotoy$ <> "" Then
        Call galilcmd("ST XYZ", res$, ermsg$)
        tcmd$ = "SP" + Str(1.5 * speedmaxxy&) + "," + Str(1.5 * speedmaxxy&)
        Call galilcmd(tcmd$, res$, ermsg$)
        tcmd$ = "PA " + gotox$ + "," + gotoy$ + ";BG XY"
        Call galilcmd(tcmd$, res$, ermsg$)
        FrmEuro.TimerMove.Interval = 100
        FrmEuro.TimerMove.Enabled = True
        moving% = 1
    End If
End Sub

Sub gotoposzw(gotoz$, gotow$)
    dontprobe% = 0
    If hDmc = -1 Then
        If flagdopsi% = 0 Then FrmEuro.Enabled = True
        Exit Sub
    End If
    If fouraxes% = 0 Then Exit Sub
    Call galilcmd("TDZW", res$, ermsg$)
    tres$ = Trim$(res$)
    cm% = InStr(tres$, ",")
    If cm% > 0 Then
        TGotoZ$ = Trim$(Left$(tres$, cm% - 1))
        TGotoW$ = Trim$(Mid$(tres$, cm% + 1))
    End If
    If gotoz$ <> "" And gotow$ <> "" Then
        If Abs(Val(TGotoZ$) - Val(gotoz$)) > 5 Or Abs(Val(TGotoW$) - Val(gotow$)) > 5 Then
            Call ProbeUp(checkok%)
            If checkok% = 0 Then
                MsgBox ("Unable to raise probes")
                FrmEuro.TimerMove.Interval = 100
                FrmEuro.TimerMove.Enabled = True
                moving% = 1
                Exit Sub
            End If
        End If
        Call galilcmd("ST ZW", res$, ermsg$)
        tcmd$ = "SP ,," + Str(SpeedMaxTavola) + "," + Str(SpeedMaxFuoco)
        Call galilcmd(tcmd$, res$, ermsg$)
        tcmd$ = "PA ,," + gotoz$ + "," + gotow$ + ";BG ZW"
        Call galilcmd(tcmd$, res$, ermsg$)
        FrmEuro.TimerMove.Interval = 100
        FrmEuro.TimerMove.Enabled = True
        moving% = 1
    End If
End Sub


Sub gotoposz(gotoz$)
    dontprobe% = 0
    Call ProbeUp
    If hDmc = -1 Then
        If flagdopsi% = 0 Then FrmEuro.Enabled = True
        Exit Sub
    End If
    Call galilcmd("TDZ", res$, ermsg$)
    tres$ = Trim$(res$)
    cm% = InStr(tres$, ",")
    If cm% > 0 Then
        TGotoZ$ = Trim$(Mid$(tres$, cm% + 1))
    End If
    If gotoz$ <> "" Then
        Call galilcmd("ST Z", res$, ermsg$)
        tcmd$ = "SP ,," + Str(SpeedMaxTavola)
        Call galilcmd(tcmd$, res$, ermsg$)
        tcmd$ = "PA ,," + gotoz$ + ";BG Z"
        Call galilcmd(tcmd$, res$, ermsg$)
        FrmEuro.TimerMove.Interval = 100
        FrmEuro.TimerMove.Enabled = True
        moving% = 1
    End If
End Sub


Sub gotoposw(gotow$)
    dontprobe% = 0
    If hDmc = -1 Then
        If flagdopsi% = 0 Then FrmEuro.Enabled = True
        Exit Sub
    End If
    Call galilcmd("TDW", res$, ermsg$)
    tres$ = Trim$(res$)
    cm% = InStr(tres$, ",")
    If cm% > 0 Then
        TGotoW$ = Trim$(Mid$(tres$, cm% + 1))
    End If
    If gotow$ <> "" Then
        Call galilcmd("ST W", res$, ermsg$)
        tcmd$ = "SP ,,," + Str(SpeedMaxFuoco)
        Call galilcmd(tcmd$, res$, ermsg$)
        tcmd$ = "PA ,,," + gotow$ + ";BG W"
        Call galilcmd(tcmd$, res$, ermsg$)
        FrmEuro.TimerMove.Interval = 100
        FrmEuro.TimerMove.Enabled = True
        moving% = 1
    End If
End Sub



Sub gotoposxyzw(gotox$, gotoy$, gotoz$, gotow$)
    dontprobe% = 0
    If hDmc = -1 Then
        If flagdopsi% = 0 Then FrmEuro.Enabled = True
        Exit Sub
    End If
    If fouraxes% = 0 Then
        Call gotoposxy(gotox$, gotoy$)
        Exit Sub
    End If
    '  caso di zw azionati con motori in continua Call galilcmd("TPZW", res$, ermsg$)
    Call galilcmd("TDZW", res$, ermsg$)
    tres$ = Trim$(res$)
    cm% = InStr(tres$, ",")
    If cm% > 0 Then
        TGotoZ$ = Trim$(Left$(tres$, cm% - 1))
        TGotoW$ = Trim$(Mid$(tres$, cm% + 1))
    End If
    If gotox$ <> "" And gotoy$ <> "" And gotoz$ <> "" And gotow$ <> "" Then
        If Abs(Val(TGotoZ$) - Val(gotoz$)) > 5 Or Abs(Val(TGotoW$) - Val(gotow$)) > 5 Then
            Call ProbeUp(checkok%)
            If checkok% = 0 Then
                MsgBox ("Unable to raise probes")
                FrmEuro.TimerMove.Interval = 100
                FrmEuro.TimerMove.Enabled = True
                moving% = 1
                Exit Sub
            End If
        End If
        Call galilcmd("ST XYZW", res$, ermsg$)
        tcmd$ = "SP" + Str(1.5 * speedmaxxy&) + "," + Str(1.5 * speedmaxxy&) + "," + Str(SpeedMaxTavola) + "," + Str(SpeedMaxFuoco)
        Call galilcmd(tcmd$, res$, ermsg$)
        tcmd$ = "PA " + gotox$ + "," + gotoy$ + "," + gotoz$ + "," + gotow$ + ";BG XYZW"
        Call galilcmd(tcmd$, res$, ermsg$)
        FrmEuro.TimerMove.Interval = 100
        FrmEuro.TimerMove.Enabled = True
        moving% = 1
    End If
End Sub

Sub galilcmd(tcmd$, res$, ermsg$)
    'invia un comando al controller e verifica se ci sono errori sul comando
    If debugging% = 1 Then Call updatedebug(tcmd$)
    tcmd$ = UCase$(tcmd$)
    Response$ = Space$(256)
    Rl% = 256
    If res$ = "A" Then
        Rl% = 2560
        res$ = ""
        Response$ = Space$(Rl%)
    End If
    ermsg$ = ""
    If hDmc <> -1 Then
        rc& = DMCCommand(hDmc, tcmd$, Response$, Rl%)
        Call checkerr(tcmd$, rc&, ermsg$)
        cr% = InStr(Response$, Chr$(13) + Chr$(10))
        If cr% > 0 Then res$ = Trim$(Left$(Response$, cr% - 1))
        If debugging% = 1 Then Call updatedebug(res$)
   
    Else
        ermsg$ = mss$(117)
    End If
 End Sub
 
Sub opengalil()
    If hDmc <> -1 Then Exit Sub
    Controller% = 1
    For i% = 1 To 10
        rc& = DMCOpen(Controller%, 0, hDmc)
        
        If rc& = 0 Then Exit For
    Next i%
    Call checkerr("OPEN", rc&, ermsg$)
    If rc& = 0 Then
        Call updatelog(mss$(118), 0)
        Rl% = 256
        'Response$ = Space$(256)
        'RC = DMCVersion(hDmc, Response$, Rl%)
        Call updatelog(mss$(119), 2)
        rc& = DMCClear(hDmc)
        Call checkerr("CLEAR", rc&, ermsg$)
        Call galilcmd(";", res$, ermsg$)
        Call galilcmd("AB", res$, ermsg$)
        Call galilcmd("MO", res$, ermsg$)
        Call galilcmd("SH XYZW", res$, ermsg$)
        Call galilcmd("AC 600000,600000,200000,200000", res$, ermsg$)
        Call galilcmd("DC 600000,600000,200000,200000", res$, ermsg$)
        Call galilcmd("CDONE=0", res$, ermsg$)
        rc& = DMCDownloadFile(hDmc, thepath$ + downfile$, "")
        Call checkerr("DOWNLOAD", rc&, ermsg$)
        Call updatelog(mss$(120), 2)
        td& = timeGetTime&()
        Do
            Call readdevice(0, 0, 5)
            ttdd& = timeGetTime&()
            If ttdd& - td& > 5000 Then
                Call updatelog(mss$(121), 1)
                Exit Do
            End If
            DoEvents
        Loop While d0.I_Inibizione_Azionamenti = 1
        'Call ProbeUp(checkok%)
        checkok% = 1
        If checkok% = 0 Then
            MsgBox ("Unable to raise probes")
        Else
            Call galilcmd("MO", res$, ermsg$)
'            Call galilcmd("XQ #HOME,1", res$, ermsg$)
        End If
    Else
        hDmc = -1
        Call updatelog(mss$(122), 1)
    End If
End Sub

Sub checkerr(tcmd$, errcode&, ermsg$)
        If errcode& <> 0 Then
            ermsg$ = Space$(256)
            sc& = DMCError(hDmc, errcode&, ermsg$, 256&)
            ch% = InStr(ermsg$, Chr$(0))
            If ch% > 0 Then ermsg$ = Left$(ermsg$, ch% - 1)
            ermsg$ = Trim$(ermsg$)
            If ermsg$ <> "" Then Call updatelog("Galil Error>" + tcmd$ + ":" + ermsg$, 1)
        End If
End Sub

