Você está na página 1de 23

ShortBus Quick Start

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

Slave Instance Functions


SetPort - sets communication port settings
Open - opens the serial port, monitors incoming communication
Close - closes the serial port
BeginLog - start logging activity to buffer
SaveLog - save log to file LogName
Logging - indicates logging is active
Activity - indicates activity on slave
Slave Instance Variables
COM - serial port used for this instance
Address - unsigned short integer, node address used for this instance default = 1
Enable - boolean, if true enables slave interface, if false exception code returned default = true
Data0x - boolean array for 0x registers (FC1), length of 65535 default = all 0
Data1x - boolean array for 1x registers (FC2), length of 65535 default = all 0
Data3x - unsigned short integer array for 3x registers (FC4), length of 65535 default = all 0
Data4x - unsigned short integer array for 4x registers (FC3), length of 65535 default = all 0
ReadOnly0x - boolean array for write permissions of 0x register indices, length of 65535 default = all false
ReadOnly4x - boolean array for write permissions of 4x register indices, length of 65535 default = all false
Enable0x - boolean, if true 0x registers exist and may be read default = true
Linked1x - boolean, if true 1x data array is linked to 0x data array default = true
Enable1x - boolean, if true 1x registers exist and may be read default = true
Linked3x - boolean, if true 3x data array is linked to 4x data array default = true
Enable3x - boolean, if true 3x registers exist and may be read default = true
Enable4x - boolean, if true 4x registers exist and may be read, see ReadOnly4x for write permission default = true
EnableFC5 - boolean, if true FC5 may be used to write 0x registers default = true
EnableFC15 - boolean, if true FC15 may be used to write 0x registers default = true
EnableFC6 - boolean, if true FC6 may be used to write 4x registers default = true
EnableFC16 - boolean, if true FC16 may be used to write 4x registers default = true
MsgGood - unsigned long integer, total number of messages received without error
MsgBadCRC - unsigned long integer, total number of messages received with bad checksum
MsgExc - unsigned long integer, total number of messages received with exception responses
MsgTotal - unsigned long integer, total number of messages received
LogName - string, file name for log file default = "SBSlaveLog.txt"
LogAppend - boolean, option to append existing log file default = false
Static Conversion Functions (Usable For Master or Slave)
ReadReal - converts an unsigned short array into a float/single array
WriteReal - converts a float/single array into an unsigned short array
ReadDInt - converts an unsigned short array into an integer array (16-bit to 32-bit)
WriteDInt - converts an integer array into an unsigned short array (32-bit to 16-bit)
BitPack - converts a 16-count boolean array into an unsigned short
BitUnpack - converts an unsigned short into a 16-count boolean array
IntToBool - converts an unsigned short array into a boolean array
BoolToInt - converts a boolean array into an unsigned short array

Setting Up New Master Instance

Examples
Visual C#

ShortBus.Master sbMst = new ShortBus.Master();

Visual Basic

Dim sbMst As New ShortBus.Master()

Setting Up New Master Instance Using TCP/IP


Examples
Visual C#

ShortBus.Master sbMst = new ShortBus.Master();


sbMst.TCPMode = true;
sbMst.TCPAddress = "localhost";
sbMst.TCPPort = 502;

Visual Basic

Dim sbMst As New ShortBus.Master()


sbMst.TCPMode = True
sbMst.TCPAddress = "localhost"
sbMst.TCPPort = 502

Setting Up New Slave Instance


Examples
Visual C#

ShortBus.Slave sbSlave = new ShortBus.Slave();

Visual Basic

Dim sbSlave As New ShortBus.Slave()

Master Instance Function Descriptions


SetCOMPort
Visual C# (ushort port, ushort baud, ushort data, char parity, double stop)
Visual Basic (port As UShort, baud As UShort, data As UShort, parity As Char, stop As Double)
Description
Function used to define the COM variable for the instance
Parameters
port - communication port, normally 1-32
baud - baud rate, normally 9600 or 19200
data - data bits, normally 8
parity - parity, must be 'O', 'E' or 'N' (Visual C#) or "O", "E" or "N" (Visual Basic)
stop - stop bits, must be 0, 1, 1.5 or 2
Examples
Visual C#
sbMst.SetCOMPort(3, 19200, 8, 'N', 1);

Visual Basic

sbMst.SetCOMPort(3, 19200, 8, "N", 1)

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#

Variables.status = sbMst.ReadWords(1, 1, 10, out Variables.words, delay: 250);


Result.Text = sbMst.ErrorString(Variables.status);

Visual Basic

err = sbMst.ReadWords(1, 1, 10, results, delay:=250, type3x:=True)


Result.Text = sbMst.ErrorString(err)

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

Dim err As UShort


Dim read(10) As Boolean
Dim data(10) As Boolean
err = sbMst.ReadBits(1, 1, 10, read, delay:=250, type1x:=False, orderswap:=False)
If err = 0 Then
For i = 0 To 9
data(i) = read(i)
Next i
Else
For i = 0 To 9
data(i) = False
Next i
End If

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

Dim data As Boolean


data = True
sbMst.WriteBitSingle(1, 1, data, delay:=250)

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

Dim data(3) As Boolean


data = {True, False, True}
sbMst.WriteBitMultiple(1, 1, 3, data, delay:=250)

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

Dim err As UShort


Dim read(10) As UShort
Dim data(10) As UShort
err = sbMst.ReadWords(1, 1, 10, read, delay:=250, type3x:=False, byteswap:=False)
If err = 0 Then
For i = 0 To 9
data(i) = read(i)
Next i
Else
For i = 0 To 9
data(i) = 0
Next i
End If

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

Dim data As UShort


data = 99
sbMst.WriteWordSingle(1, 1, data, delay:=250)

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

Dim data(3) As UShort


data = {1, 2, 3}
sbMst.WriteWordMultiple(1, 1, 3, data, delay:=250)

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()

Slave Instance Function Descriptions


SetPort
Visual C# (ushort port, ushort baud, ushort data, char parity, double stop)
Visual Basic (port As UShort, baud As UShort, data As UShort, parity As Char, stop As Double)
Description
Function used to define the COM variable for the instance
Parameters
port - communication port, normally 1-32
baud - baud rate, normally 9600 or 19200
data - data bits, normally 8
parity - parity, must be 'O', 'E' or 'N' (Visual C#) or "O", "E" or "N" (Visual Basic)
stop - stop bits, must be 0, 1, 1.5 or 2
Examples
Visual C#
sbSlave.SetPort(3, 19200, 8, 'N', 1);

Visual Basic

sbSlave.SetPort(3, 19200, 8, "N", 1)

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()

Static Conversion Function Descriptions


ReadReal
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 a float/single array. Each 32-bit float/single value is the result of
merging two consecutive 16-bit unsigned short values.
Return
The function returns a float/single array of the length specified.
Parameters
length - how many floats/singles 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 a float/single 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);
float[] rdata = sbMst.ReadReal(1, 0, data);

Visual Basic

Dim data(2) As UShort


sbMst.ReadWords(1, 1, 2, data, delay:=250)
Dim rdata() As Single
rdata = sbMst.ReadReal(1, 0, data)

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

Dim data(1) As Single


data(0) = 987.0
sbMst.WriteWordMultiple(1, 1, 2, sbMst.WriteReal(1, 0, data), delay:=250)

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

Dim data(2) As UShort


sbMst.ReadWords(1, 1, 2, data, delay:=250)
Dim idata() As UInteger
idata = sbMst.ReadDInt(1, 0, data)

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

Dim data(1) As UInteger


data(0) = 99999
sbMst.WriteWordMultiple(1, 1, 2, sbMst.WriteDInt(1, 0, data), delay:=250)

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

Dim data(16) As Boolean


data(0) = True
data(7) = True
sbMst.WriteWordSingle(1, 1, sbMst.BitPack(0, data, length:=16), delay:=250)

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

Dim data(1) As UShort


Dim bdata(16) As Boolean
sbMst.ReadWords(1, 1, 1, data, delay:=250)
bdata = sbMst.BitUnpack(data(0))

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

Dim udata(16) As UShort


udata(0) = 1
udata(7) = 1
sbMst.WriteBitMultiple(1, 1, 16, sbMst.IntToBool(16, udata))

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

Dim data(16) As Boolean


Dim idata(16) As UShort
sbMst.ReadBits(1, 1, 16, data, delay:=250)
idata = sbMst.BoolToInt(16, data)

Master Example of Usage (Two Serial Ports and Logging)


Visual C#
namespace MB_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Variables.sb1.SetCOMPort(1, 19200, 8,
Variables.sb2.SetCOMPort(3, 19200, 8,
Variables.sb1.LogName = "sb1Log.txt";
Variables.sb2.LogName = "sb2Log.txt";
Variables.sb1.BeginLog(); //optional
Variables.sb2.BeginLog(); //optional
}

'N', 1);
'N', 1);
//optional
//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];
}

//periodically poll devices, place responses in public static arrays for


//universal use in program
private void timer1_Tick(object sender, EventArgs e)
{
Variables.sb1.ReadWords(1, 1, 10, out Variables.data1, delay: 250);
Variables.sb2.ReadWords(1, 1, 10, out Variables.data2, delay: 250);
Variables.sb1.SaveLog(); //optional
Variables.sb2.SaveLog(); //optional
}

Visual Basic

Public Class Form1


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Variables.sb1.SetCOMPort(1, 19200, 8, "N", 1)
Variables.sb2.SetCOMPort(3, 19200, 8, "N", 1)
Variables.sb1.LogName = "sb1Log.txt" 'optional
Variables.sb2.LogName = "sb2Log.txt" 'optional
Variables.sb1.BeginLog() 'optional
Variables.sb2.BeginLog() 'optional
End Sub
Public Class Variables
Public Shared data1(10) As UShort
Public Shared data2(10) As UShort
Public Shared sb1 As New ShortBus.Master()
Public Shared sb2 As New ShortBus.Master()
End Class
'periodically poll devices, place responses in public shared arrays for
'universal use in program and save log
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Variables.sb1.ReadWords(1, 1, 10, Variables.data1, delay:=250)
Variables.sb2.ReadWords(1, 1, 10, Variables.data2, delay:=250)
Variables.sb1.SaveLog() 'optional
Variables.sb2.SaveLog() 'optional
End Sub
End Class

Master Example of Usage (Two TCP/IP Connections)


Visual C#

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];
}

//periodically poll devices, place responses in public static arrays for


//universal use in program and save log
private void timer1_Tick(object sender, EventArgs e)
{
Variables.sb1.ReadWords(1, 1, 10, out Variables.data1, delay: 250);
Variables.sb2.ReadWords(1, 1, 10, out Variables.data2, delay: 250);
Variables.sb1.SaveLog(); //optional
Variables.sb2.SaveLog(); //optional
}

Visual Basic

Public Class Form1


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
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
End Sub
Public Class Variables
Public Shared data1(10) As UShort
Public Shared data2(10) As UShort
Public Shared sb1 As New ShortBus.Master()
Public Shared sb2 As New ShortBus.Master()
End Class
'periodically poll devices, place responses in public shared arrays for
'universal use in program and save log
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Variables.sb1.ReadWords(1, 1, 10, Variables.data1, delay:=250)
Variables.sb2.ReadWords(1, 1, 10, Variables.data2, delay:=250)
Variables.sb1.SaveLog() 'optional
Variables.sb2.SaveLog() 'optional
End Sub
End Class

Slave Example of Usage (Two Serial Ports and Logging)


Visual C#

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

Public Class Form1


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
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()
End Sub
Public Class Variables
Public Shared prgData1(10) As UShort
Public Shared prgData2(10) As UShort
Public Shared sb1 As New ShortBus.Slave()
Public Shared sb2 As New ShortBus.Slave()
End Class
'periodically populate data array
Private Sub Timer1_Tick(sender As
For a = 0 To 9
Variables.sb1.Data4x(a) =
Variables.sb2.Data4x(a) =
Variables.sb1.SaveLog()
Variables.sb2.SaveLog()
Next
End Sub
End Class

with program variables and save log


Object, e As EventArgs) Handles Timer1.Tick
Variables.prgData1(a)
Variables.prgData2(a)

Master Example of Logging File


shortbus.dll by Carlo Zaskorski - Client/Master Log
started by user: 6/8/2015 15:34:13
OUT(DEC)-> 1 3 0 0 0 10 197 205
OUT(HEX)-> 01 03 00 00 00 0A C5 CD
Node{1} FC{3} Start{0} Qty{10} Timestamp{15:34:14.125}
IN(DEC)-> 1 3 20 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 143 22
IN(HEX)-> 01 03 14 00 01 00 02 00 03 00 04 00 05 00 06 00 07 00 08 00 09 00 0A 8F 16
Node{1} FC{3} Bytes{20} Data{1,2,3,4,5,6,7,8,9,10} Timestamp{15:34:14.382}
OUT(DEC)-> 1 16 0 1 0 1 2 0 222 39 217
OUT(HEX)-> 01 10 00 01 00 01 02 00 DE 27 D9
Node{1} FC{16} Start{1} Qty{1} Bytes{2} Data{222} Timestamp{15:34:18.650}
IN(DEC)-> 1 16 0 1 0 1 80 9
IN(HEX)-> 01 10 00 01 00 01 50 09
Node{1} FC{16} Start{1} Qty{1} Timestamp{15:34:18.900}
ended by user: 6/8/2015 15:34:27

Slave Example of Logging File


shortbus.dll by Carlo Zaskorski - Server/Slave Log
started by user: 6/8/2015 15:36:39
IN(DEC)-> 1 3 0 0 0 10 197 205
IN(HEX)-> 01 03 00 00 00 0A C5 CD
Node{1} FC{3} Start{0} Qty{10} Timestamp{15:36:40.027}
OUT(DEC)-> 1 3 20 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 143 22
OUT(HEX)-> 01 03 14 00 01 00 02 00 03 00 04 00 05 00 06 00 07 00 08 00 09 00 0A 8F 16
Node{1} FC{3} Bytes{20} Data{1,2,3,4,5,6,7,8,9,10} Timestamp{15:36:40.027}
IN(DEC)-> 1 16 0 1 0 1 2 0 222 39 217
IN(HEX)-> 01 10 00 01 00 01 02 00 DE 27 D9
Node{1} FC{16} Start{1} Qty{1} Bytes{2} Data{222} Timestamp{15:36:44.104}
OUT(DEC)-> 1 16 0 1 0 1 80 9
OUT(HEX)-> 01 10 00 01 00 01 50 09
Node{1} FC{16} Start{1} Qty{1} Timestamp{15:36:44.109}
ended by user: 6/8/2015 15:36:47

Você também pode gostar