public void updateState(bool newState) { int res = -1; if (deviceHandle > 0) { TimeSpan ts = DateTime.Now - previousTime; previousTime = DateTime.Now; if (ts.Seconds >= minTime) { if (newState) { deviceHandle = USBRelayDriver.usb_relay_device_open_with_serial_number(serial, serial.Length); res = USBRelayDriver.usb_relay_device_open_all_relay_channel(deviceHandle); } else { res = USBRelayDriver.usb_relay_device_close_all_relay_channel(deviceHandle); USBRelayDriver.usb_relay_device_close(deviceHandle); } } } if (res != 0 || deviceHandle < 0) { //revert back to the original state if the device was not able to respond ignoreCheckEvent = true; chkOpen.Checked = !newState; ignoreCheckEvent = false; } }
/** * Returns 0 if it is okay to close the application. 1 otherwise */ public int closeUSBRelayControl() { if (deviceHandle > 0) { int res = USBRelayDriver.usb_relay_device_close_all_relay_channel(deviceHandle); USBRelayDriver.usb_relay_device_close(deviceHandle); } deviceHandle = -1; return(0); }