Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package GUI; import static com.sun.jna.Pointer.NULL; import java.awt.Component; import java.io.PrintWriter; import java.util.List; import purejavahidapi.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.swing.JPanel; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.XYPlot; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; //import javax.comm.*; /** * * @author gadis */ public class GUIModel extends javax.swing.JFrame { /** * Creates new form GUIModel */ public GUIModel() { initComponents(); } //Edited by SureshGadi private HidDevice PIC = (HidDevice) NULL; //static SerialPort serialPort; private boolean ARecInp00 = false; private boolean ARecInp01 = false; private boolean ARecInp02 = false; private boolean RunCont = false; private boolean RCThread = false; private boolean ExpThread = false; private boolean StopExperiment = false; PlotHere PlotAr; //private boolean IsConnected=false; //End SureshGadi /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jFrame1 = new javax.swing.JFrame(); BtnConnect = new javax.swing.JButton(); StatusText = new javax.swing.JLabel(); Clockwise = new javax.swing.JButton(); CClockwise = new javax.swing.JButton(); BtnSetZero = new javax.swing.JButton(); NSteps = new javax.swing.JSpinner(); ADC00 = new javax.swing.JTextField(); ReadA0 = new javax.swing.JButton(); ReadA1 = new javax.swing.JButton(); ReadA2 = new javax.swing.JButton(); ADC01 = new javax.swing.JTextField(); ADC02 = new javax.swing.JTextField(); TurnNRead = new javax.swing.JButton(); SetClkOCClk = new javax.swing.JCheckBox(); StartExp = new javax.swing.JButton(); Steps = new javax.swing.JSpinner(); jSeparator1 = new javax.swing.JSeparator(); jSeparator2 = new javax.swing.JSeparator(); jSeparator3 = new javax.swing.JSeparator(); ReadContinuous = new javax.swing.JToggleButton(); StopExp = new javax.swing.JButton(); ExpStatus = new javax.swing.JLabel(); MotorStepDelay = new javax.swing.JSpinner(); jSeparator4 = new javax.swing.JSeparator(); SetMotorStepDelay = new javax.swing.JButton(); ExperimentName = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); Comments = new javax.swing.JTextArea(); jLabel2 = new javax.swing.JLabel(); ViewGraph = new javax.swing.JButton(); StartCapture = new javax.swing.JButton(); SecDelay = new javax.swing.JTextField(); javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane()); jFrame1.getContentPane().setLayout(jFrame1Layout); jFrame1Layout.setHorizontalGroup(jFrame1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE)); jFrame1Layout.setVerticalGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE)); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("USB Digital data aquisition"); setName("GUIFrame"); // NOI18N BtnConnect.setText("Connect"); BtnConnect.setName(""); // NOI18N BtnConnect.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BtnConnectActionPerformed(evt); } }); StatusText.setText("Solar Project"); Clockwise.setText("Clockwise"); Clockwise.setEnabled(false); Clockwise.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ClockwiseActionPerformed(evt); } }); CClockwise.setText("Counter Clockwise"); CClockwise.setEnabled(false); CClockwise.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { CClockwiseActionPerformed(evt); } }); BtnSetZero.setText("Set zero"); BtnSetZero.setEnabled(false); BtnSetZero.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BtnSetZeroActionPerformed(evt); } }); NSteps.setModel(new javax.swing.SpinnerNumberModel(0, 0, 254, 1)); NSteps.setEnabled(false); NSteps.setValue(1); ADC00.setEditable(false); ADC00.setText("0.000"); ADC00.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ADC00ActionPerformed(evt); } }); ReadA0.setText("Read A0"); ReadA0.setEnabled(false); ReadA0.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ReadA0ActionPerformed(evt); } }); ReadA1.setText("Read A1"); ReadA1.setEnabled(false); ReadA1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ReadA1ActionPerformed(evt); } }); ReadA2.setText("Read A2"); ReadA2.setEnabled(false); ReadA2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ReadA2ActionPerformed(evt); } }); ADC01.setEditable(false); ADC01.setText("0.000"); ADC01.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ADC01ActionPerformed(evt); } }); ADC02.setEditable(false); ADC02.setText("0.000"); ADC02.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ADC02ActionPerformed(evt); } }); TurnNRead.setText("Turn and Read"); TurnNRead.setEnabled(false); TurnNRead.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { TurnNReadActionPerformed(evt); } }); SetClkOCClk.setText("Clockwise (click to change)"); SetClkOCClk.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SetClkOCClkActionPerformed(evt); } }); StartExp.setText("Start Experiment"); StartExp.setEnabled(false); StartExp.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { StartExpActionPerformed(evt); } }); Steps.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(10), Integer.valueOf(0), null, Integer.valueOf(1))); Steps.setEnabled(false); ReadContinuous.setText("Read Analog Continuous"); ReadContinuous.setEnabled(false); ReadContinuous.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ReadContinuousActionPerformed(evt); } }); StopExp.setText("Stop Experiment"); StopExp.setEnabled(false); StopExp.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { StopExpActionPerformed(evt); } }); MotorStepDelay.setModel(new javax.swing.SpinnerNumberModel(1, 0, 255, 1)); MotorStepDelay.setEnabled(false); SetMotorStepDelay.setText("Set Delay"); SetMotorStepDelay.setEnabled(false); SetMotorStepDelay.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SetMotorStepDelayActionPerformed(evt); } }); ExperimentName.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ExperimentNameActionPerformed(evt); } }); jLabel1.setText("Experiment Name"); Comments.setColumns(20); Comments.setRows(5); jScrollPane1.setViewportView(Comments); jLabel2.setText("Comments"); ViewGraph.setText("View Graph"); ViewGraph.setEnabled(false); ViewGraph.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ViewGraphActionPerformed(evt); } }); StartCapture.setText("Start"); StartCapture.setEnabled(false); StartCapture.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { StartCaptureActionPerformed(evt); } }); SecDelay.setText("0"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(StatusText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jSeparator4, javax.swing.GroupLayout.Alignment.LEADING)) .addGap(10, 10, 10)) .addGroup(layout.createSequentialGroup().addComponent(BtnConnect).addContainerGap(375, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jSeparator1) .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup().addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(BtnSetZero, javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout .createSequentialGroup() .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(ReadA2, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(ReadA1, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Clockwise, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(ReadA0, javax.swing.GroupLayout.Alignment.LEADING)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(NSteps, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(CClockwise)) .addGroup(layout.createSequentialGroup() .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(ADC02, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(ADC01, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(ADC00, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(ReadContinuous))))) .addGap(0, 0, Short.MAX_VALUE))) .addGap(10, 10, 10)) .addComponent(jSeparator3) .addComponent(SetClkOCClk, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(ExperimentName).addComponent(jScrollPane1))) .addGroup(layout.createSequentialGroup().addComponent(SetMotorStepDelay) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(MotorStepDelay, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap()) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(StartExp, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(Steps, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(ExpStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(ViewGraph, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(StopExp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(TurnNRead, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addComponent(StartCapture) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(SecDelay, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 0, Short.MAX_VALUE))))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(BtnConnect) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(StartCapture).addComponent(SecDelay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(SetMotorStepDelay) .addComponent(MotorStepDelay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(14, 14, 14) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(BtnSetZero) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(NSteps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Clockwise).addComponent(CClockwise)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(ADC00, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(ReadA0)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(ReadA1).addComponent(ADC01, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(ReadA2).addComponent(ADC02, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(ReadContinuous, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(ExperimentName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(18, 18, 18).addComponent(SetClkOCClk).addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(StartExp) .addComponent(Steps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(TurnNRead)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(StopExp).addComponent(ExpStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(ViewGraph) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(StatusText).addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void BtnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnConnectActionPerformed // TODO add your handling code here: List<HidDeviceInfo> devList = PureJavaHidApi.enumerateDevices(); StatusText.setText("No device found"); for (HidDeviceInfo info : devList) { //System.out.println(info.getProductId()); if ((info.getVendorId() == 0x12BF) && (info.getProductId() == 0x0010)) { StatusText.setText("Got it"); //System.out.println("Got it"); try { PIC = PureJavaHidApi.openDevice(info.getPath()); PIC.setInputReportListener(new InputReportListener() { @Override public void onInputReport(HidDevice source, byte Id, byte[] data, int len) { // change flag in order for waiting loop to finish PORTA = data[0]; PORTB = data[1]; PORTC = data[2]; receivedFlag = true; double VoltEqal = (data[1] & 0xFF) + (data[0]) * 256;//+(int)data[0]; VoltEqal = VoltEqal * 5 / 1024; if (data[2] == 0x0) { ADC00.setText(String.valueOf(VoltEqal)); ARecInp00 = true; } if (data[2] == 0x1) { ADC01.setText(String.valueOf(VoltEqal)); ARecInp01 = true; } if (data[2] == 0x2) { ADC02.setText(String.valueOf(VoltEqal)); ARecInp02 = true; } /* if ((data[0] >= 0x30) && (data[0] <= 0x39)) { if (data[7] == 0x41) { data[7] = 0x30; String s = new String(data); if (Float.parseFloat(s)>=0) ADC00.setText(s.substring(0, 8)); ARecInp00 = true; } if (data[7] == 0x42) { data[7] = 0x30; String s = new String(data); if (Float.parseFloat(s)>=0) ADC01.setText(s.substring(0, 8)); ARecInp01 = true; } if (data[7] == 0x43) { data[7] = 0x30; String s = new String(data); if (Float.parseFloat(s)>=0) ADC02.setText(s.substring(0, 8)); ARecInp02 = true; } }*/ } }); //System.out.println("Connected"); StatusText.setText("Connected"); SetEnabledActivities(true); } catch (Exception e) { //System.out.println("Unable to connect"); StatusText.setText("Unable to connect"); } break; } } }//GEN-LAST:event_BtnConnectActionPerformed private void BtnSetZeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSetZeroActionPerformed // TODO add your handling code here: byte[] data = new byte[] { 0x8 }; // my data byte ReportID = 0; try { int sent = PIC.setOutputReport(ReportID, data, data.length); if (sent > 0) StatusText.setText("Set to zero"); else { StatusText.setText("Communication error"); SetEnabledActivities(false); } } catch (Exception e) { SetEnabledActivities(false); StatusText.setText( "Communication Error: Please remove and connect the USB cable and press reset button on the PIC."); } }//GEN-LAST:event_BtnSetZeroActionPerformed private void ClockwiseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClockwiseActionPerformed // TODO add your handling code here: int n = Integer.parseInt(NSteps.getValue().toString()); byte[] data = new byte[2]; // my data data[0] = 0x1; data[1] = (byte) n; byte ReportID = 0; try { int sent = PIC.setOutputReport(ReportID, data, data.length); if (sent > 0) StatusText.setText("Rotated clockwise"); else { StatusText.setText("Communication error"); SetEnabledActivities(false); } } catch (Exception e) { SetEnabledActivities(false); StatusText.setText( "Communication Error: Please remove and connect the USB cable and press reset button on the PIC."); } }//GEN-LAST:event_ClockwiseActionPerformed private void CClockwiseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CClockwiseActionPerformed // TODO add your handling code here: int n = Integer.parseInt(NSteps.getValue().toString()); byte[] data = new byte[2]; // my data data[0] = 0x2; data[1] = (byte) n; byte ReportID = 0; try { int sent = PIC.setOutputReport(ReportID, data, data.length); if (sent > 0) StatusText.setText("Rotated Counter clockwise"); else { StatusText.setText("Communication error"); SetEnabledActivities(false); } } catch (Exception e) { SetEnabledActivities(false); StatusText.setText( "Communication Error: Please remove and connect the USB cable and press reset button on the PIC."); } }//GEN-LAST:event_CClockwiseActionPerformed private void ADC00ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ADC00ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_ADC00ActionPerformed private void ADC01ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ADC01ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_ADC01ActionPerformed private void ADC02ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ADC02ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_ADC02ActionPerformed private void ReadAngAction(int i) { int n = Integer.parseInt(NSteps.getValue().toString()); byte[] data = new byte[1]; // my data data[0] = (byte) ((int) 0x03 + i); byte ReportID = 0; try { int sent = PIC.setOutputReport(ReportID, data, data.length); if (sent > 0) StatusText.setText("Requested for Analog channel ".concat(String.valueOf(i))); else { StatusText.setText("Communication error"); SetEnabledActivities(false); } } catch (Exception e) { SetEnabledActivities(false); StatusText.setText( "Communication Error: Please remove and connect the USB cable and press reset button on the PIC."); } } private void ReadA0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReadA0ActionPerformed // TODO add your handling code here: ReadAngAction(0); }//GEN-LAST:event_ReadA0ActionPerformed private void ReadA1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReadA1ActionPerformed // TODO add your handling code here: ReadAngAction(1); }//GEN-LAST:event_ReadA1ActionPerformed private void ReadA2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReadA2ActionPerformed // TODO add your handling code here: ReadAngAction(2); }//GEN-LAST:event_ReadA2ActionPerformed private void TurnNReadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TurnNReadActionPerformed // TODO add your handling code here: //int n = Integer.parseInt(NSteps.getValue().toString()); byte[] data = new byte[1]; // my data if (SetClkOCClk.isSelected()) data[0] = 0x06; else data[0] = 0x07; //data[2] = (byte) n; byte ReportID = 0; try { int sent = PIC.setOutputReport(ReportID, data, data.length); if (sent > 0) StatusText.setText("Rotated and read"); else { StatusText.setText("Communication error"); SetEnabledActivities(false); } } catch (Exception e) { SetEnabledActivities(false); StatusText.setText( "Communication Error: Please remove and connect the USB cable and press reset button on the PIC."); } data = new byte[1]; // my data data[0] = 0x09; ReportID = 0; try { int sent = PIC.setOutputReport(ReportID, data, data.length); if (sent > 0) StatusText.setText("Rotated and read"); else { StatusText.setText("Communication error"); SetEnabledActivities(false); } } catch (Exception e) { SetEnabledActivities(false); StatusText.setText( "Communication Error: Please remove and connect the USB cable and press reset button on the PIC."); } }//GEN-LAST:event_TurnNReadActionPerformed private void SetAllAInputRec(boolean set) { ARecInp00 = set; ARecInp01 = set; ARecInp02 = set; } private boolean WaitTillReceived() throws Exception { int i = 0; while (!(ARecInp00 && ARecInp01 && ARecInp02)) { //Thread.sleep(0, 10); Thread.sleep(1); i = i + 1; if (i == 500) return false; } return true; } private void SetClkOCClkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SetClkOCClkActionPerformed // TODO add your handling code here: if (SetClkOCClk.isSelected()) SetClkOCClk.setText("Counter Clockwise (click to change)"); else SetClkOCClk.setText("Clockwise (click to change)"); }//GEN-LAST:event_SetClkOCClkActionPerformed private void StartExpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartExpActionPerformed // TODO add your handling code here: //int n = Integer.parseInt(NSteps.getValue().toString()); final PlotHere PlotArea = new PlotHere("Experiment Results"); PlotAr = PlotArea; PlotArea.setDefaultCloseOperation(HIDE_ON_CLOSE); PlotArea.setVisible(true);//.show(); if (!ExpThread) { ExpThread = true; StopExperiment = false; ExpStatus.setText("Experiment started"); EnableForExperiment(false); Thread t1 = new Thread(new Runnable() { @Override public void run() { PrintWriter writer; DateFormat df1 = new SimpleDateFormat("yyyyMMdd-HHmmss-"); DateFormat df2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date today = Calendar.getInstance().getTime(); String reportDate = df1.format(today); try { writer = new PrintWriter(reportDate.concat(ExperimentName.getText()).concat(".csv"), "UTF-8"); byte[] data = new byte[1]; // my data if (SetClkOCClk.isSelected()) data[0] = 0x6; else data[0] = 0x7; //data[2] = (byte) n; byte ReportID = 0; if (ExperimentName.getText().length() != 0) writer.printf("\"Experiment Name\",\"%s\"\n", ExperimentName.getText()); if (Comments.getText().length() != 0) writer.printf("\"Comments\",\"%s\"\n", Comments.getText()); writer.printf("\"Motor step delay\",\"%s\"\n", MotorStepDelay.getValue().toString()); for (int i = 0; i < Integer.parseInt(Steps.getValue().toString()); i = i + 1) { if (StopExperiment) break; try { SetAllAInputRec(false); int sent = PIC.setOutputReport(ReportID, data, data.length); if (sent > 0) StatusText.setText("Rotated and read"); else { StatusText.setText("Communication error"); SetEnabledActivities(false); } if (sent > 0) { today = Calendar.getInstance().getTime(); reportDate = df2.format(today); if (WaitTillReceived()) { writer.printf("\"%s\",\"%d\",\"%s\",\"%s\",\"%s\"\n", reportDate, i, ADC00.getText(), ADC01.getText(), ADC02.getText()); PlotArea.PutPoint(0, i * 0.015, Double.parseDouble(ADC00.getText())); PlotArea.PutPoint(1, i * 0.015, Double.parseDouble(ADC01.getText())); PlotArea.PutPoint(2, i * 0.015, Double.parseDouble(ADC02.getText())); } else writer.printf("\"%s\",\"%d\",\"nan\",\"nan\",\"nan\"\n", reportDate, i); } StatusText.setText("Finished step ".concat(String.valueOf(i + 1))); ExpStatus.setText("Finished step ".concat(String.valueOf(i + 1))); } catch (Exception e) { SetEnabledActivities(false); StatusText.setText( "Communication Error: Please remove and connect the USB cable and press reset button on the PIC."); } /*try { Thread.sleep(100); } catch (Exception e){}*/ } writer.close(); data[0] = 0x9; PIC.setOutputReport(ReportID, data, data.length); } catch (Exception e) { StatusText.setText("Unable to open the file."); } ExpThread = false; SetEnabledActivities(true); } }); t1.start(); } PlotArea.setDefaultCloseOperation(DISPOSE_ON_CLOSE); }//GEN-LAST:event_StartExpActionPerformed public void PutPoint(double key, double x, double y, JPanel GraphHerePanel) { Component[] a = GraphHerePanel.getComponents(); ChartPanel chartpanel = (ChartPanel) a[0]; JFreeChart chart = chartpanel.getChart(); XYPlot plot = (XYPlot) chart.getPlot(); XYSeriesCollection data = (XYSeriesCollection) plot.getDataset(); XYSeries XYseries = data.getSeries(key); XYseries.add(x, y); this.revalidate(); this.repaint(); } public void SetStatusText(String str) { StatusText.setText(str); } private void ReadContinuousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReadContinuousActionPerformed // TODO add your handling code here: if (!RCThread) { RCThread = true; Thread t1 = new Thread(new Runnable() { @Override public void run() { while (true) { for (int i = 0; i < 3; i++) { ReadAngAction(i); try { Thread.sleep(50); } catch (Exception e) { } } do { try { Thread.sleep(100); } catch (Exception e) { } } while (!RunCont); } } }); t1.start(); } if (ReadContinuous.isSelected()) { RunCont = true; EnableForContRead(false); StatusText.setText("Continuous reading started"); } else { RunCont = false; EnableForContRead(true); StatusText.setText("Continuous reading stopped"); } // ReadContinuous. }//GEN-LAST:event_ReadContinuousActionPerformed private void StopExpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StopExpActionPerformed // TODO add your handling code here: StopExperiment = true; }//GEN-LAST:event_StopExpActionPerformed private void SetMotorStepDelayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SetMotorStepDelayActionPerformed // TODO add your handling code here: int n = Integer.parseInt(MotorStepDelay.getValue().toString()); byte[] data = new byte[2]; // my data data[0] = 0x10; data[1] = (byte) n; byte ReportID = 0; try { int sent = PIC.setOutputReport(ReportID, data, data.length); if (sent > 0) StatusText.setText("Step Delay change requested"); else { StatusText.setText("Communication error"); SetEnabledActivities(false); } } catch (Exception e) { SetEnabledActivities(false); StatusText.setText( "Communication Error: Please remove and connect the USB cable and press reset button on the PIC."); } }//GEN-LAST:event_SetMotorStepDelayActionPerformed private void ExperimentNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExperimentNameActionPerformed // TODO add your handling code here: }//GEN-LAST:event_ExperimentNameActionPerformed private void ViewGraphActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewGraphActionPerformed // TODO add your handling code here: PlotAr.setVisible(true);//.show(); }//GEN-LAST:event_ViewGraphActionPerformed public byte getBit(byte ID, int pos) { return (byte) ((ID >> pos) & (0x01)); } public boolean SendAndWait() { receivedFlag = false; byte[] data = new byte[2]; // my data data[0] = 0x3; byte ReportID = 0; try { int sent = PIC.setOutputReport(ReportID, data, data.length); if (sent > 0) StatusText.setText("Sent request"); else { StatusText.setText("Communication error"); SetEnabledActivities(false); } int i = 0; while (!(receivedFlag)) { Thread.sleep(1); i = i + 1; if (i == 500) return false; } } catch (Exception e) { SetEnabledActivities(false); StatusText.setText( "Communication Error: Please remove and connect the USB cable and press reset button on the PIC."); return false; } return true; } private void StartCaptureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartCaptureActionPerformed final PlotHere PlotArea = new PlotHere("Data Aquisition"); PlotAr = PlotArea; PlotArea.setDefaultCloseOperation(HIDE_ON_CLOSE); //PlotArea.setVisible(true); if (!ExpThread) { ExpThread = true; StopExperiment = false; ExpStatus.setText("Experiment started"); EnableForExperiment(false); Thread t1 = new Thread(new Runnable() { @Override public void run() { PrintWriter writer; DateFormat df1 = new SimpleDateFormat("yyyyMMdd-HHmmss-"); DateFormat df2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date today = Calendar.getInstance().getTime(); String reportDate = df1.format(today); try { writer = new PrintWriter(reportDate.concat(ExperimentName.getText()).concat(".csv"), "UTF-8"); byte[] data = new byte[1]; // my data if (SetClkOCClk.isSelected()) data[0] = 0x6; else data[0] = 0x7; //data[2] = (byte) n; byte ReportID = 0; if (ExperimentName.getText().length() != 0) writer.printf("\"Experiment Name\",\"%s\"\n", ExperimentName.getText()); if (Comments.getText().length() != 0) writer.printf("\"Comments\",\"%s\"\n", Comments.getText()); for (int i = 0; i < 10 * Integer.parseInt(Steps.getValue().toString()); i = i + 1) { Thread.sleep(100); if (StopExperiment) break; try { today = Calendar.getInstance().getTime(); reportDate = df2.format(today); if (SendAndWait()) { writer.printf("\"%s\"", reportDate); /*for (int Ite=0;Ite<8;Ite++) { writer.printf(",\"%d\"",getBit(PORTA,Ite)); } for (int Ite=0;Ite<8;Ite++) { writer.printf(",\"%d\"",getBit(PORTB,Ite)); }*/ for (int Ite = 0; Ite < 8; Ite++) { writer.printf(",\"%d\"", getBit(PORTC, Ite)); } writer.printf("\n"); //writer.printf("\"%s\",\"%d\",\"%s\",\"%s\",\"%s\"\n",reportDate, i, ADC00.getText(),ADC01.getText(),ADC02.getText()); /*PlotArea.PutPoint(0, i*0.015, Double.parseDouble(ADC00.getText())); PlotArea.PutPoint(1, i*0.015, Double.parseDouble(ADC01.getText())); PlotArea.PutPoint(2, i*0.015, Double.parseDouble(ADC02.getText()));*/ } else writer.printf("\"%s\",\"%d\",\"nan\",\"nan\",\"nan\"\n", reportDate, i); StatusText.setText("Finished step ".concat(String.valueOf(i + 1))); ExpStatus.setText("Finished step ".concat(String.valueOf(i + 1))); } catch (Exception e) { SetEnabledActivities(false); StatusText.setText( "Communication Error: Please remove and connect the USB cable and press reset button on the PIC."); } /*try { Thread.sleep(100); } catch (Exception e){}*/ } writer.close(); data[0] = 0x9; PIC.setOutputReport(ReportID, data, data.length); } catch (Exception e) { StatusText.setText("Unable to open the file."); } ExpThread = false; SetEnabledActivities(true); } }); t1.start(); } PlotArea.setDefaultCloseOperation(DISPOSE_ON_CLOSE); }//GEN-LAST:event_StartCaptureActionPerformed private void EnableForContRead(boolean set) { BtnSetZero.setEnabled(set); Clockwise.setEnabled(set); CClockwise.setEnabled(set); ReadA0.setEnabled(set); ReadA1.setEnabled(set); ReadA2.setEnabled(set); TurnNRead.setEnabled(set); SetClkOCClk.setEnabled(set); StartExp.setEnabled(set); NSteps.setEnabled(set); Steps.setEnabled(set); SetMotorStepDelay.setEnabled(set); MotorStepDelay.setEnabled(set); //ReadContinuous.setEnabled(set); //BtnConnect.setEnabled(!set); } private void EnableForExperiment(boolean set) { BtnSetZero.setEnabled(set); Clockwise.setEnabled(set); CClockwise.setEnabled(set); ReadA0.setEnabled(set); ReadA1.setEnabled(set); ReadA2.setEnabled(set); TurnNRead.setEnabled(set); SetClkOCClk.setEnabled(set); StartExp.setEnabled(set); ReadContinuous.setEnabled(set); NSteps.setEnabled(set); Steps.setEnabled(set); SetMotorStepDelay.setEnabled(set); MotorStepDelay.setEnabled(set); ExperimentName.setEnabled(set); Comments.setEnabled(set); StopExp.setEnabled(!set); ViewGraph.setEnabled(!set); BtnConnect.setEnabled(set); } private void SetEnabledActivities(boolean set) { BtnSetZero.setEnabled(set); Clockwise.setEnabled(set); CClockwise.setEnabled(set); ReadA0.setEnabled(set); ReadA1.setEnabled(set); ReadA2.setEnabled(set); TurnNRead.setEnabled(set); SetClkOCClk.setEnabled(set); StartExp.setEnabled(set); ReadContinuous.setEnabled(set); NSteps.setEnabled(set); Steps.setEnabled(set); SetMotorStepDelay.setEnabled(set); MotorStepDelay.setEnabled(set); ExperimentName.setEnabled(set); Comments.setEnabled(set); StopExp.setEnabled(!set); ViewGraph.setEnabled(!set); if (!set) { RunCont = false; ReadContinuous.setSelected(set); StatusText.setText("Continuous reading stopped"); } BtnConnect.setEnabled(!set); StartCapture.setEnabled(set); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(GUIModel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(GUIModel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(GUIModel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(GUIModel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new GUIModel().setVisible(true); } }); } private byte PORTA; private byte PORTB; private byte PORTC; private boolean receivedFlag = false; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField ADC00; private javax.swing.JTextField ADC01; private javax.swing.JTextField ADC02; private javax.swing.JButton BtnConnect; private javax.swing.JButton BtnSetZero; private javax.swing.JButton CClockwise; private javax.swing.JButton Clockwise; private javax.swing.JTextArea Comments; private javax.swing.JLabel ExpStatus; private javax.swing.JTextField ExperimentName; private javax.swing.JSpinner MotorStepDelay; private javax.swing.JSpinner NSteps; private javax.swing.JButton ReadA0; private javax.swing.JButton ReadA1; private javax.swing.JButton ReadA2; private javax.swing.JToggleButton ReadContinuous; private javax.swing.JTextField SecDelay; private javax.swing.JCheckBox SetClkOCClk; private javax.swing.JButton SetMotorStepDelay; private javax.swing.JButton StartCapture; private javax.swing.JButton StartExp; private javax.swing.JLabel StatusText; private javax.swing.JSpinner Steps; private javax.swing.JButton StopExp; private javax.swing.JButton TurnNRead; private javax.swing.JButton ViewGraph; private javax.swing.JFrame jFrame1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JSeparator jSeparator4; // End of variables declaration//GEN-END:variables }