Escolar Documentos
Profissional Documentos
Cultura Documentos
ShortBus is a dll that provides master functionality (Modbus RTU and TCP/IP) and slave functionality (Modbus RTU)
to any Visual Studio .NET application.
Master Instance Functions
SetCOMPort - sets serial communication port settings
ErrorString - converts integer error code into a string
ReadBits - read bits using function codes 0 or 1
WriteBitSingle - write single bit using function code 5
WriteBitMultiple - write bits using function code 15
ReadWords - read words using function codes 3 or 4
WriteWordSingle - write single word using function code 6
WriteWordMultiple - write words using function code 16
BeginLog - start logging activity to buffer
SaveLog - save log to file LogName
Logging - indicates logging is active
Master Instance Variables
TCPMode - boolean, enables Modbus TCP/IP communication
TCPAddress - string, sets the Modbus TCP/IP IP address or URL
TCPPort - unsigned short integer, sets the Modbus TCP/IP port (default 502)
TCPTimeout - unsigned short integer, sets the Modbus TCP/IP timeout in milliseconds (default 500)
COM - serial port used for this instance
LogName - string, file name for log file, default is "SBMasterLog.txt"
LogAppend - boolean, option to append existing log file, default is false
Examples
Visual C#
Visual Basic
Visual Basic
Visual Basic
Visual Basic
ErrorString
Visual C# (ushort code), Visual Basic (code As UShort)
Description
Converts the unsigned short error code into a string.
0 = No Error
1 = No Response or Connection Error
2 = Exception Error - Generic
5 = Start Address 0 Invalid (Offset +1 Required)
6 = Node Address Must Be Less Than 248
7 = Node Address Cannot Be Less Than 1
11 = Exception Error - Illegal Function
12 = Exception Error - Illegal Data Address
13 = Exception Error - Illegal Data Value
14 = Exception Error - Slave Device Failure
15 = Exception Error - Slave Acknowledge
16 = Exception Error - Slave Device Busy
17 = Exception Error - Negative Acknowledge
18 = Exception Error - Memory Parity Error
99 = Broadcast Message
Parameters
code - unsigned short returned from read or write function
Examples
Visual C#
Visual Basic
ReadBits
Visual C# (ushort node, ushort start, ushort length, out bool[ ] bits, [ushort delay = 100], [bool type1x = false]
, [bool orderswap = false])
Visual Basic (node As UShort, start As UShort, length As UShort, ByRef bits() As Boolean, [delay As UShort = 100]
, [type1x As Boolean = False, [orderswap As Boolean = False])
Description
Reads bits using function codes 0 (0x registers) or 1 (1x registers).
Return
The function returns an error code as an unsigned short but it may be invoked without reading. If the
error code is 0, that can be used to determine the validity of the data.
Parameters
node - device to read data from (if not acknowledged with Modbus TCP/IP set as 1)
start - address in node to begin reading from, must be 1 or greater (1 = 00001 or 1 = 10001)
length - how many bits to read
bits - output parameter, boolean array to store results into
delay (optional) - specifies the ms interval between characters, 100ms if not specified
type1x (optional) - specifies that function code 1 is to be used instead of 0, false if not specified
orderswap (optional) - specifies that the bit order is to be swapped, false if not specified
Examples
Visual C#
ushort err;
bool[] read = new bool[10];
bool[] data = new bool[10];
err = sbMst.ReadBits(1, 1, 10, out read, delay: 250, type1x: false, orderswap: false);
if (err == 0) for (int i = 0; i <= 9; i++) data[i] = read[i];
else for (int i = 0; i <= 9; i++) data[i] = false;
Visual Basic
WriteBitSingle
Visual C# (ushort node, ushort start, bool data, [ushort delay = 100])
Visual Basic (node As UShort, start As UShort, data As Boolean, [delay As UShort = 100])
Description
Writes a bit using function code 5 (0x registers).
Return
The function returns an error code as an unsigned short but it may be invoked without reading. If the
error code is 0, that can be used to determine the success of the write.
Parameters
node - device to read data from, node 0 may be used for broadcasts (if not acknowledged with Modbus
TCP/IP set as 1)
start - address in node to begin reading from, must be 1 or greater (1 = 00001)
data - boolean value to be written
delay (optional) - specifies the ms interval between characters, 100ms if not specified
Examples
Visual C#
bool data = true;
sbMst.WriteBitSingle(1, 1, data, delay: 250);
Visual Basic
WriteBitMultiple
Visual C# (ushort node, ushort start, ushort length, bool[ ] data, [ushort delay = 100], [bool orderswap = false])
Visual Basic (node As UShort, start As UShort, length As UShort, data() As Boolean, [delay As UShort = 100]
, [orderswap As Boolean= False])
Description
Writes bits using function code 15 (0x registers). A length of one or greater must be specified.
Return
The function returns an error code as an unsigned short but it may be invoked without reading. If the
error code is 0, that can be used to determine the success of the write.
Parameters
node - device to read data from, node 0 may be used for broadcasts (if not acknowledged with Modbus
TCP/IP set as 1)
start - address in node to begin reading from, must be 1 or greater (1 = 00001)
length - how many bits to write
data - boolean values to be written
delay (optional) - specifies the ms interval between characters, 100ms if not specified
orderswap (optional) - specifies that the bit order is to be swapped, false if not specified
Examples
Visual C#
bool[] data = new bool[3] {true, false, true};
sbMst.WriteBitMultiple(1, 1, 3, data, delay: 250);
Visual Basic
ReadWords
Visual C# (ushort node, ushort start, ushort length, out ushort[ ] words, [ushort delay = 100]
, [bool type3x = false], [bool byteswap = false])
Visual Basic (node As UShort, start As UShort, length As UShort, ByRef words()As UShort, [delay As UShort = 100]
, [type3x As Boolean = False], [byteswap As Boolean = False])
Description
Reads words using function codes 3 (4x registers) or 4 (3x registers).
Return
The function returns an error code as an unsigned short but it may be invoked without reading. If the
error code is 0, that can be used to determine the validity of the data.
Parameters
node - device to read data from (if not acknowledged with Modbus TCP/IP set as 1)
start - address in node to begin reading from, must be 1 or greater (1 = 30001 or 1 = 40001)
length - how many words to read
words - output parameter, unsigned short array to store results into
delay (optional) - specifies the ms interval between characters, 100ms if not specified
type3x (optional) - specifies that function code 4 is to be used instead of 3, false if not specified
byteswap (optional) - specifies that the byte order is to be swapped, false if not specified
Examples
Visual C#
ushort err;
ushort[] read = new ushort[10];
ushort[] data = new ushort[10];
err = sbMst.ReadWords(1, 1, 10, out read, delay: 250, type3x: false, byteswap: false);
if (err == 0) for (int i = 0; i <= 9; i++) data[i] = read[i];
else for (int i = 0; i <= 9; i++) data[i] = 0;
Visual Basic
WriteWordSingle
Visual C# (ushort node, ushort start, ushort data, [ushort delay = 100], [bool byteswap = false])
Visual Basic (node As UShort, start As UShort, data As UShort, [delay As UShort = 100]
, [byteswap As Boolean = False])
Description
Writes a word using function code 6 (4x registers).
Return
The function returns an error code as an unsigned short but it may be invoked without reading. If the
error code is 0, that can be used to determine the success of the write.
Parameters
node - device to read data from, node 0 may be used for broadcasts (if not acknowledged with Modbus
TCP/IP set as 1)
start - address in node to begin reading from, must be 1 or greater (1 = 40001)
data - unsigned short value to be written
delay (optional) - specifies the ms interval between characters, 100ms if not specified
byteswap (optional) - specifies that the byte order is to be swapped, false if not specified
Examples
Visual C#
ushort data = 99;
sbMst.WriteWordSingle(1, 1, data, delay: 250);
Visual Basic
WriteWordMultiple
Visual C# (ushort node, ushort start, ushort length, out ushort[ ] data, [ushort delay = 100]
, [bool byteswap = false])
Visual Basic (node As UShort, start As UShort, length As UShort, ByRef data() As UShort, [delay As UShort = 100]
, [byteswap As Boolean = False])
Description
Writes words using function code 16 (4x registers). A length of one or greater must be specified.
Return
The function returns an error code as an unsigned short but it may be invoked without reading. If the
error code is 0, that can be used to determine the success of the write.
Parameters
node - device to read data from, node 0 may be used for broadcasts (if not acknowledged with Modbus
TCP/IP set as 1)
start - address in node to begin reading from, must be 1 or greater (1 = 00001)
length - how many bits to write
data - unsigned short values to be written
delay (optional) - specifies the ms interval between characters, 100ms if not specified
byteswap (optional) - specifies that the byte order is to be swapped, false if not specified
Examples
Visual C#
ushort[] data = new ushort[3] {1, 2, 3};
sbMst.WriteWordMultiple(1, 1, 3, data, delay: 250);
Visual Basic
BeginLog
Visual C# ()
Visual Basic ()
Description
Begin logging activity to buffer.
Return
No return.
Parameters
No parameters.
Examples
Visual C#
sbMst.BeginLog();
Visual Basic
sbMst.BeginLog()
SaveLog
Visual C# ()
Visual Basic ()
Description
Save log to file LogName.
Return
No return.
Parameters
No parameters.
Examples
Visual C#
sbMst.SaveLog();
Visual Basic
sbMst.SaveLog()
Logging
Visual C# (no parameters)
Visual Basic (no parameters)
Description
Indicates logging is active.
Return
The function returns a boolean.
Examples
Visual C#
loggingActive = sbMst.Logging();
Visual Basic
loggingActive = sbMst.Logging()
Visual Basic
Open
Visual C# (no parameters)
Visual Basic (no parameters)
Description
Function used to open the serial port and monitor incoming communication
Examples
Visual C#
sbSlave.Open();
Visual Basic
sbSlave.Open()
Close
Visual C# (no parameters)
Visual Basic (no parameters)
Description
Function used to close the serial port
Examples
Visual C#
sbSlave.Close();
Visual Basic
sbSlave.Close()
BeginLog
Visual C# (no parameters)
Visual Basic (no parameters)
Description
Begin logging activity to buffer.
Examples
Visual C#
sbSlave.BeginLog();
Visual Basic
sbSlave.BeginLog()
SaveLog
Visual C# (no parameters)
Visual Basic (no parameters)
Description
Save log to file LogName.
Examples
Visual C#
sbSlave.SaveLog();
Visual Basic
sbSlave.SaveLog()
Logging
Visual C# (no parameters)
Visual Basic (no parameters)
Description
Indicates logging is active.
Return
The function returns a boolean.
Examples
Visual C#
loggingActive = sbSlave.Logging();
Visual Basic
loggingActive = sbSlave.Logging()
Activity
Visual C# (no parameters)
Visual Basic (no parameters)
Description
Indicates activity on slave.
Return
The function returns a boolean.
Examples
Visual C#
slaveActivity = sbSlave.Activity();
Visual Basic
slaveActivity = sbSlave.Activity()
Visual Basic
WriteReal
Visual C# (ushort length, ushort offset, float[ ] data, [bool byteswap = false], [bool wordswap = false])
Visual Basic (length As UShort, offset As UShort, ByRef data() As Single, [byteswap As Boolean = False]
, [wordswap As Boolean = False])
Description
Converts a float/single array into an unsigned short array. Each 32-bit float/single value is split into two
consecutive 16-bit unsigned short values.
Return
The function returns an unsigned short array of double the length specified.
Parameters
length - how many floats to create, should equal the length of the data array
offset - which array position to begin converting from
data - float/single array to convert to an unsigned short array
byteswap (optional) - specifies that the byte order is to be swapped, false if not specified
wordswap (optional) - specifies that the word order is to be swapped, false if not specified
Examples
Visual C#
float[] data = new float[1] {987.0F};
sbMst.WriteWordMultiple(1, 1, 2, sbMst.WriteReal(1, 0, data), delay: 250);
Visual Basic
ReadDInt
Visual C# (ushort length, ushort offset, ushort[ ] data, [bool byteswap = false], [bool wordswap = false])
Visual Basic (length As UShort, offset As UShort, ByRef data() As UShort, [byteswap As Boolean = False]
, [wordswap As Boolean = False])
Description
Converts an unsigned short array into an unsigned integer array. Each 32-bit unsigned integer value is the
result of merging two consecutive 16-bit unsigned short values.
Return
The function returns an unsigned integer of the length specified.
Parameters
length - how many unsigned integers to convert, should be half of the length of the data array
offset - which array position to begin converting from
data - unsigned short array to convert to an unsigned integer array
byteswap (optional) - specifies that the byte order is to be swapped, false if not specified
wordswap (optional) - specifies that the word order is to be swapped, false if not specified
Examples
Visual C#
ushort[] data = new ushort[2];
sbMst.ReadWords(1, 1, 2, out data, delay: 250);
uint[] idata = sbMst.ReadDInt(1, 0, data);
Visual Basic
WriteDInt
Visual C# (ushort length, ushort offset, uint[ ] data, [bool byteswap = false], [bool wordswap = false])
Visual Basic (length As UShort, offset As UShort, ByRef data() As UInteger, [byteswap As Boolean = False]
, [wordswap As Boolean = False])
Description
Converts an unsigned integer array into an unsigned short array. Each 32-bit unsigned integer value is
split into two consecutive 16-bit unsigned short values.
Return
The function returns an unsigned short array of double the length specified.
Parameters
length - how many unsigned integers to create, should equal the length of the data array
offset - which array position to begin converting from
data - unsigned integer array to convert to an unsigned short array
byteswap (optional) - specifies that the byte order is to be swapped, false if not specified
wordswap (optional) - specifies that the word order is to be swapped, false if not specified
Examples
Visual C#
uint[] data = new uint[1] {99999};
sbMst.WriteWordMultiple(1, 1, 2, sbMst.WriteDInt(1, 0, data), delay: 250);
Visual Basic
BitPack
Visual C# (ushort offset, bool[ ] data, [ushort length = 16], [bool byteswap = false], [bool orderswap = false])
Visual Basic (offset As UShort, ByRef data() As Boolean, [length As UShort = 16], [byteswap As Boolean = False]
, [orderswap As Boolean = False])
Description
Converts a boolean array of up to 16 values into a single unsigned short. Each array position n (from 0 to
n+1
15) represents 2 and all 16 are summed to create a single unsigned short.
Return
The function returns an unsigned short.
Parameters
offset - which array position to begin converting from
data - boolean array to convert to an unsigned short
length (optional) - number of boolean values in array to convert (16 maximum), 16 if not specified
byteswap (optional) - specifies that the byte order is to be swapped, false if not specified
orderswap (optional) - specifies that the bit order is to be swapped, false if not specified
Examples
Visual C#
bool[] data = new bool[16];
data[0] = true;
data[7] = true;
sbMst.WriteWordSingle(1, 1, sbMst.BitPack(0, data, length: 16), delay: 250);
Visual Basic
BitUnpack
Visual C# (ushort data, [bool byteswap = false], [bool orderswap = false])
Visual Basic (data As UShort, [byteswap As Boolean = False], [orderswap As Boolean = False])
Description
Converts an unsigned short into a boolean array of 16 values. Each array position n (from 0 to 15) is
derived from the binary representation of the unsigned short.
Return
The function returns a boolean array.
Parameters
data - unsigned short to convert to a boolean array
byteswap (optional) - specifies that the byte order is to be swapped, false if not specified
orderswap (optional) - specifies that the bit order is to be swapped, false if not specified
Examples
Visual C#
ushort[] data = new ushort[1];
bool[] bdata = new bool[16];
sbMst.ReadWords(1, 1, 1, out data, delay: 250);
bdata = sbMst.BitUnpack(data[0]);
Visual Basic
IntToBool
Visual C# (ushort length, ushort[ ] data)
Visual Basic (length As UShort, ByRef data() As UShort)
Description
Converts an unsigned short array into a boolean array. For each value, 0 = false and any value greater
than 0 is true.
Return
The function returns a boolean array of the specified length.
Parameters
length - number of unsigned short values to convert
data - unsigned short array to convert to a boolean array
Examples
Visual C#
ushort[] udata = new ushort[16];
udata[0] = 1;
udata[7] = 1;
sbMst.WriteBitMultiple(1, 1, 16, sbMst.IntToBool(16, udata));
Visual Basic
BoolToInt
Visual C# (ushort length, bool[ ] data)
Visual Basic (length As UShort, ByRef data() As Boolean)
Description
Converts a boolean array into an unsigned short array. For each value, false = 0 and true = 1.
Return
The function returns an unsigned short array of the specified length.
Parameters
length - number of boolean values to convert
data - boolean array to convert to an unsigned short array
Examples
Visual C#
bool[] data = new bool[16];
ushort[] idata = new ushort[16];
sbMst.ReadBits(1, 1, 16, out data, delay: 250);
idata = sbMst.BoolToInt(16, data);
Visual Basic
'N', 1);
'N', 1);
//optional
//optional
Visual Basic
namespace MB_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Variables.sb1.TCPMode = true;
Variables.sb2.TCPMode = true;
Variables.sb1.TCPAddress = "192.168.1.60";
Variables.sb2.TCPAddress = "192.168.1.61";
Variables.sb1.TCPPort = 502;
Variables.sb2.TCPPort = 502;
Variables.sb1.LogName = "sb1Log.txt"; //optional
Variables.sb2.LogName = "sb2Log.txt"; //optional
Variables.sb1.BeginLog(); //optional
Variables.sb2.BeginLog(); //optional
}
public class Variables
{
public static ShortBus.Master sb1 = new ShortBus.Master();
public static ShortBus.Master sb2 = new ShortBus.Master();
public static ushort[] data1 = new ushort[10];
public static ushort[] data2 = new ushort[10];
}
Visual Basic
namespace MB_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Variables.sb1.SetPort(1, 19200, 8, 'N', 1);
Variables.sb2.SetPort(3, 19200, 8, 'N', 1);
Variables.sb1.Open();
Variables.sb2.Open();
Variables.sb1.Enable3x = false;
Variables.sb2.Enable3x = false;
Variables.sb1.LogName = "sb1Log.txt";
Variables.sb2.LogName = "sb2Log.txt";
Variables.sb1.BeginLog();
Variables.sb2.BeginLog();
}
public class Variables
{
public static ShortBus.Slave sb1 = new ShortBus.Slave();
public static ShortBus.Slave sb2 = new ShortBus.Slave();
public static ushort[] prgData1 = new ushort[10];
public static ushort[] prgData2 = new ushort[10];
}
//periodically populate data array with program variables and save log
private void timer1_Tick(object sender, EventArgs e)
{
for (ushort a = 0; a < 10; ++a)
{
Variables.sb1.Data4x[a] = Variables.prgData1[a];
Variables.sb2.Data4x[a] = Variables.prgData2[a];
Variables.sb1.SaveLog();
Variables.sb2.SaveLog();
}
}
Visual Basic