#include "stdafx.h" #include "..\Tools\UsbUtility\Proto_Util.h" #include "KeyenceTM065_Proto.h" #include "KeyenceStatus.h" #define MY_CONFIG 1 #define MAX_DEVPATH_LENGTH 256 #define ENDPOINT_TIMEOUT 500 //****************************************************************************** // void CKeyenceTM065_Proto::_process_rcv_transfer_data() { switch (ep_buff._cmd0) { case _CMD0_0800: break; case _CMD0_0C00: switch (ep_buff._cmd1) { case _CMD1_0730: break; default: break; }; case _CMD0_100a: break; case _CMD0_1400: break; case _CMD0_3000: break; case _CMD0_4800: break; case _CMD0_4c00: break; default: break; }; _process_KEYENCE_CMD_GET_DATA(); }; //****************************************************************************** CKeyenceTM065_Proto::CKeyenceTM065_Proto() { ep_buff._buff = (unsigned char *)malloc(MAX_KEYENCE_EP_BUFF_SIZE); ep_buff._bin = (unsigned char *)malloc(MAX_KEYENCE_EP_BUFF_SIZE); ep_buff._send_size = 0; ep_buff._async_context = NULL; ep_buff._cmd0 = 0; ep_buff._cmd1 = 0; g_dev = NULL; }; //****************************************************************************** CKeyenceTM065_Proto::~CKeyenceTM065_Proto() { free(ep_buff._buff); free(ep_buff._bin); } #pragma warning(disable:4996) //****************************************************************************** //****************************************************************************** usb_dev_handle* CKeyenceTM065_Proto::_open_usb_dev(void) { struct usb_bus *bus = NULL; struct usb_device *dev = NULL; for (bus = usb_get_busses(); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idVendor == KEYENCE_VID && dev->descriptor.idProduct == KEYENCE_PID) { return usb_open(dev); } } } return NULL; } //****************************************************************************** int CKeyenceTM065_Proto::_usb_reset(void) { if (g_dev) { usb_reset(g_dev); g_dev = NULL; } else { ASSERT(0); return -1; } return -1; } //****************************************************************************** int CKeyenceTM065_Proto::Init_Keyence() { int usb_status = NULL; usb_init(); // initialize the library usb_status = usb_find_busses(); // find all busses usb_status = usb_find_devices(); // find all connected devices g_dev = _open_usb_dev(); if (!g_dev) { MessageBox(NULL, _T("Unable to open device"), _T("Message"), MB_OK); return KEYENCE_STATUS_DATALINK_ERROR; } if (usb_set_configuration(g_dev, MY_CONFIG) < 0) { MessageBox(NULL, _T("Unable to SET CONFIGURATION"), _T("Message"), MB_OK); return KEYENCE_STATUS_DATALINK_ERROR; } if (usb_claim_interface(g_dev, 0) < 0) { usb_close(g_dev); MessageBox(NULL, _T("Unable to CLAIM DEVICE"), _T("Message"), MB_OK); return KEYENCE_STATUS_DATALINK_ERROR; } return KEYENCE_STATUS_NORMAL; } //****************************************************************************** int CKeyenceTM065_Proto::Exit_Keyence() { if (g_dev) { usb_release_interface(g_dev,0); usb_close(g_dev); g_dev = NULL; } return KEYENCE_STATUS_NORMAL; } //****************************************************************************** // This startup just kicks off the EP_KEYENCE_81 worker thread. //****************************************************************************** int CKeyenceTM065_Proto::_start_machine() { return KEYENCE_STATUS_NORMAL; }; //=============================================================================== int CKeyenceTM065_Proto::_shutdown_machine() { return KEYENCE_STATUS_NORMAL; }; //=============================================================================== // Keyence Protocol - This can be processed serially. // Send data on EP_01 channel. // Receive data on EP_01. // Send data on EP_82 channel. // Receive data on EP_82 channel. // if more data needs to be received, send data to EP_82 channel. // (Note, we do not receive Image Data, so we will not receive 0x40000 bytes. // But we must make provision for receiving and sending 0x40000 bytes. (512x512). // // The following two commands is all we need: // // usb_bulk_write(g_dev, iEP, (char *)(ep_buff._buff+33), ep_buff._send_size, 5000); // iRcvSize = usb_bulk_read(g_dev, iEP, (char *)ep_buff._buff, MAX_KEYENCE_EP_BUFF_SIZE, 5000); // //=============================================================================== int CKeyenceTM065_Proto::_send_usb_data() { int _ret; _ret = usb_bulk_setup_async(g_dev, &ep_buff._async_context, ep_buff._iEP); if (_ret < 0) { return KEYENCE_STATUS_DATALINK_ERROR; } _ret = usb_submit_async(ep_buff._async_context, (char *)ep_buff._bin, ep_buff._send_size); if (_ret < 0) { usb_free_async(&ep_buff._async_context); return KEYENCE_STATUS_DATALINK_ERROR; } _ret = usb_reap_async(ep_buff._async_context, 10000); if (_ret > 0) { ep_buff._recv_size = _ret; _process_rcv_transfer_data(); usb_free_async(&ep_buff._async_context); } return KEYENCE_STATUS_NORMAL; } //============================================================== int CKeyenceTM065_Proto::_send_cmd_KEYENCE_CMD_GET_DATA() { return KEYENCE_STATUS_NORMAL; }; //============================================================== int CKeyenceTM065_Proto::_process_KEYENCE_CMD_GET_DATA() { return KEYENCE_STATUS_NORMAL; }; //============================================================== int CKeyenceTM065_Proto::_get_image() { return KEYENCE_STATUS_NORMAL; }; //============================================================== int CKeyenceTM065_Proto::_put_program() { return KEYENCE_STATUS_NORMAL; }; //============================================================== int CKeyenceTM065_Proto::_init_program() { return KEYENCE_STATUS_NORMAL; }; //============================================================== int CKeyenceTM065_Proto::_get_program() { return KEYENCE_STATUS_NORMAL; }; //============================================================== int CKeyenceTM065_Proto::_read_env() { return KEYENCE_STATUS_NORMAL; }; //============================================================== int CKeyenceTM065_Proto::_write_env() { return KEYENCE_STATUS_NORMAL; }; //============================================================== int CKeyenceTM065_Proto::_set_config() // sets trigger etc. { return KEYENCE_STATUS_NORMAL; }; //============================================================== // //============================================================== int CKeyenceTM065_Proto::_replay_capture(CString s_replay_file) { //struct_ep ep_buff; //ep_buff._buff = (unsigned char *)malloc(MAX_KEYENCE_EP_BUFF_SIZE); //ep_buff._bin = (unsigned char *)malloc(MAX_KEYENCE_EP_BUFF_SIZE); char cTemp[9]; FILE* pInFile; _wfopen_s(&pInFile, s_replay_file, _T("r")); if (pInFile == NULL) { return KEYENCE_STATUS_REPLAY_FILE_ERROR; }; fgets((char *)ep_buff._buff, MAX_KEYENCE_EP_BUFF_SIZE, pInFile ); // pick up the first line TRACE("\nReading1 %s", ep_buff._buff); CProto_Util* _protoUtil = new CProto_Util(); while (!feof(pInFile)) { memset(cTemp, 0 , 9); memcpy(cTemp, ep_buff._buff+24, 8); sscanf_s(cTemp, "%8x", &ep_buff._send_size); // get the length of the transmission ep_buff._recv_size = ep_buff._send_size; memcpy(cTemp, ep_buff._buff+33, 8); sscanf_s(cTemp, "%8x", &ep_buff._cmd0); ep_buff._iEP = (*(ep_buff._buff+21) == '1') ? 0x01:0x82; if (*ep_buff._buff == '>') { if (ep_buff._iEP == 0x82) memset(ep_buff._bin, ep_buff._send_size, 0); else { _protoUtil->_char2bin(ep_buff._buff+33, ep_buff._bin, ep_buff._send_size); if (ep_buff._send_size == 0x200) // Temporary patch to see how Keyence behaves. { memset(ep_buff._bin+12, 0, 0x200); } }; _send_usb_data(); fgets((char *)ep_buff._buff, MAX_KEYENCE_EP_BUFF_SIZE, pInFile ); // skip the input line } fgets((char *)ep_buff._buff, MAX_KEYENCE_EP_BUFF_SIZE, pInFile ); // pick up the first line Sleep(50); }; delete _protoUtil; fclose(pInFile); //free(ep_buff._buff); //free(ep_buff._bin); return KEYENCE_STATUS_NORMAL; } //============================================================== // //============================================================== int CKeyenceTM065_Proto::_read_prs_file(CString cFileName) { UNREFERENCED_PARAMETER(cFileName); return 0; };