// protocol for control SevenOcean's Machine // ////////////////////////////////////////////////////////////////////// #ifndef AFX_KEYENCE_Proto_H__B422904C_2CEB_495B_B7BD_B45AB30286DD__INCLUDED_ #define AFX_KEYENCE_Proto_H__B422904C_2CEB_495B_B7BD_B45AB30286DD__INCLUDED_ #define KEYENCE_TRACE if ( g_pLogger && g_pLogger->m_lLogMask & LOGACTIONS ) g_pLogger->SendAndFlushPerMode #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "..\Tools\UsbUtility\logger.h" #include "..\..\..\..\..\ThirdParty\UsbSupport\LibUsb_Win\Include\lusb0_usb.h" #include "..\MicroVu\SsiStatus.h" #define MAX_BUFF_SIZE 0x200 #define USB_ENDPOINT_TYPE_CONTROL 0 #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 #define USB_ENDPOINT_TYPE_BULK 2 #define USB_ENDPOINT_TYPE_INTERRUPT 3 #define USB_DEVICE_DESCRIPTOR_TYPE 1 #define USB_CONFIGURATION_DESCRIPTOR_TYPE 2 #define THREAD_RUNNING 0 #define THREAD_PAUSED 1 #define THREAD_EXIT -1 // Device configuration and interface id. #define KEYENCE_USB_CONFIG 1 #define KEYENCE_USB_INTF 0 #define EP_KEYENCE_01 0x01 #define EP_KEYENCE_02 0x02 #define EP_KEYENCE_03 0x03 #define EP_KEYENCE_81 0x82 #define EP_KEYENCE_82 0x81 #define EP_KEYENCE_84 0x84 #define EP_01_CMD_IDX 0 // index to usb buffers #define EP_81_DATA_IDX 1 // #define EP_02_CMD_IDX 2 // #define EP_82_DATA_IDX 3 // #define EP_03_CMD_IDX 4 // #define EP_84_DATA_IDX 5 // #define lEPSIZE 6 #define MAXRINGS 5 // for MicroVu Vertex 220 #define MAXSEGS 8 #pragma pack(push) #pragma pack(1) //**************************************************************************************************** // Set the _status to Idle after reply data has been received. // How to recover? In order to send data, _status must be Idle. If the system is not available // for 1 second, assume something wrong and treat it as "TimeOut". // Another way to do this is to lock this structure is to use a mutex to ensure single-threaded // access. //**************************************************************************************************** //==================================================================================================== struct struct_Keyence_machine { // g_machine structure struct s_status { bool _machine_running; } s_status; }; #define KEYENCE_VID 0x0720 #define KEYENCE_PID 0x000A //**************************************************************************************************** // Binary SevenOcean command structure, out going // //**************************************************************************************************** #define pKEYENCE_CMD_01 ((s_KEYENCE_CMD_BUFF_01 *) ep_buff[EP_01_CMD_IDX]._buffer) struct s_KEYENCE_CMD_BUFF_01 { UCHAR uCmdByte; //---------------------------------------------------------------------- // [endpoint 0x00000001] 00000060 : 530001010100fffe1d89ffff4d37fffebeee000000000000c34e0000021a0000003200000000000f423b000029fb0000138800000000003d08ee00030d4000030d40000000000000000000000000000000000000000000000000000000000000 // *Command : 53 Move To -- X Y Z ZM Scale-0123511 -0045769 -0082194 00000000 // *Speed : 00049998 00000538 00000050 00000000 // *Unk1 : 00999995 00010747 00005000 00000000 // *Unk2 : 03999982 00200000 00200000 00000000 union { struct { BYTE uSubCmdByte; char data[9]; }S_KEYENCE_CMD_MOVE_XYZ; }; }; //====================== struct struct_Keyence_ep_buff { int _ep; BYTE _save_send_cmd; BYTE _save_send_cmd0; BYTE _save_send_cmd1; char *_buffer; // MAX_BUFF_SIZE int _size; void *_async_context; BOOL _hProtoPending; HANDLE _event; }; //====================================================================================== class CKeyence_Proto { public: // EP 01/81 channel threads. static int g_hEP81_Thread_State; static unsigned __stdcall g_EP81_Thread(LPVOID pThis); static HANDLE g_hEP81_Thread_Id; // EP 02/82 channel threads. static int g_hEP82_Thread_State; static unsigned __stdcall g_EP82_Thread(LPVOID pThis); static HANDLE g_hEP82_Thread_Id; // // g_hEP01_Thread_Event is used to kick a command // static int g_hEP01_Thread_State; static unsigned __stdcall g_EP01_Thread(LPVOID pThis); static HANDLE g_hEP01_Thread_Id; static HANDLE g_hEP01_Serial_Mutex; // EP01 // static int g_hEP02_Thread_State; static unsigned __stdcall g_EP02_Thread(LPVOID pThis); static HANDLE g_hEP02_Thread_Id; static HANDLE g_hEP02_Serial_Mutex; // EP02 static HANDLE g_hHomedEvent; // CKeyence_Proto(); virtual ~CKeyence_Proto(); // Global Structures static struct_Keyence_ep_buff ep_buff[lEPSIZE]; static struct_Keyence_machine g_machine; static CLogger* g_pLogger; static usb_dev_handle *g_dev; SSI_STATUS_MOTION Init_MvUsb(); SSI_STATUS_MOTION Exit_MvUsb(); static void Trace_EP_Buff(long lIndex); static void _process_rcv_transfer_data(int iEP); usb_dev_handle* _open_usb_dev(void); SSI_STATUS_MOTION _usb_reset(void); SSI_STATUS_MOTION _start_machine(); SSI_STATUS_MOTION _shutdown_machine(); SSI_STATUS_MOTION _do_single_threaded_usb_comm(int iEP); SSI_STATUS_MOTION _submit_async_8x(int iEP_Base); SSI_STATUS_MOTION _reap_async_8x(int iEP_Base); SSI_STATUS_MOTION _send_usb_data(int iEP_Base); SSI_STATUS_MOTION _send_cmd_KEYENCE_CMD_GET_LASER_DATA(); static SSI_STATUS_MOTION _process_KEYENCE_CMD_GET_LASER_DATA(); }; #endif