library mtrapi32; uses Windows, Messages, SysUtils, Classes, Motors; {$R *.res} var SMC: TStepperMotorCard; { SMC = Stepper Motor Card } procedure SMCDisconnect; stdcall; begin if SMC <> nil then begin SMC.Close; FreeAndNil(SMC); end; end; function SMCConnect(Port: PChar; Motors: DWORD): BOOL; stdcall; begin // Close and destroy any open cards SMCDisconnect; // Card factory case Motors of 1: SMC := TK8096.Create(nil); 4: SMC := TK8097.Create(nil); end; // Connection if SMC <> nil then begin SMC.Port := Port; try SMC.Open; except // just want to supress the error end; end; // Report Result := (SMC <> nil) and (SMC.Connected); end; function SMCGetMotorCount: DWORD; stdcall; begin if (SMC <> nil) then Result := SMC.Motors.Count else Result := 0; end; function SMCGetInputCount: DWORD; stdcall; begin if (SMC <> nil) then Result := SMC.Inputs.Count else Result := 0; end; function SMCGetOutputCount: DWORD; stdcall; begin if (SMC <> nil) then Result := SMC.Outputs.Count else Result := 0; end; function SMCConnected: BOOL; stdcall; begin Result := (SMC <> nil) and (SMC.Connected); end; procedure SMCSetDemo(Enabled: BOOL); stdcall; begin if SMC <> nil then SMC.TxDemo(Enabled); end; function SMCDemo: BOOL; stdcall; begin if SMC <> nil then Result := SMC.Demo else Result := False; end; procedure SMCMove(Motor, Steps: DWORD; Direction: DWORD; Speed: DWORD); stdcall; begin if (SMC <> nil) and (Motor >= 0) and (Motor < SMC.Motors.Count) then begin SMC.TxStop(Motor); if not (Direction in [0,1]) then // Correct invalid values Direction := 0; SMC.TxMove(Motor, Steps, TStepperMotorDirection(Direction), Speed); end; end; procedure SMCStop(Motor: DWORD); stdcall; begin if (SMC <> nil) and (Motor >= 0) and (Motor < SMC.Motors.Count) then begin SMC.TxStop(Motor); end; end; function SMCGetMoving(Motor: DWORD): BOOL; stdcall; begin if (SMC <> nil) and (Motor >= 0) and (Motor < SMC.Motors.Count) then Result := SMC.Motors[Motor].Moving else Result := False; end; function SMCGetDirection(Motor: DWORD): DWORD; stdcall; begin if (SMC <> nil) and (Motor >= 0) and (Motor < SMC.Motors.Count) then Result := DWORD(SMC.Motors[Motor].Direction) else Result := DWORD(mdLeft); end; procedure SMCSetTorque(Motor: DWORD; Enabled: BOOL); stdcall; begin if (SMC <> nil) and (Motor >= 0) and (Motor < SMC.Motors.Count) then begin SMC.TxTorque(Motor, Enabled); end; end; function SMCGetTorque(Motor: DWORD): BOOL; stdcall; begin if (SMC <> nil) and (Motor >= 0) and (Motor < SMC.Motors.Count) then Result := SMC.Motors[Motor].Torque else Result := False; end; function SMCGetInput(Input: DWORD): BOOL; stdcall; begin if (SMC <> nil) and (Input >= 0) and (Input < SMC.Inputs.Count) then Result := SMC.Inputs[Input].Active else Result := False; end; procedure SMCSetOutput(Active: BOOL); stdcall; begin if SMC <> nil then begin SMC.TxOutput(Active); end; end; function SMCGetOutput: BOOL; stdcall; begin if SMC <> nil then Result := SMC.Outputs[0].Active else Result := False; end; procedure Initialize; begin SMC := nil; end; procedure Finalize; begin SMCDisconnect; end; procedure DllMain(Reason: DWORD); begin case Reason of DLL_PROCESS_ATTACH: Initialize; DLL_PROCESS_DETACH: Finalize; end; end; exports SMCConnect; exports SMCDisconnect; exports SMCConnected; exports SMCGetMotorCount; exports SMCGetInputCount; exports SMCGetOutputCount; exports SMCSetDemo; exports SMCDemo; exports SMCMove; exports SMCStop; exports SMCSetTorque; exports SMCGetMoving; exports SMCGetDirection; exports SMCGetTorque; exports SMCGetInput; exports SMCSetOutput; exports SMCGetOutput; begin DllProc := @DllMain; DllProc(DLL_PROCESS_ATTACH); end.