void write_configuration_block_USB500( libusb_device_handle *udev, configurationBlock *cblock ) { uint8_t send_configuration[3] = {0x01, 0xff, 0xff}; // Send Configuration String uint8_t acknowledge = 0x0; // Acknowledge unsigned int size = 0; int ret; int transferred; /* Determine the size of the Configuration Block */ switch(cblock->type) { case 0x1: // EL-USB-1 Temperature Logger Packet size 64 bytes case 0x2: // EL-USB-1 Temperature Logger Packet size 64 bytes send_configuration[1] = 0x40; // Low Byte send_configuration[2] = 0x00; // High Byte size = 64; break; case 0x3: // EL-USB-2 Temperature/Humidity Logger Packet size 128 bytes send_configuration[1] = 0x80; // Low Byte send_configuration[2] = 0x00; // High Byte size = 128; break; case 0x4: // EL-USB-3 Voltage Logger 256 bytes case 0x6: // EL-USB-3 Voltage Logger 256 bytes case 0x5: // EL-USB-4 Current Logger 256 bytes case 0x7: // EL-USB-4 Current Logger 256 bytes send_configuration[1] = 0x00; // Low Byte send_configuration[2] = 0x01; // High Byte size = 256; break; default: printf("Unknown type device = %d\n", cblock->type); return; break; } unlock_device_USB500(udev); ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_OUT | 2, (unsigned char *) send_configuration, 3, &transferred, USB500_WAIT_WRITE); if (ret < 0) { perror("Error in sending configuration acknowledgement (Bulk Write)"); } ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_OUT | 2, (unsigned char *) cblock, size, &transferred, USB500_WAIT_WRITE); if (ret < 0) { perror("Error in sending configuration block (Bulk Write)"); } acknowledge = 0x0; do { ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN|2, (unsigned char *) &acknowledge, 1, &transferred, USB500_WAIT_READ); } while (acknowledge != 0xff); lock_device_USB500(udev); }
int read_configuration_block_USB500( libusb_device_handle *udev, configurationBlock *cblock ) { unsigned char request_configuration[3] = {0x00, 0xff, 0xff}; // Request Configuration String uint8_t acknowledge[3] = {0x00, 0x00, 0x00}; // Acknowledge string unsigned int size = 0; int ret; int try = 0; int transferred; unlock_device_USB500(udev); ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_OUT | 2, request_configuration, 3, &transferred, USB500_WAIT_WRITE); if (ret < 0) { perror("Error in requesting configuration (Bulk Write)"); lock_device_USB500(udev); return 0; } acknowledge[0] = 0x0; do { ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN|2, (unsigned char *) acknowledge, 3, &transferred, USB500_WAIT_READ); if (ret < 0) { perror("Error in acknowledging configuration from USB 500"); printf("Bytes transferred = %d\n", transferred); lock_device_USB500(udev); return 0; } if (transferred == 3 && acknowledge[0] != 0x2) { printf("read_configuration_block_USB500: try = %d first byte = %x\n", try, acknowledge[0]); libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_OUT | 2, (unsigned char *) request_configuration, 3, &transferred, USB500_WAIT_WRITE); try++; } if (try > 10) { printf("read_configuration_block_USB500: try = %d Stopping now \n", try); lock_device_USB500(udev); return 0; exit(-1); } } while (acknowledge[0] != 0x2); size = ((acknowledge[2] << 8) | acknowledge[1]); do { ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN | 2, (unsigned char *) cblock, size, &transferred, USB500_WAIT_READ); if (ret < 0) { perror("Error in reading configuration block from USB 500"); printf("Size = %d transferred = %d\n", size, transferred); } } while (transferred != size); lock_device_USB500(udev); return cblock->type; }
int read_configuration_block_USB500( usb_dev_handle *udev, configurationBlock *cblock ) { char request_configuration[3] = {0x00, 0xff, 0xff}; // Request Configuration String __u8 acknowledge[3] = {0x00, 0x00, 0x00}; // Acknowledge string unsigned int size = 0; int ret; int try = 0; unlock_device_USB500(udev); ret = usb_bulk_write(udev, USB_ENDPOINT_OUT | 2, request_configuration, 3, USB500_WAIT_WRITE); if (ret < 0) { perror("Error in requesting configuration (Bulk Write)"); } acknowledge[0] = 0x0; do { ret = usb_bulk_read(udev, USB_ENDPOINT_IN|2, (char *) acknowledge, 3, USB500_WAIT_READ); if ( ret < 0 ) { perror("Error in acknowledging configuration from USB 500"); } if (ret == 3 && acknowledge[0] != 0x2) { printf("read_configuration_block_USB500: try = %d first byte = %x\n", try, acknowledge[0]); usb_bulk_write(udev, USB_ENDPOINT_OUT | 2, request_configuration, 3, USB500_WAIT_WRITE); try++; } if (try > 10) { printf("read_configuration_block_USB500: try = %d Stopping now \n", try); exit(-1); } } while (acknowledge[0] != 0x2); size = ((acknowledge[2] << 8) | acknowledge[1]); do { ret = usb_bulk_read(udev, USB_ENDPOINT_IN | 2, (char *) cblock, size, USB500_WAIT_READ); if ( ret < 0 ) { perror("Error in reading configuration block from USB 500"); } } while (ret != size); lock_device_USB500(udev); return cblock->type; }
int read_recorded_data_USB504( usb_dev_handle *udev, configurationBlock *cblock, usb504_data *data_504 ) { __u8 request_data[3] = {0x03, 0xff, 0xff}; // Request Recorded Data __u8 acknowledge[3] = {0x00, 0x00, 0x00}; // Acknowledge string int packet_size = 512; int memory_size = 0xfe00; __u8 rdata[0xfe00]; // max data size int num_packets; unsigned int size = 0; int ret; int i; struct tm ltime; time_t currentTime; time(¤tTime); localtime_r(¤tTime, <ime); // set daylight savings field stop_logging_USB500(udev, cblock); // must stop logging before reading the data. unlock_device_USB500(udev); ret = usb_bulk_write(udev, USB_ENDPOINT_OUT | 2, (char *)request_data, 3, USB500_WAIT_WRITE); if (ret < 0) { perror("Error in requesting configuration (Bulk Write)"); } do { ret = usb_bulk_read(udev, USB_ENDPOINT_IN|2, (char *) acknowledge, 3, USB500_WAIT_READ); if ( ret < 0 ) { perror("Error in acknowledging read data from USB 500"); } } while (acknowledge[0] != 0x2); size = ((acknowledge[2] << 8) | acknowledge[1]); if (size != memory_size) { printf("Memory Error mismatch. size = %#x should be %#x\n", size, memory_size); return -1; } num_packets = memory_size / packet_size; for ( i = 0; i < num_packets; i++ ) { do { ret = usb_bulk_read(udev, USB_ENDPOINT_IN | 2, (char *) &rdata[i*packet_size], packet_size, USB500_WAIT_READ); if (ret < 0) { perror("Error reading data. Retrying."); } } while (ret != packet_size); } lock_device_USB500(udev); ltime.tm_sec = cblock->startTimeSeconds; ltime.tm_min = cblock->startTimeMinutes; ltime.tm_hour = cblock->startTimeHours; ltime.tm_mday = cblock->startTimeDay; ltime.tm_mon = cblock->startTimeMonth - 1; ltime.tm_year = cblock->startTimeYear + 100; data_504[0].time = mktime(<ime); // get local time stamp for (i = 0; i < cblock->sampleCount; i++) { data_504[i].time = data_504[0].time + i*cblock->sampleRate; data_504[i].current = cblock->calibrationMValue*rdata[i] + cblock->calibrationCValue; data_504[i].current *= cblock->capScalingFactor; } start_logging_USB500(udev, cblock); return cblock->sampleCount; }
int read_recorded_data_USB502( libusb_device_handle *udev, configurationBlock *cblock, usb502_data *data_502 ) { int packet_size = 512; int memory_size = 0x8000; int num_packets; unsigned int size = 0; int ret; int transferred; int i; struct tm ltime; time_t currentTime; uint8_t request_data[3] = {0x03, 0xff, 0xff}; // Request Recorded Data uint8_t acknowledge[3] = {0x00, 0x00, 0x00}; // Acknowledge string uint8_t rdata[0x8000]; // max data size time(¤tTime); localtime_r(¤tTime, <ime); // set daylight savings field stop_logging_USB500(udev, cblock); // must stop logging before reading the data. unlock_device_USB500(udev); ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_OUT | 2, (unsigned char *)request_data, 3, &transferred, USB500_WAIT_WRITE); if (ret < 0) { perror("Error in requesting configuration (Bulk Write)"); } do { ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN|2, (unsigned char *) acknowledge, 3, &transferred, USB500_WAIT_READ); if (ret < 0) { perror("Error in acknowledging read data from USB 500"); } } while (acknowledge[0] != 0x2); size = ((acknowledge[2] << 8) | acknowledge[1]); if (size != memory_size) { printf("Memory Error mismatch. size = %#x should be %#x\n", size, memory_size); return -1; } num_packets = memory_size / packet_size; for ( i = 0; i < num_packets; i++ ) { do { ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN | 2, (unsigned char *) &rdata[i*packet_size], packet_size, &transferred, USB500_WAIT_READ); if (ret < 0) { perror("Error reading data. Retrying."); } } while (transferred != packet_size); } lock_device_USB500(udev); ltime.tm_sec = cblock->startTimeSeconds; ltime.tm_min = cblock->startTimeMinutes; ltime.tm_hour = cblock->startTimeHours; ltime.tm_mday = cblock->startTimeDay; ltime.tm_mon = cblock->startTimeMonth - 1; ltime.tm_year = cblock->startTimeYear + 100; data_502[0].time = mktime(<ime); // get local time stamp for (i = 0; i < cblock->sampleCount; i++) { data_502[i].time = data_502[0].time + i*cblock->sampleRate; data_502[i].temperature = cblock->calibrationMValue*rdata[2*i] + cblock->calibrationCValue; // temperature first byte data_502[i].humidity = 0.5*rdata[2*i+1]; // humidity second byte } start_logging_USB500(udev, cblock); return cblock->sampleCount; }