import jssc.SerialPort; import jssc.SerialPortException;
public ards()import {jssc.SerialPort;
import initComponents();
}jssc.SerialPortException;
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code"> public ards() {
private void initComponents();
{ }
jLabel1 = new javax.swing.JLabel@SuppressWarnings("unchecked");
jPanel1// =<editor-fold newdefaultstate="collapsed" javax.swing.JPanel();
desc="Generated Code"> jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new
javax.swing.JTextArea private void initComponents(); {
jLabel1 = new javax.setTextswing.JLabel("jLabel1");
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants jLabel1.EXIT_ON_CLOSEsetText("jLabel1");
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformedsetDefaultCloseOperation(javajavax.awtswing.eventWindowConstants.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
}EXIT_ON_CLOSE);
jTextArea1 jButton1.setEditablesetText(false"jButton1");
jTextArea1 jButton1.setColumnsaddActionListener(20new java.awt.event.ActionListener(); {
jTextArea1 public void actionPerformed(java.setRowsawt.event.ActionEvent evt) {
jButton1ActionPerformed(5evt);
jScrollPane1.setViewportView(jTextArea1 }
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1jTextArea1.setLayoutsetEditable(jPanel1Layoutfalse);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(319, 319, 319)
.addComponent(jButton1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(154, 154, 154)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(140, 140, 140)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
jTextArea1.addContainerGapsetColumns(157, Short.MAX_VALUE))
20);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
jTextArea1.addGapsetRows(74, 74, 745);
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGapsetViewportView(94, Short.MAX_VALUE))
jTextArea1);
javax.swing.GroupLayout layoutjPanel1Layout = new javax.swing.GroupLayout(getContentPane()jPanel1);
getContentPane() jPanel1.setLayout(layoutjPanel1Layout);
layout jPanel1Layout.setHorizontalGroup(
layout jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(319, 319, 319)
.addComponent(jPanel1jButton1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(154, 154, 154)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZEPREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(140, 140, 140)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(157, Short.MAX_VALUE))
);
layout jPanel1Layout.setVerticalGroup(
layout jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(74, 74, 74)
.addComponent(jPanel1jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZEPREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(94, Short.MAX_VALUE))
);
pack javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
}// </editor-fold> getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { pack();
}// </editor-fold>
String str;
//Inprivate thevoid constructorjButton1ActionPerformed(java.awt.event.ActionEvent passevt) the{ name of the port with which we work
SerialPort serialPort = new SerialPort("COM6"); String str;
try {
//In the constructor pass the //Openname of the port with which we work
SerialPort serialPort.openPort = new SerialPort("COM6");
try {
//Open port
serialPort.openPort();
//We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0);
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
Thread.sleep(1000);//Very important !!!
//Read data from port
for (int i = 0; i < 1000; i++) {
str = serialPort.readString();
if (str != null) {
System.out.println(str);
jTextArea1.setText(str);
jLabel2.setText(str);
}
Thread.sleep(500);
}
Thread.sleep(500);
}
//Closing the port
serialPort.closePort();
} catch (SerialPortException ex) {
System.out.println(ex);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
/**
* @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@param withargs the default look and feel.
* For details seecommand http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.htmlline 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(ards.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ards.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ards.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ards.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
} catch (ClassNotFoundException ex) {//</editor-fold>
java.util.logging.Logger.getLogger(ards.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
/* Create }and catchdisplay (InstantiationExceptionthe ex)form {*/
java.util.loggingawt.LoggerEventQueue.getLoggerinvokeLater(ards.class.getNamenew Runnable()).log(java.util.logging.Level.SEVERE, null, ex);{
} catch (IllegalAccessException ex public void run() {
java.util.logging.Logger.getLogger( new ards.class.getName()).logsetVisible(java.util.logging.Level.SEVERE, null, extrue);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
}
java.util.logging.Logger.getLogger(ards.class.getName()).log(java.util.logging.Level.SEVERE, null, ex });
}
//</editor-fold>
/*/ CreateVariables anddeclaration display- thedo formnot */modify
java.awtprivate javax.EventQueueswing.invokeLater(new Runnable()JButton {jButton1;
private javax.swing.JLabel jLabel1;
public void run()private {javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane newjScrollPane1;
ards() private javax.setVisible(true);swing.JTextArea jTextArea1;
// End of variables }
declaration });
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
const int model = 0;
void setup() {
Serial.begin(9600);
}
float sensitivity[] ={ 0.185, // for ACS712ELCTR-05B-T 0.100, // for ACS712ELCTR-20A-T 0.066 // for ACS712ELCTR-30A-T } ;
void loop() {
static unsigned long count=0;
static float Vsum=0;
float Vave=0;
int volt = analogRead(A1);
float voltage= map(volt,0,1023, 0, 2500)/100.0;
Vsum=Vsum+voltage;
static float Csum=0; float Cave=0; float milli = pow(10, -3); float current = (voltage / sensitivity[model]); float current1 = current*milli; Csum=Csum+current1; count++; Vave=Vsum/count; Cave=Csum/count;
Serial.print("Average voltage: "); Serial.print(Vave,2); Serial.println("V"); Serial.print("Average current: "); Serial.print(Cave,3); Serial.println("A"); Serial.println("-----"); delay(3000);
}
const int model = 0;
void setup() {
Serial.begin(9600);
}
float sensitivity[] ={
0.185, // for ACS712ELCTR-05B-T
0.100, // for ACS712ELCTR-20A-T
0.066 // for ACS712ELCTR-30A-T } ;
void loop() {
static unsigned long count=0;
static float Vsum=0;
float Vave=0;
int volt = analogRead(A1);
float voltage= map(volt,0,1023, 0, 2500)/100.0;
Vsum=Vsum+voltage;
static float Csum=0;
float Cave=0;
float milli = pow(10, -3);
float current = (voltage / sensitivity[model]);
float current1 = current*milli;
Csum=Csum+current1;
count++;
Vave=Vsum/count;
Cave=Csum/count;
Serial.print("Average voltage: ");
Serial.print(Vave,2);
Serial.println("V");
Serial.print("Average current: ");
Serial.print(Cave,3);
Serial.println("A");
Serial.println("-----");
delay(3000);
}