Skip to content

Commit c23fe5e

Browse files
committed
Validated user input and added error message for RTU Scanner.
1 parent 1375b0b commit c23fe5e

File tree

3 files changed

+135
-114
lines changed

3 files changed

+135
-114
lines changed

nbproject/project.properties

Lines changed: 100 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,100 @@
1-
annotation.processing.enabled=true
2-
annotation.processing.enabled.in.editor=false
3-
annotation.processing.processors.list=
4-
annotation.processing.run.all.processors=true
5-
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
6-
application.title=ModbusMechanic
7-
application.vendor=jamesma
8-
build.classes.dir=${build.dir}/classes
9-
build.classes.excludes=**/*.java,**/*.form
10-
# This directory is removed when the project is cleaned:
11-
build.dir=build
12-
build.generated.dir=${build.dir}/generated
13-
build.generated.sources.dir=${build.dir}/generated-sources
14-
# Only compile against the classpath explicitly listed here:
15-
build.sysclasspath=ignore
16-
build.test.classes.dir=${build.dir}/test/classes
17-
build.test.results.dir=${build.dir}/test/results
18-
# Uncomment to specify the preferred debugger connection transport:
19-
#debug.transport=dt_socket
20-
debug.classpath=\
21-
${run.classpath}
22-
debug.modulepath=\
23-
${run.modulepath}
24-
debug.test.classpath=\
25-
${run.test.classpath}
26-
debug.test.modulepath=\
27-
${run.test.modulepath}
28-
# Files in build.classes.dir which should be excluded from distribution jar
29-
dist.archive.excludes=
30-
# This directory is removed when the project is cleaned:
31-
dist.dir=dist
32-
dist.jar=${dist.dir}/ModbusMechanic.jar
33-
dist.javadoc.dir=${dist.dir}/javadoc
34-
endorsed.classpath=
35-
excludes=
36-
file.reference.etherip.jar=/Users/matthew/Downloads/ModbusMechanic/lib/etherip.jar
37-
file.reference.etherip.jar-1=C:\\Users\\JAMESMA\\Downloads\\etherip-master\\etherip-master\\dist\\etherip.jar
38-
file.reference.jlibmodbus-1.2.9.7.jar=/Users/matthew/Downloads/ModbusMechanic/lib/jlibmodbus-1.2.9.7.jar
39-
file.reference.jlibmodbus-1.2.9.7.jar-1=C:\\Users\\JAMESMA\\Downloads\\jlibmodbus-1.2.9.7.jar
40-
file.reference.jna-5.12.1.jar=/Users/matthew/Downloads/jna-5.12.1.jar
41-
file.reference.purejavacomm.jar=/Users/matthew/Downloads/purejavacomm.jar
42-
file.reference.purejavacomm.jar-1=C:\\Users\\JAMESMA\\Downloads\\jlibmodbus-1.2.9.7\\jlibmodbus-1.2.9.7\\lib\\purejavacomm.jar
43-
includes=**
44-
jar.compress=false
45-
javac.classpath=\
46-
${file.reference.etherip.jar}:\
47-
${file.reference.jlibmodbus-1.2.9.7.jar}:\
48-
${file.reference.jna-5.12.1.jar}:\
49-
${file.reference.jlibmodbus-1.2.9.7.jar-1}:\
50-
${file.reference.purejavacomm.jar-1}:\
51-
${file.reference.etherip.jar-1}:\
52-
${file.reference.purejavacomm.jar}
53-
# Space-separated list of extra javac options
54-
javac.compilerargs=
55-
javac.deprecation=false
56-
javac.external.vm=true
57-
javac.modulepath=
58-
javac.processormodulepath=
59-
javac.processorpath=\
60-
${javac.classpath}
61-
javac.source=1.8
62-
javac.target=1.8
63-
javac.test.classpath=\
64-
${javac.classpath}:\
65-
${build.classes.dir}
66-
javac.test.modulepath=\
67-
${javac.modulepath}
68-
javac.test.processorpath=\
69-
${javac.test.classpath}
70-
javadoc.additionalparam=
71-
javadoc.author=false
72-
javadoc.encoding=${source.encoding}
73-
javadoc.html5=false
74-
javadoc.noindex=false
75-
javadoc.nonavbar=false
76-
javadoc.notree=false
77-
javadoc.private=false
78-
javadoc.splitindex=true
79-
javadoc.use=true
80-
javadoc.version=false
81-
javadoc.windowtitle=
82-
jlink.launcher=false
83-
jlink.launcher.name=ModbusMechanic
84-
main.class=modbusmechanic.ModbusMechanic
85-
manifest.file=manifest.mf
86-
meta.inf.dir=${src.dir}/META-INF
87-
mkdist.disabled=false
88-
platform.active=default_platform
89-
project.license=apache20
90-
run.classpath=\
91-
${javac.classpath}:\
92-
${build.classes.dir}
93-
run.modulepath=\
94-
${javac.modulepath}
95-
run.test.classpath=\
96-
${javac.test.classpath}:\
97-
${build.test.classes.dir}
98-
run.test.modulepath=\
99-
${javac.test.modulepath}
100-
source.encoding=UTF-8
101-
src.dir=src
102-
test.src.dir=test
1+
annotation.processing.enabled=true
2+
annotation.processing.enabled.in.editor=false
3+
annotation.processing.processors.list=
4+
annotation.processing.run.all.processors=true
5+
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
6+
application.title=ModbusMechanic
7+
application.vendor=jamesma
8+
build.classes.dir=${build.dir}/classes
9+
build.classes.excludes=**/*.java,**/*.form
10+
# This directory is removed when the project is cleaned:
11+
build.dir=build
12+
build.generated.dir=${build.dir}/generated
13+
build.generated.sources.dir=${build.dir}/generated-sources
14+
# Only compile against the classpath explicitly listed here:
15+
build.sysclasspath=ignore
16+
build.test.classes.dir=${build.dir}/test/classes
17+
build.test.results.dir=${build.dir}/test/results
18+
# Uncomment to specify the preferred debugger connection transport:
19+
#debug.transport=dt_socket
20+
debug.classpath=\
21+
${run.classpath}
22+
debug.modulepath=\
23+
${run.modulepath}
24+
debug.test.classpath=\
25+
${run.test.classpath}
26+
debug.test.modulepath=\
27+
${run.test.modulepath}
28+
# Files in build.classes.dir which should be excluded from distribution jar
29+
dist.archive.excludes=
30+
# This directory is removed when the project is cleaned:
31+
dist.dir=dist
32+
dist.jar=${dist.dir}/ModbusMechanic.jar
33+
dist.javadoc.dir=${dist.dir}/javadoc
34+
endorsed.classpath=
35+
excludes=
36+
file.reference.etherip.jar=/Users/matthew/Downloads/ModbusMechanic/lib/etherip.jar
37+
file.reference.etherip.jar-1=C:\\Users\\JAMESMA\\Downloads\\etherip-master\\etherip-master\\dist\\etherip.jar
38+
file.reference.jlibmodbus-1.2.9.7.jar=/Users/matthew/Downloads/ModbusMechanic/lib/jlibmodbus-1.2.9.7.jar
39+
file.reference.jlibmodbus-1.2.9.7.jar-1=C:\\Users\\JAMESMA\\Downloads\\jlibmodbus-1.2.9.7.jar
40+
file.reference.jna-5.12.1.jar-1=C:\\Users\\JAMESMA\\Downloads\\ModbusMechanic.v2.2.1\\ModbusMechanic\\lib\\jna-5.12.1.jar
41+
file.reference.purejavacomm.jar=/Users/matthew/Downloads/purejavacomm.jar
42+
file.reference.purejavacomm.jar-1=C:\\Users\\JAMESMA\\Downloads\\ModbusMechanic.v2.2.1\\ModbusMechanic\\lib\\purejavacomm.jar
43+
includes=**
44+
jar.compress=false
45+
javac.classpath=\
46+
${file.reference.etherip.jar}:\
47+
${file.reference.jlibmodbus-1.2.9.7.jar}:\
48+
${file.reference.etherip.jar-1}:\
49+
${file.reference.purejavacomm.jar-1}:\
50+
${file.reference.jna-5.12.1.jar-1}
51+
# Space-separated list of extra javac options
52+
javac.compilerargs=
53+
javac.deprecation=false
54+
javac.external.vm=true
55+
javac.modulepath=
56+
javac.processormodulepath=
57+
javac.processorpath=\
58+
${javac.classpath}
59+
javac.source=1.8
60+
javac.target=1.8
61+
javac.test.classpath=\
62+
${javac.classpath}:\
63+
${build.classes.dir}
64+
javac.test.modulepath=\
65+
${javac.modulepath}
66+
javac.test.processorpath=\
67+
${javac.test.classpath}
68+
javadoc.additionalparam=
69+
javadoc.author=false
70+
javadoc.encoding=${source.encoding}
71+
javadoc.html5=false
72+
javadoc.noindex=false
73+
javadoc.nonavbar=false
74+
javadoc.notree=false
75+
javadoc.private=false
76+
javadoc.splitindex=true
77+
javadoc.use=true
78+
javadoc.version=false
79+
javadoc.windowtitle=
80+
jlink.launcher=false
81+
jlink.launcher.name=ModbusMechanic
82+
main.class=modbusmechanic.ModbusMechanic
83+
manifest.file=manifest.mf
84+
meta.inf.dir=${src.dir}/META-INF
85+
mkdist.disabled=false
86+
platform.active=default_platform
87+
project.license=apache20
88+
run.classpath=\
89+
${javac.classpath}:\
90+
${build.classes.dir}
91+
run.modulepath=\
92+
${javac.modulepath}
93+
run.test.classpath=\
94+
${javac.test.classpath}:\
95+
${build.test.classes.dir}
96+
run.test.modulepath=\
97+
${javac.test.modulepath}
98+
source.encoding=UTF-8
99+
src.dir=src
100+
test.src.dir=test

src/modbusmechanic/PacketFrame.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class PacketFrame extends javax.swing.JFrame {
4040
public ArrayList<String[]> bookmarkList = new ArrayList<String[]>();
4141
BitsFrame bitsFrame = null;
4242
boolean hasTtyPorts = false;
43+
String lastField = "";
4344
public PacketFrame()
4445
{
4546
try
@@ -639,22 +640,33 @@ private void transactionFieldActionPerformed(java.awt.event.ActionEvent evt) {//
639640

640641
private void transmitPacketButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_transmitPacketButtonActionPerformed
641642
//todo no thread dispatched when this action is performed which causes the UI to freeze for a few seconds
643+
try
644+
{
645+
transmitPacket();
646+
}
647+
catch (NumberFormatException e)
648+
{
649+
JOptionPane.showMessageDialog(this, "Format error. " + lastField + " is a required field and must be a number.", "Parse error", JOptionPane.ERROR_MESSAGE);
650+
if (ModbusMechanic.debug)
651+
{
652+
e.printStackTrace();
653+
}
654+
}
655+
}//GEN-LAST:event_transmitPacketButtonActionPerformed
656+
private void transmitPacket() throws NumberFormatException
657+
{
642658
lastResponse = null;
659+
lastField = "Slave node";
643660
int slaveNode = Integer.parseInt(slaveNodeField.getText());
661+
lastField = "Register";
644662
int register = Integer.parseInt(registerField.getText());
645663
int functionCode = 0;
646664
int quantity = 0;
647665
int transactionId = 1;
648666
int protocolId = 0;
649667
int tcpPort = 502;
650-
try
651-
{
652-
tcpPort = Integer.parseInt(portField.getText());
653-
}
654-
catch (Exception e)
655-
{
656-
e.printStackTrace();
657-
}
668+
lastField = "TCP Port";
669+
tcpPort = Integer.parseInt(portField.getText());
658670
boolean writingFlag = false;
659671
boolean coilsFlag = false;
660672
byte[] values = null;
@@ -694,29 +706,34 @@ private void transmitPacketButtonActionPerformed(java.awt.event.ActionEvent evt)
694706
{
695707
if (!rtuMsgButton.isSelected())
696708
{
709+
lastField = "Transaction ID";
697710
transactionId = Integer.parseInt(transactionField.getText());
711+
lastField = "Protocol ID";
698712
protocolId = Integer.parseInt(protoIdField.getText());
699713
}
700-
quantity = Integer.parseInt(quantityField.getText());
701714
}
702715
lastFunctionCode = functionCode;
716+
lastField = "Quantity";
703717
quantity = Integer.parseInt(quantityField.getText());
704718
if (writingFlag && !coilsFlag)
705719
{
706720
if (readFloatButton.isSelected())
707721
{
722+
lastField = "Value";
708723
float floatValue = Float.parseFloat(valueField.getText());
709724
values = java.nio.ByteBuffer.allocate(4).putFloat(floatValue).array();
710725
values = ModbusMechanic.wordSwap(values);
711726
}
712727
if (u16ReadButton.isSelected())
713728
{
729+
lastField = "Value";
714730
int intValue = Integer.parseInt(valueField.getText());
715731
values = java.nio.ByteBuffer.allocate(4).putInt(intValue).array();
716732
values = java.util.Arrays.copyOfRange(values, 2, 4);
717733
}
718734
if (u32ReadButton.isSelected())
719735
{
736+
lastField = "Value";
720737
long intValue = Long.parseLong(valueField.getText());
721738
values = java.nio.ByteBuffer.allocate(8).putLong(intValue).array();
722739
values = java.util.Arrays.copyOfRange(values, 4, 8);
@@ -803,7 +820,7 @@ private void transmitPacketButtonActionPerformed(java.awt.event.ActionEvent evt)
803820
displayResponse();
804821
displayRaw();
805822
}
806-
}//GEN-LAST:event_transmitPacketButtonActionPerformed
823+
}
807824
public String[] getBookmarkNames()
808825
{
809826
bookmarkList.clear();

src/modbusmechanic/RTUScannerThread.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.*;
2323
import purejavacomm.CommPortIdentifier;
2424
import purejavacomm.SerialPort;
25+
import javax.swing.*;
2526

2627
/**
2728
*
@@ -66,15 +67,20 @@ public void run()
6667
port.close();
6768
parentFrame.scanButton.setText("Scan");
6869
parentFrame.repaint();
69-
keepScanning = false;
70-
70+
keepScanning = false;
7171
}
7272
catch (Exception e)
7373
{
74+
if (e instanceof purejavacomm.PortInUseException)
75+
{
76+
JOptionPane.showMessageDialog(null, "Port in use or insufficient permissions. If the port is not in use running as administrator or root can elevate permissions.", "Serial port error", JOptionPane.ERROR_MESSAGE);
77+
}
7478
if (ModbusMechanic.debug)
7579
{
7680
e.printStackTrace();
7781
}
82+
parentFrame.scanButton.setText("Scan");
83+
keepScanning = false;
7884
}
7985
parentFrame.repaint();
8086
}

0 commit comments

Comments
 (0)