// DelcomTest - A simple linux example to read and write the Delcom USB Device. // Delcom Products Inc - 45 Backus Ave Danbury CT 06810 // Works with all Delcom HID device (Generations 2,3+). // Version 1.0 2015/04/08 - Tested on Ubuntu V14 // 2018/01/22 - Added apt-get notes // 2018/10/11 - !!! See Linux Kernel Issue !!! -> https://www.delcomproducts.com/webnote.asp?id=3 // 2019/03/03 - Added Add/Remove software notes // For Delcom Device commands see http://www.delcomproducts.com/downloads/USBIOHID.pdf // Compile String: gcc -g -O0 -o DelcomTest DelcomTest.c -lhidapi-libusb // To run: './Delcomtest' (or 'sudo ./DelcomTest' if you need other rights) // Requires the hidapi library. Methods to install the library are below. // 1) Install using Add/remove programs // Click GUI->Preferences->Add / Remove Software // Search for 'hidapi' // Select 'libhidapi-dev', 'libhidapi-hidraw' & 'libhidapi-libusb' // optionally select Debugging Sysbols and Python libs if you require them. // Click Apply // 2) Install using apt-get // sudo apt-get install libusb-1.0-0-dev // sudo apt-get install libudev-dev // 3) Install from website // http://packages.ubuntu.com/source/trusty/hidapi #include #include #include #include #include //#include typedef union HIDPacketStruct { unsigned char Data[256]; struct { unsigned char MajorCmd; unsigned char MinorCmd; unsigned char DataLSB; unsigned char DataMSB; unsigned char DataHID[4]; unsigned char DataExt[8]; } Tx; struct { unsigned char Cmd; } Rx; } HIDPacketStruct, *pHIDPacketStruct; int main(int argc, char* argv[]) { char Port0, Port1; int res, count, cmd; hid_device *handle; int report_id; HIDPacketStruct MyPacket; printf("\n"); cmd = -1; if (argc > 1) { // Check for command line arguments if(isdigit(argv[1][0]) ) cmd = (int) strtol(argv[1], NULL, 0); else { printf("Delcom USB Linux Example. Version 1.0.\n"); printf("Syntax: tryme [cmd]\n"); printf("With no arguments, just reads the ports.\n"); printf("With numeric argument, XORs the value with port1 and write it to port1.\n"); printf("For example 'tryme 1' will toggle bit 0 on port 1.\n"); return 1; } } // Open the device using the VID, PID, handle = hid_open(0xfc5, 0xB080, NULL); // VID:0x0FC5, PID:0XB080(Standard), 0xA080(Alternate) if (!handle) { printf("Error: Unable to open device.\n"); return 1; } // device found, read device info and display printf("Delcom Device found. "); MyPacket.Rx.Cmd = 10; // Read Version (Command #10) res = hid_get_feature_report(handle, MyPacket.Data, 8); if (res < 0) { printf("Error: Failed to read device.\n"); printf("%ls", hid_error(handle)); return 1; } else printf("Firmware Version: %d\n", MyPacket.Data[4]); // Read the ports (Command #100). MyPacket.Rx.Cmd = 100; res = hid_get_feature_report(handle, MyPacket.Data, 8); if (res < 0) { printf("Error: Failed to read device.\n"); printf("%ls", hid_error(handle)); return 1; } else { // Get and Display the current pin values Port0 = MyPacket.Data[0]; Port1 = MyPacket.Data[1]; printf("Port0: 0x%02hhx Port1: 0x%02hhx\n", Port0, Port1); } // Now do the write port1 command (if cmd!=-1) // Note if your having problem writing to the port see Delcom note 3 // https://www.delcomproducts.com/webnote.asp?id=3 if(cmd!=-1){ if(1){ // Set PWM value MyPacket.Tx.MajorCmd = 101; // Write 8 byte command MyPacket.Tx.MinorCmd = 34; // Set PWM Command MyPacket.Tx.DataLSB = 0; // PWM(Green) MyPacket.Tx.DataMSB = 100; // 100 hid_send_feature_report(handle, MyPacket.Data, 8); // Send it MyPacket.Tx.MajorCmd = 101; // Write 8 byte command MyPacket.Tx.MinorCmd = 34; // Set PWM Command MyPacket.Tx.DataLSB = 1; // PWM(Red) MyPacket.Tx.DataMSB = 100; // 100 hid_send_feature_report(handle, MyPacket.Data, 8); // Send it MyPacket.Tx.MajorCmd = 101; // Write 8 byte command MyPacket.Tx.MinorCmd = 34; // Set PWM Command MyPacket.Tx.DataLSB = 2; // PWM(Yellow/Blue) MyPacket.Tx.DataMSB = 100; // 100 hid_send_feature_report(handle, MyPacket.Data, 8); // Send it } Port1 ^= (char)cmd; // XOR the port1 value printf("-Writing value 0x%02hhx to port1.\n",Port1); MyPacket.Tx.MajorCmd = 101; // Write 8 byte command MyPacket.Tx.MinorCmd = 2; // Write to port 1 command MyPacket.Tx.DataLSB = Port1; // Data to write to Port1 hid_send_feature_report(handle, MyPacket.Data, 8); // Send it } if(0) { printf("Press Any Key to Continue\n"); getchar(); } printf("All done, closing device and HIDAPI objects!\n\n"); hid_close(handle); hid_exit(); return 0; }