private void USBResetButton_Click(object sender, EventArgs e) { usb_state = USB_DEFINES.USB_CNC_RESET; }
private void USBWyslijDane(USB_DEFINES usb_def) { // nastawa try { switch (NNastawaComboBox.SelectedItem.ToString()) { case "CURRENT_ON": stepMotorDataOut.current = CURRENT.CURRENT_ON; break; case "CURRENT_OFF": stepMotorDataOut.current = CURRENT.CURRENT_OFF; break; } switch (NStanComboBox.SelectedItem.ToString()) { case "STATE_MOVE": stepMotorDataOut.state = STATE_OF_MOVEMENT.STATE_MOVE; stepMotorDataOut.current = CURRENT.CURRENT_ON; break; case "STATE_STOP": stepMotorDataOut.state = STATE_OF_MOVEMENT.STATE_STOP; stepMotorDataOut.current = CURRENT.CURRENT_OFF; break; } stepMotorDataOut.feed_x = Convert.ToInt16(NFeedXTextBox.Text.ToString()); stepMotorDataOut.feed_y = Convert.ToInt16(NFeedYTextBox.Text.ToString()); stepMotorDataOut.new_x = Convert.ToInt16(NPozXTextBox.Text.ToString()); stepMotorDataOut.new_y = Convert.ToInt16(NPozYTextBox.Text.ToString()); if (usb_def != USB_DEFINES.USB_CNC_RESET_ACTUAL_POS) { stepMotorDataOut.x = stepMotorDataIn.x; stepMotorDataOut.y = stepMotorDataIn.y; } // wysyla dane z pc usb_state = usb_def; } catch { //MessageBox.Show("Nic się nie wysłało"); } }
private void USBReadWriteThread_DoWork(object sender, DoWorkEventArgs e) { //------------------------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------BEGIN CUT AND PASTE BLOCK----------------------------------------------------------------------------------- Byte[] OUTBuffer = new byte[65]; //Allocate a memory buffer equal to the OUT endpoint size + 1 Byte[] INBuffer = new byte[65]; //Allocate a memory buffer equal to the IN endpoint size + 1 uint BytesWritten = 0; uint BytesRead = 0; while (true) { try { if (AttachedState == true) //Do not try to use the read/write handles unless the USB device is attached and ready { // do communication switch (usb_state) { case USB_DEFINES.USB_CNC_NULL: break; case USB_DEFINES.USB_CNC_CONFIRM_DATA: //IntPtr pINBuffer = IntPtr.Zero; bool similar = true; INBuffer[0] = 0; //ReadFile(ReadHandleToUSBDevice, pINBuffer, 65, ref BytesRead, IntPtr.Zero); if (USBReadFileManagedBuffer(ReadHandleToUSBDevice, INBuffer, 65, ref BytesRead, IntPtr.Zero)) //Blocking function, unless an "overlapped" structure is used { if (BytesRead != 0) { for (int i = 0; i < BytesRead; i++) if (INBuffer[i] != OUTBuffer[i]) similar = false; if (similar) { //MessageBox.Show("Comm confirmed"); usb_state = USB_DEFINES.USB_CNC_NULL; } else { MessageBox.Show("Message Error"); usb_state = (USB_DEFINES)OUTBuffer[1]; } } else MessageBox.Show("I receive nothing"); } break; case USB_DEFINES.USB_CNC_RESET: OUTBuffer[0] = 0; OUTBuffer[1] = (byte) USB_DEFINES.USB_CNC_RESET; for (uint i = 2; i < 65; i++) OUTBuffer[i] = 0xFF; WriteFile(WriteHandleToUSBDevice, OUTBuffer, 65, ref BytesWritten, IntPtr.Zero); // switch usb to idle state usb_state = USB_DEFINES.USB_CNC_NULL; break; case USB_DEFINES.USB_CNC_RESET_ACTUAL_POS: OUTBuffer[0] = 0; OUTBuffer[1] = (byte)USB_DEFINES.USB_CNC_RESET_ACTUAL_POS; for (uint i = 2; i < 65; i++) OUTBuffer[i] = 0xFF; OUTBuffer[2] = (byte)(stepMotorDataOut.x & 0xff); OUTBuffer[3] = (byte)((stepMotorDataOut.x >> 8) & 0xff); OUTBuffer[5] = (byte)(stepMotorDataOut.new_x & 0xff); OUTBuffer[6] = (byte)((stepMotorDataOut.new_x >> 8) & 0xff); OUTBuffer[10] = (byte)(stepMotorDataOut.y & 0xff); OUTBuffer[11] = (byte)((stepMotorDataOut.y >> 8) & 0xff); OUTBuffer[13] = (byte)(stepMotorDataOut.new_y & 0xff); OUTBuffer[14] = (byte)((stepMotorDataOut.new_y >> 8) & 0xff); WriteFile(WriteHandleToUSBDevice, OUTBuffer, 65, ref BytesWritten, IntPtr.Zero); // switch usb to idle state usb_state = USB_DEFINES.USB_CNC_NULL; break; case USB_DEFINES.USB_CNC_SEND_DATA: OUTBuffer[0] = 0; OUTBuffer[1] = (byte) USB_DEFINES.USB_CNC_SEND_DATA; for (uint i = 2; i < 65; i++) OUTBuffer[i] = 0xFF; if (WriteFile(WriteHandleToUSBDevice, OUTBuffer, 65, ref BytesWritten, IntPtr.Zero)) { INBuffer[0] = 0; if (USBReadFileManagedBuffer(ReadHandleToUSBDevice, INBuffer, 65, ref BytesRead, IntPtr.Zero)) //Blocking function, unless an "overlapped" structure is used { if (INBuffer[1] == (byte)USB_DEFINES.USB_CNC_SEND_DATA) { stepMotorDataIn.x = (Int16)((INBuffer[3] << 8) + INBuffer[2]); stepMotorDataIn.step_x = INBuffer[4]; stepMotorDataIn.new_x = (Int16)((INBuffer[6] << 8) + INBuffer[5]); stepMotorDataIn.dir_x = (DIRECTION_OF_MOVEMENT)INBuffer[7]; stepMotorDataIn.end_x1 = INBuffer[8] > 0 ? true : false; stepMotorDataIn.end_x2 = INBuffer[9] > 0 ? true : false; stepMotorDataIn.y = (Int16)((INBuffer[11] << 8) + INBuffer[10]); stepMotorDataIn.step_y = INBuffer[12]; stepMotorDataIn.new_y = (Int16)((INBuffer[14] << 8) + INBuffer[13]); stepMotorDataIn.dir_y = (DIRECTION_OF_MOVEMENT)INBuffer[15]; stepMotorDataIn.end_y1 = INBuffer[16] > 0 ? true : false; stepMotorDataIn.end_y2 = INBuffer[17] > 0 ? true : false; stepMotorDataIn.state = (STATE_OF_MOVEMENT)INBuffer[18]; stepMotorDataIn.feed_x = (UInt16)((INBuffer[20] << 8) + INBuffer[19]); stepMotorDataIn.feed_y = (UInt16)((INBuffer[22] << 8) + INBuffer[21]); stepMotorDataIn.current = (CURRENT)INBuffer[23]; stepMotorDataIn.controll_state = (CONTROLL_STATES)INBuffer[24]; } // switch usb to idle state, but check if we don't want to send data first if (usb_state != USB_DEFINES.USB_CNC_RECEIVE_DATA) usb_state = USB_DEFINES.USB_CNC_NULL; } } break; case USB_DEFINES.USB_CNC_RECEIVE_DATA: OUTBuffer = USBPrepareOutBuffer(stepMotorDataOut, USB_DEFINES.USB_CNC_RECEIVE_DATA); WriteFile(WriteHandleToUSBDevice, OUTBuffer, 65, ref BytesWritten, IntPtr.Zero); // switch usb to confirm mode usb_state = USB_DEFINES.USB_CNC_CONFIRM_DATA; //ContReadCheckBox.Checked = true; break; case USB_DEFINES.USB_CNC_JOYSTICK: OUTBuffer = USBPrepareOutBuffer(stepMotorDataOut, USB_DEFINES.USB_CNC_JOYSTICK); WriteFile(WriteHandleToUSBDevice, OUTBuffer, 65, ref BytesWritten, IntPtr.Zero); //ContReadCheckBox.Checked = true; // switch usb to confirm mode usb_state = USB_DEFINES.USB_CNC_CONFIRM_DATA; break; case USB_DEFINES.USB_CNC_AUTOMATIC: OUTBuffer = USBPrepareOutBuffer(stepMotorDataOut, USB_DEFINES.USB_CNC_AUTOMATIC); WriteFile(WriteHandleToUSBDevice, OUTBuffer, 65, ref BytesWritten, IntPtr.Zero); //ContReadCheckBox.Checked = true; // switch usb to idle state usb_state = USB_DEFINES.USB_CNC_NULL; break; } // check if bootloader if (Bootloader == true) { OUTBuffer[0] = 0; //The first byte is the "Report ID" and does not get sent over the USB bus. Always set = 0. OUTBuffer[1] = 0x38; //0x80 is the "Toggle LED(s)" command in the firmware for (uint i = 2; i < 65; i++) //This loop is not strictly necessary. Simply initializes unused bytes to OUTBuffer[i] = 0xFF; //0xFF for lower EMI and power consumption when driving the USB cable. //Now send the packet to the USB firmware on the microcontroller WriteFile(WriteHandleToUSBDevice, OUTBuffer, 65, ref BytesWritten, IntPtr.Zero); //Blocking function, unless an "overlapped" structure is used Bootloader = false; } } //end of: if(AttachedState == true) else { Thread.Sleep(2); //Add a small delay. Otherwise, this while(true) loop can execute very fast and cause //high CPU utilization, with no particular benefit to the application. } } catch { } } //end of while(true) loop //-------------------------------------------------------END CUT AND PASTE BLOCK------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------------------------------------------------------------- }
private Byte[] USBPrepareOutBuffer(StepMotorData s, USB_DEFINES usb_def) { Byte[] OUTBuffer = new Byte[65]; OUTBuffer[0] = 0; OUTBuffer[1] = (byte)usb_def; for (uint i = 2; i < 65; i++) OUTBuffer[i] = 0xFF; OUTBuffer[2] = (byte)(s.x & 0xff); OUTBuffer[3] = (byte)((s.x >> 8) & 0xff); OUTBuffer[4] = s.step_x; OUTBuffer[5] = (byte)(s.new_x & 0xff); OUTBuffer[6] = (byte)((s.new_x >> 8) & 0xff); OUTBuffer[7] = (byte)s.dir_x; OUTBuffer[8] = s.end_x1 == true ? (byte)1 : (byte)0; OUTBuffer[9] = s.end_x2 == true ? (byte)1 : (byte)0; OUTBuffer[10] = (byte)(s.y & 0xff); OUTBuffer[11] = (byte)((s.y >> 8) & 0xff); OUTBuffer[12] = s.step_y; OUTBuffer[13] = (byte)(s.new_y & 0xff); OUTBuffer[14] = (byte)((s.new_y >> 8) & 0xff); OUTBuffer[15] = (byte)s.dir_y; OUTBuffer[16] = s.end_y1 == true ? (byte)1 : (byte)0; OUTBuffer[17] = s.end_y2 == true ? (byte)1 : (byte)0; OUTBuffer[18] = (byte)s.state; OUTBuffer[19] = (byte)(s.feed_x & 0xff); OUTBuffer[20] = (byte)((s.feed_x >> 8) & 0xff); OUTBuffer[21] = (byte)(s.feed_y & 0xff); OUTBuffer[22] = (byte)((s.feed_y >> 8) & 0xff); OUTBuffer[23] = (byte)s.current; OUTBuffer[24] = (byte)s.controll_state; return OUTBuffer; }
private void USBDataReadButton_Click(object sender, EventArgs e) { // wysyla dane do pc usb_state = USB_DEFINES.USB_CNC_SEND_DATA; }
private void TimerFormUpdate_Tick(object sender, EventArgs e) { //------------------------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------BEGIN CUT AND PASTE BLOCK----------------------------------------------------------------------------------- //This timer tick event handler function is used to update the user interface on the form, based on data //obtained asynchronously by the ReadWriteThread and the WM_DEVICECHANGE event handler functions. USBCheckIfPresent(); //Check if user interface on the form should be enabled or not, based on the attachment state of the USB device. if (AttachedState == true) { //Device is connected and ready to communicate, enable user interface on the form StatusBar.Text = cnc_connected; } if ((AttachedState == false) || (AttachedButBroken == true)) { //Device not available to communicate. Disable user interface on the form. StatusBar.Text = cnc_not_connected; } // fill form windows with data if (usb_state == USB_DEFINES.USB_CNC_NULL) { DataFill(stepMotorDataIn); usb_state = USB_DEFINES.USB_CNC_SEND_DATA; } if (plytka_wygenerowana) { ReturnToFirstButton.Enabled = true; NextDrillButton.Enabled = true; PrevDrillButton.Enabled = true; } OtworTextBox.Text = aktualny_otwor.ToString(); /* if (sterowanie_reczne) { switch (send_manual) { case true: WyslijDane(USB_DEFINES.USB_CNC_MANUAL); send_manual = !send_manual; break; case false: Key_Off(); break; } } */ // debug try { textBox.Text = usb_state.ToString(); } catch { } //Refresh(); //-------------------------------------------------------END CUT AND PASTE BLOCK------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------------------------------------------------------------- }
private void MainForm_Load(object sender, EventArgs e) { // program version this.Text = "DrillCAD v." + PublishVersion; // additional events this.canvas2D.MouseWheel += new MouseEventHandler(Canvas2D_MouseWheel); // usb state usb_state = USB_DEFINES.USB_CNC_NULL; // jednostki currentUnits = Units.kroki; JednostkiComboBox.SelectedIndex = JednostkiComboBox.Items.IndexOf(Units.kroki.ToString()); // step motor init stepMotorDataIn.x = 0; stepMotorDataIn.new_x = 0; stepMotorDataIn.step_x = 0; stepMotorDataIn.dir_x = DIRECTION_OF_MOVEMENT.DIRECTION_STOP; stepMotorDataIn.end_x1 = false; stepMotorDataIn.end_x2 = false; stepMotorDataIn.y = 0; stepMotorDataIn.new_y = 0; stepMotorDataIn.step_y = 0; stepMotorDataIn.dir_y = DIRECTION_OF_MOVEMENT.DIRECTION_STOP; stepMotorDataIn.end_y1 = false; stepMotorDataIn.end_y2 = false; stepMotorDataIn.state = STATE_OF_MOVEMENT.STATE_MOVE; stepMotorDataIn.feed_x = STEP_MOTOR_MAX_FEED; stepMotorDataIn.feed_y = STEP_MOTOR_MAX_FEED; stepMotorDataIn.current = CURRENT.CURRENT_ON; stepMotorDataIn.controll_state = CONTROLL_STATES.CON_AUTOMATIC; stepMotorDataOut = stepMotorDataIn; DataFill(stepMotorDataIn); DataFillNew(); }