Skip to content

Commit

Permalink
Added Signed 16 and 32 bit integer interpretation options.
Browse files Browse the repository at this point in the history
  • Loading branch information
SciFiDryer committed Jul 20, 2023
1 parent 9450123 commit e345d76
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/modbusmechanic/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ PacketFrame.jLabel8.text=Data value type
PacketFrame.jLabel14.text=Serial settings
PacketFrame.jLabel7.text=Quantity
PacketFrame.jMenuItem6.text=Register Scanner...
PacketFrame.s16ReadButton.text=Signed Int16
PacketFrame.s32ReadButton.text=Signed Int32
16 changes: 16 additions & 0 deletions src/modbusmechanic/ModbusMechanic.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class ModbusMechanic {
public static int RESPONSE_TYPE_UINT32 = 4;
public static int RESPONSE_TYPE_RAW = 5;
public static int RESPONSE_TYPE_BOOLEAN = 6;
public static int RESPONSE_TYPE_SINT16 = 7;
public static int RESPONSE_TYPE_SINT32 = 8;
public static int READ_HOLDING_REGISTER_CODE = 3;
public static int READ_COILS_CODE = 1;
public static int READ_DI_CODE = 2;
Expand Down Expand Up @@ -736,4 +738,18 @@ public static long bytesToInt32(byte[] buf)
int[] regs = DataUtils.BeToIntArray(buf);
return ((long)regs[0]*65536) + (long)regs[1];
}
public static int bytesToShort(byte[] buf)
{
ByteBuffer bb = ByteBuffer.allocate(2);
bb.put(buf);
bb.rewind();
return bb.getShort();
}
public static int bytesToSint32(byte[] buf)
{
ByteBuffer bb = ByteBuffer.allocate(4);
bb.put(buf);
bb.rewind();
return bb.getInt();
}
}
32 changes: 32 additions & 0 deletions src/modbusmechanic/PacketFrame.form
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,38 @@
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="messagePanel3">

<Layout class="org.netbeans.modules.form.compat2.layouts.DesignFlowLayout"/>
<SubComponents>
<Component class="javax.swing.JRadioButton" name="s16ReadButton">
<Properties>
<Property name="buttonGroup" type="javax.swing.ButtonGroup" editor="org.netbeans.modules.form.RADComponent$ButtonGroupPropertyEditor">
<ComponentRef name="buttonGroup1"/>
</Property>
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
<ResourceString bundle="modbusmechanic/Bundle.properties" key="PacketFrame.s16ReadButton.text" replaceFormat="java.util.ResourceBundle.getBundle(&quot;{bundleNameSlashes}&quot;).getString(&quot;{key}&quot;)"/>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="s16ReadButtonActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JRadioButton" name="s32ReadButton">
<Properties>
<Property name="buttonGroup" type="javax.swing.ButtonGroup" editor="org.netbeans.modules.form.RADComponent$ButtonGroupPropertyEditor">
<ComponentRef name="buttonGroup1"/>
</Property>
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
<ResourceString bundle="modbusmechanic/Bundle.properties" key="PacketFrame.s32ReadButton.text" replaceFormat="java.util.ResourceBundle.getBundle(&quot;{bundleNameSlashes}&quot;).getString(&quot;{key}&quot;)"/>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="s32ReadButtonActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="messagePanel2">

<Layout class="org.netbeans.modules.form.compat2.layouts.DesignFlowLayout"/>
Expand Down
81 changes: 81 additions & 0 deletions src/modbusmechanic/PacketFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ private void initComponents() {
asciiReadButton = new javax.swing.JRadioButton();
u16ReadButton = new javax.swing.JRadioButton();
u32ReadButton = new javax.swing.JRadioButton();
messagePanel3 = new javax.swing.JPanel();
s16ReadButton = new javax.swing.JRadioButton();
s32ReadButton = new javax.swing.JRadioButton();
messagePanel2 = new javax.swing.JPanel();
transmitPacketButton = new javax.swing.JButton();
jSeparator4 = new javax.swing.JSeparator();
Expand Down Expand Up @@ -419,6 +422,26 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {

getContentPane().add(messagePanel);

buttonGroup1.add(s16ReadButton);
s16ReadButton.setText(bundle.getString("PacketFrame.s16ReadButton.text")); // NOI18N
s16ReadButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s16ReadButtonActionPerformed(evt);
}
});
messagePanel3.add(s16ReadButton);

buttonGroup1.add(s32ReadButton);
s32ReadButton.setText(bundle.getString("PacketFrame.s32ReadButton.text")); // NOI18N
s32ReadButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
s32ReadButtonActionPerformed(evt);
}
});
messagePanel3.add(s32ReadButton);

getContentPane().add(messagePanel3);

transmitPacketButton.setText(bundle.getString("PacketFrame.transmitPacketButton.text")); // NOI18N
transmitPacketButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Expand Down Expand Up @@ -747,6 +770,18 @@ private void transmitPacket() throws NumberFormatException
values = java.nio.ByteBuffer.allocate(8).putLong(intValue).array();
values = java.util.Arrays.copyOfRange(values, 4, 8);
}
if (s16ReadButton.isSelected())
{
lastField = "Value";
short shortValue = Short.parseShort(valueField.getText());
values = java.nio.ByteBuffer.allocate(2).putShort(shortValue).array();
}
if (s32ReadButton.isSelected())
{
lastField = "Value";
int intValue = Integer.parseInt(valueField.getText());
values = java.nio.ByteBuffer.allocate(8).putInt(intValue).array();
}
if (asciiReadButton.isSelected())
{
values = valueField.getText().getBytes();
Expand Down Expand Up @@ -812,6 +847,14 @@ private void transmitPacket() throws NumberFormatException
{
lastResponseType = ModbusMechanic.RESPONSE_TYPE_UINT32;
}
if (s16ReadButton.isSelected())
{
lastResponseType = ModbusMechanic.RESPONSE_TYPE_SINT16;
}
if (s32ReadButton.isSelected())
{
lastResponseType = ModbusMechanic.RESPONSE_TYPE_SINT32;
}
if (customMessageButton.isSelected())
{
lastResponseType = ModbusMechanic.RESPONSE_TYPE_RAW;
Expand Down Expand Up @@ -943,6 +986,14 @@ public void displayResponse()
{
valueLabel.setText("Response value: " + ModbusMechanic.bytesToInt32(result));
}
if (lastResponseType == ModbusMechanic.RESPONSE_TYPE_SINT16)
{
valueLabel.setText("Response value: " + ModbusMechanic.bytesToShort(result));
}
if (lastResponseType == ModbusMechanic.RESPONSE_TYPE_SINT32)
{
valueLabel.setText("Response value: " + ModbusMechanic.bytesToSint32(result));
}
if (lastResponseType == ModbusMechanic.RESPONSE_TYPE_BOOLEAN)
{
String boolValue = "false";
Expand Down Expand Up @@ -1393,6 +1444,8 @@ public void fireSelectionEvent()
readFloatButton.setEnabled(false);
u16ReadButton.setEnabled(false);
u32ReadButton.setEnabled(false);
s16ReadButton.setEnabled(false);
s32ReadButton.setEnabled(false);
asciiReadButton.setEnabled(false);
transactionField.setEnabled(false);
protoIdField.setEnabled(false);
Expand Down Expand Up @@ -1427,6 +1480,7 @@ public void fireSelectionEvent()
readFloatButton.setEnabled(false);
asciiReadButton.setEnabled(false);
u32ReadButton.setEnabled(false);
s32ReadButton.setEnabled(false);
quantityField.setText("1");
quantityField.setEnabled(false);
if (u16ReadButton.isSelected())
Expand All @@ -1441,6 +1495,8 @@ public void fireSelectionEvent()
readFloatButton.setEnabled(true);
u16ReadButton.setEnabled(true);
u32ReadButton.setEnabled(true);
s16ReadButton.setEnabled(true);
s32ReadButton.setEnabled(true);
asciiReadButton.setEnabled(true);
if (customMessageButton.isSelected())
{
Expand Down Expand Up @@ -1485,6 +1541,20 @@ public void fireSelectionEvent()
quantityField.setEnabled(false);
quantityField.setText("2");
}
if (s16ReadButton.isSelected())
{
protoIdField.setEnabled(false);
transactionField.setEnabled(false);
quantityField.setEnabled(false);
quantityField.setText("1");
}
if (s32ReadButton.isSelected())
{
protoIdField.setEnabled(false);
transactionField.setEnabled(false);
quantityField.setEnabled(false);
quantityField.setText("2");
}
wordSwapCheckbox.setEnabled(true);
byteSwapCheckbox.setEnabled(true);
}
Expand Down Expand Up @@ -1687,6 +1757,14 @@ private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI
(new RegisterScannerFrame()).setVisible(true);
}//GEN-LAST:event_jMenuItem6ActionPerformed

private void s16ReadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_s16ReadButtonActionPerformed
fireSelectionEvent();
}//GEN-LAST:event_s16ReadButtonActionPerformed

private void s32ReadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_s32ReadButtonActionPerformed
fireSelectionEvent();
}//GEN-LAST:event_s32ReadButtonActionPerformed

private DefaultComboBoxModel getPortNames()
{
String[] portNames = ModbusMechanic.getPortNames();
Expand Down Expand Up @@ -1788,6 +1866,7 @@ public static void main(String args[]) {
private javax.swing.JSeparator jSeparator6;
private javax.swing.JPanel messagePanel;
private javax.swing.JPanel messagePanel2;
private javax.swing.JPanel messagePanel3;
private javax.swing.JPanel modbusPanel;
private javax.swing.JPanel modbusPanel2;
private javax.swing.JRadioButton offButton;
Expand All @@ -1803,6 +1882,8 @@ public static void main(String args[]) {
private javax.swing.JPanel responsePanel;
private javax.swing.JRadioButton rtuMsgButton;
private javax.swing.JMenuItem rtuSerialMonitorItem;
private javax.swing.JRadioButton s16ReadButton;
private javax.swing.JRadioButton s32ReadButton;
private javax.swing.JPanel serialPanel;
private javax.swing.JTextField slaveNodeField;
private javax.swing.JMenuItem startModbusBridge;
Expand Down

0 comments on commit e345d76

Please sign in to comment.