void HID_PnP::PollUSB() { buf[0] = 0x00; memset((void*)&buf[2], 0x00, sizeof(buf) - 2); if (isConnected == false) { device = hid_open(0x04d8, 0x003f, NULL); if (device) { isConnected = true; hid_set_nonblocking(device, true); timer->start(15); } } else { if(toggleLeds == true) { toggleLeds = false; buf[1] = 0x80; if (hid_write(device, buf, sizeof(buf)) == -1) { CloseDevice(); return; } buf[0] = 0x00; buf[1] = 0x37; memset((void*)&buf[2], 0x00, sizeof(buf) - 2); } if (hid_write(device, buf, sizeof(buf)) == -1) { CloseDevice(); return; } if(hid_read(device, buf, sizeof(buf)) == -1) { CloseDevice(); return; } if(buf[0] == 0x37) { potentiometerValue = (buf[2]<<8) + buf[1]; buf[1] = 0x81; } else if(buf[0] == 0x81) { pushbuttonStatus = (buf[1] == 0x00); buf[1] = 0x37; } } hid_comm_update(isConnected, pushbuttonStatus, potentiometerValue); }
void send(const unsigned char* buffer, std::size_t bufferSize) { const int size = hid_write(handle_, buffer, bufferSize); if (size < 0) { if (size == -1) { const int errorCode = hid_last_error_code(handle_); if (errorCode == ERROR_DEVICE_NOT_CONNECTED) { throw IOException("Interface not connected"); } else { throw IOException(Poco::format("USB driver: Send Error (WriteFile error %d)", errorCode)); } } else if (size == -2) { throw IOException("USB driver: Send Error (WaitForSingleObject Timeout)"); } else if (size == -3) { const int errorCode = hid_last_error_code(handle_); throw IOException(Poco::format("USB driver: Send Error (GetOverlappedResult error %d)", errorCode)); } throw IOException("USB driver: Send Error"); } }
// Send a command to the portal void PortalIO::Write(RWBlock *pb) throw (int) { #if DEBUG printf(">>> PortalIO::Write\n"); #endif pb->buf[0] = 0; // Use report 0 /* SkylanderIO *skio; skio = new SkylanderIO(); printf(">>>\n"); skio->fprinthex(stdout,pb->buf, 0x21); delete skio; */ if (hid_write(hPortalHandle, pb->buf, 0x21) == -1) { #if DEBUG printf("<<< PortalIO::Write throw 6\n"); #endif throw 6; } #if DEBUG printf("<<< PortalIO::Write\n"); #endif }
int write(hid_device *device, unsigned char const *data, size_t length) { return hid_executor->await([=] { return hid_write(device, data, length); }); }
int CorsairTemp::SetTempLimit(int limit) {//2 memset(cl->buf,0x00,sizeof(cl->buf)); // Read fan Mode cl->buf[0] = 0x05; // Length cl->buf[1] = cl->CommandId++; // Command ID cl->buf[2] = WriteTwoBytes; // Command Opcode cl->buf[3] = TEMP_Limit; // Command data... cl->buf[4] = limit & 0x00FF; cl->buf[5] = limit >> 8; int res = hid_write(cl->handle, cl->buf, 11); if (res < 0) { fprintf(stderr, "Error: Unable to write() %s\n", (char*)hid_error(cl->handle) ); return -1; } res = cl->hid_read_wrapper(cl->handle, cl->buf); if (res < 0) { fprintf(stderr, "Error: Unable to read() %s\n", (char*)hid_error(cl->handle) ); return -1; } return 0; }
int CorsairTemp::GetTempLimit() {//2 memset(cl->buf,0x00,sizeof(cl->buf)); // Read fan Mode cl->buf[0] = 0x03; // Length cl->buf[1] = cl->CommandId++; // Command ID cl->buf[2] = ReadTwoBytes; // Command Opcode cl->buf[3] = TEMP_Limit; // Command data... int res = hid_write(cl->handle, cl->buf, 11); if (res < 0) { fprintf(stderr, "Error: Unable to write() %s\n", (char*)hid_error(cl->handle) ); //return -1; } res = cl->hid_read_wrapper(cl->handle, cl->buf); if (res < 0) { fprintf(stderr, "Error: Unable to read() %s\n", (char*)hid_error(cl->handle) ); //return -1; } int limit = cl->buf[5]<<8; limit += cl->buf[4]; return limit; }
int minicut::testmove() { //eg: usb data to move the first motor on 10 stepwhile 1 second //1 . send reset from pc: 0x42+0x00 //1a. IPL5X answer is 0x42+0xFF //2 .source=USB (from PC): 0x42+0x01 //2a. IPL5X answer is 0x42+0xFF //3 .Fill buffer //page7 unsigned char data[]={0x42,0x00}; //Return the size of written data unsigned char data1[]={0x42,0x01}; //Return the size of written data unsigned char data2[]={0x42,0x01}; //... unsigned char reset=0x42; hid_write(minicut_parrot_device, data, sizeof(data)); //hid_write(virtmnc, &data, sizeof(data)); // hid_write(virtmnc, &data, sizeof(data)); return 0; }
int USBInterface::SendBuffer() { BufferOUT[64]=Mode; if(HIDisOpen) return hid_write(DeviceHandle, BufferOUT, 65); else return -1; }
/********************************************************** * Function set_relay_hidapi() * * Description: Set new relay state * * Parameters: portname (in) - communication port * relay (in) - relay number * relay_state (in) - current relay state * serial (in) - serial number [not used] * * Return: o - success * -1 - fail *********************************************************/ int set_relay_hidapi(char* portname, uint8_t relay, relay_state_t relay_state, char* serial) { hid_device *hid_dev; unsigned char buf[REPORT_LEN]; if (relay<FIRST_RELAY || relay>(FIRST_RELAY+g_num_relays-1)) { fprintf(stderr, "ERROR: Relay number out of range\n"); return -1; } /* Open HID API device */ if ((hid_dev = hid_open_path(portname)) == NULL) { fprintf(stderr, "unable to open HID API device %s\n", portname); return -2; } /* Write relay state by sending an output report to the device */ memset(buf, 0, sizeof(buf)); buf[REPORT_WRCMD_OFFSET] = (relay_state==ON) ? CMD_ON : CMD_OFF; buf[REPORT_WRREL_OFFSET] = relay; //printf("DBG: Write relay data %02X %02X\n", buf[REPORT_WRCMD_OFFSET], buf[REPORT_WRREL_OFFSET]); if (hid_write(hid_dev, buf, sizeof(buf)) < 0) { fprintf(stderr, "unable to write output report to device %s (%ls)\n", portname, hid_error(hid_dev)); return -3; } hid_close(hid_dev); return 0; }
void HIDDMXDevice::updateMode() { /** * Send chosen mode to the HID DMX device */ unsigned char driver_mode = 0; if (m_mode & DMX_MODE_OUTPUT) driver_mode += 2; if (m_mode & DMX_MODE_INPUT) driver_mode += 4; unsigned char buffer[34]; memset(buffer, 0, 34); buffer[1] = 16; buffer[2] = driver_mode; hid_write(m_handle, buffer, 34); /** * Start / stop input polling thread based on whether the input is activated */ if (m_mode & DMX_MODE_INPUT) { m_running = true; start(); } else if (isRunning() == true) { m_running = false; wait(); } }
void HID_write(unsigned char *buff, int size) { int ret = 0; ret = hid_write(target, buff, size); DBG_INFO("HID_write ret = %d", ret); }
uint16_t get_pot1_val() { static unsigned char CMD = 0x37; int response; // Request state (cmd 0x37). The first byte is the report number (0x0). buf[0] = 0x0; buf[1] = CMD; response = hid_write(handle, buf, 64); if(response < 0) { printf("Error sending command %x", CMD); return -1; } // Read in the requested state response = hid_read(handle, buf, 64); if(response < 0) { printf("Error reading response for command %x", CMD); return -1; } return merge_uint8(buf[1], buf[2]); // Print out the returned buffer. // for (i = 0; i < 3; i++) // printf("buf[%d]: %d\n", i, buf[i]); }
bool get_push1_state() { static unsigned char CMD = 0x81; int response; // Request state (cmd 0x81). The first byte is the report number (0x0). buf[0] = 0x0; buf[1] = CMD; response = hid_write(handle, buf, 64); if(response < 0) { printf("Error sending command %x", CMD); return false; } // Read requested state response = hid_read(handle, buf, 64); if(response < 0) { printf("Error reading response for command %x", CMD); return false; } // Print out the returned buffer. // for (i = 0; i < 2; i++) // printf("buf[%d]: %d\n", i, buf[i]); return !buf[1]; }
void usb_write(unsigned char data[8]) { int r = hid_write(dev, data, 8); if (r < 0) { fatal("error writing data (%ls)", hid_error(dev)); } usleep(30 * 1000); }
//----------------------------------------------------------------------------- int dbg_dap_cmd(uint8_t *data, int size, int rsize) { char cmd = data[0]; int res; memset(hid_buffer, 0xff, report_size + 1); hid_buffer[0] = 0x00; // Report ID memcpy(&hid_buffer[1], data, rsize); res = hid_write(handle, hid_buffer, report_size + 1); if (res < 0) { printf("Error: %ls\n", hid_error(handle)); perror_exit("debugger write()"); } res = hid_read(handle, hid_buffer, report_size + 1); if (res < 0) perror_exit("debugger read()"); check(res, "empty response received"); check(hid_buffer[0] == cmd, "invalid response received"); res--; memcpy(data, &hid_buffer[1], (size < res) ? size : res); return res; }
Comm::ErrorCode Comm::SendPacket(unsigned char *pData, int size) { QTime timeoutTimer; int res = 0, timeout = 5; timeoutTimer.start(); while(res < 1) { res = hid_write(boot_device, pData, size); if(timeoutTimer.elapsed() > SyncWaitTime) { timeoutTimer.start(); timeout--; } // If timed out several times, or return error then close device and return failure if(timeout == 0) { qWarning("Timed out waiting for query command acknowledgement."); return Timeout; } if(res == -1) { qWarning("Write failed."); close(); return Fail; } } return Success; }
int boca_hid_write(boca_hid_printer_t *self, char *data, size_t byte_to_write) { if (!self->device) return 0; int byte_written = 0; while ((*data) != '\0') { char *buf; size_t byte_to_copy = self->output_length; byte_to_copy--; buf = calloc(self->output_length, sizeof(char)); byte_to_copy = byte_to_copy > strlen(data) ? strlen(data) : byte_to_copy; if (byte_to_copy == 0)break; memcpy(buf + 1, data, byte_to_copy); int ret = hid_write(self->device, (const unsigned char *) buf, self->output_length); if (ret == -1) { zsys_warning("hid printer: can not write code: %d msg: %ls", GetLastError(), hid_error(self->device)); free(buf); break; } data += byte_to_copy; byte_written += ret; free(buf); } return byte_written; }
static inline int em_osp_request_startstop(energymon_osp* em) { memset((void*) &em->buf, 0x00, sizeof(em->buf)); em->buf[1] = OSP_REQUEST_STARTSTOP; if (hid_write(em->device, em->buf, sizeof(em->buf)) == -1) { return -1; } return 0; }
bool CDevice::DiskWrite(uint8_t *buf, int size) { if (size != DISK_WRITEMAX) //always max! return false; hidbuf[0] = ID_DISK_WRITE; memcpy(hidbuf + 1, buf, size); return hid_write(handle, hidbuf, DISK_WRITEMAX + 1) >= 0; // WRITEMAX+reportID }
int USB_Write() { if(DeviceHandle == NULL) return -1; return hid_write(DeviceHandle, OutputBuffer, USB_MIN_PACKET_SIZE+1); }
int send_ontrak_command(hid_device *handle){ unsigned char buf[] = { 0x01, 'P', 'I', 0, 0, 0, 0, 0 }; int res = hid_write(handle, buf, 8); if(res < 0) { printf("Unable to write()!\n"); } return res; }
int main(int argc, char* argv[]) { int res; unsigned char buf[RELAY_COMMAND_SIZE]; hid_device *handle; unsigned char command; if (argc != 2) { fprintf(stderr, "usage: %s [on|off]\n", argv[0]); exit(1); } if (strcmp(argv[1], "on") == 0) { command = RELAY_STATE_ON; } else if (strcmp(argv[1], "off") == 0) { command = RELAY_STAT_OFF; } else if (strcmp(argv[1], "scan") == 0) { return doScan(); } else { fprintf(stderr, "usage: %s [on|off]\n", argv[0]); exit(1); } if (hid_init()) { fprintf(stderr, "can't init hid lib\n"); exit(1); } // Set up the command buffer. memset(buf,0x00,sizeof(buf)); buf[1] = command; buf[2] = RELAY_NUMBER; // Open the device using the VID, PID, // and optionally the Serial number. ////handle = hid_open(0x4d8, 0x3f, L"12345"); handle = hid_open(RELAY_VENDOR_ID, RELAY_PRODUCT_ID, NULL); if (!handle) { printf("unable to open device\n"); return 1; } res = hid_write(handle, buf, sizeof(buf)); if (res < 0) { printf("Unable to send command\n"); } hid_close(handle); /* Free static HIDAPI objects. */ hid_exit(); exit(0); }
int send_usb_packet(dldev_t *dev, u8 *packet, u16 len) { if(hid_write(dev->usb.handle, packet, len) == -1){ ERROR("could not send packet"); return -1; } return 0; }
bool DeviceHandleHIDAPI::write(const Transfer& transfer_, uint8_t) { if (transfer_) { int nBytesWritten = hid_write(m_pCurrentDevice, transfer_.data().data(), transfer_.size()); return (nBytesWritten >= static_cast<int>(transfer_.size())); } return false; }
void vrpn_HidInterface::send_data(size_t bytes, const vrpn_uint8 *buffer) { if (!_working) { fprintf(stderr,"vrpn_HidInterface::send_data(): Interface not currently working\n"); return; } int ret; if ( (ret = hid_write(_device, const_cast<vrpn_uint8 *>(buffer), bytes)) != bytes) { fprintf(stderr,"vrpn_HidInterface::send_data(): hid_interrupt_write() failed with code %d\n", ret); } }
int WiimoteHidapi::IOWrite(const u8* buf, size_t len) { DEBUG_ASSERT(buf[0] == (WR_SET_REPORT | BT_OUTPUT)); int result = hid_write(m_handle, buf + 1, len - 1); if (result == -1) { ERROR_LOG(WIIMOTE, "Failed to write to %s.", m_device_path.c_str()); return 0; } return (result == 0) ? 1 : result; }
/************************************************************************//** * \brief Services USB communciation with the Manta * * HandleEvents should be called periodically to poll all connected Mantas for * incoming USB frames as well as to send any messages that have been queued * up with WriteFrame(). It should be called at least once every 6ms, but you * may get improved results polling as fast as every 1ms if your application * supports it. * * Note: Because WriteFrame() accesses the same message queue that HandleEvents() * does, they should be protected from each other by a mutex on the application * level if they're being called from parallel threads. ****************************************************************************/ void MantaUSB::HandleEvents(void) { list<MantaUSB *>::iterator i = mantaList.begin(); /* read from each manta and trigger any events */ while(mantaList.end() != i) { MantaUSB *current = *i; if(current->IsConnected()) { int bytesRead; int8_t inFrame[InPacketLen]; bytesRead = hid_read(current->DeviceHandle, reinterpret_cast<uint8_t *>(inFrame), InPacketLen); if(bytesRead < 0) { current->DebugPrint("%s-%d: Read error on Manta %d", __FILE__, __LINE__, current->GetSerialNumber()); throw(MantaCommunicationException(current)); } else if(bytesRead) { current->FrameReceived(inFrame); } } ++i; } /* pop one item off the transmit queue and send down to its target */ if(! txQueue.empty()) { int bytesWritten; MantaTxQueueEntry *txMessage = txQueue.front(); txQueue.pop_front(); bytesWritten = hid_write(txMessage->TargetManta->DeviceHandle, txMessage->OutFrame, OutPacketLen + 1); txMessage->TargetManta->DebugPrint("%s-%d: Frame Written to Manta %d", __FILE__, __LINE__, txMessage->TargetManta->GetSerialNumber()); for(int i = 0; i < 16; i += 8) { uint8_t *frame = txMessage->OutFrame + 1; txMessage->TargetManta->DebugPrint("\t\t%x %x %x %x %x %x %x %x", frame[i], frame[i+1], frame[i+2], frame[i+3], frame[i+4], frame[i+5], frame[i+6], frame[i+7]); } delete txMessage; if(bytesWritten < 0) { txMessage->TargetManta->DebugPrint("%s-%d: Write error on Manta %d", __FILE__, __LINE__, txMessage->TargetManta->GetSerialNumber()); throw(MantaCommunicationException(txMessage->TargetManta)); } } }
bool vmarker::writereg(uint8_t reg, uint16_t *data){ if(this->connected){ unsigned char buf[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; buf[1] = 0x05; buf[2] = reg; buf[3] = *data >> 8; buf[4] = *data; if(hid_write(this->vmarkerdev,buf,9)<0) return false; if(hid_read_timeout(this->vmarkerdev,buf,9,200)<0) return false; return true; }else{
int cb_panel_write(unsigned char buf[]) { int res = 0; if (cbHandle) { res = hid_write(cbHandle, buf, CB_OUT_BUF_SIZE); if (res < 0) { sprintf(tmp, "-> CP: cb_driver.panel_write: Error: %ls\n", hid_error(cbHandle)); printf(tmp); } } return res; }
void deviceSend(struct config_report Data) { /* Метод отправки данных HID-устройству */ if (hid_write(handle_device, (const unsigned char*)&Data, sizeof(Data))==-1) { // Установка отметки "Error" Form1->Button3->ImageIndex = 3; // Вывод статуса Form1->Label2->Text = "Ошибка отправки данных!"; } }