AN015A-EN-P (RTU).fm

Transcription

AN015A-EN-P (RTU).fm
Application Notes
Communicate NXIO Modbus RTU with
VB6 and VB.Net2005 Application
Overview
This application note describes how to control OEMax™ NX I/O- Modbus
RTU and implement protocol using Visual Basic v6.0 and Visual Basic .Net
2005 in PC by using examples.
PC
PLC
OEMaxTM NX IO
Modbus/RTU
Windows Operating
System 2000/XP/Vista
VB6 or VB.Net2005
Application
Serial Port
Target Application
If PLC data is monitored and controlled from a remote station through a PC in
FA system, generally self-developed application using HMI software and
Visual Basic/C/C++ will be used. This application note describes how to
implement and communicate Modbus/RTU protocol using Visual Basic 6.0
and Visual Basic .Net 2005 among high-level languages such as Visual Basic,
C, and C++.
Prerequisites
• Knowledge of Serial Communication & Wiring
• Knowledge of Visual Basic 6.0 , Visual Basic.Net 2005
• Knowledge of Win32 API
Publication AN015A-EN-P - 1
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Related Publication
Introduction to NXIO-MNA/232 Installation Manual
Visual Basic 6 User/Reference Manual
Visual Basic.Net 2005 User/Reference Manual
Basic
OS
Specification of Basically, OS is Windows XP and 2000/Vista (32 bit). However, because
Windows API is compatible with NT, Windows NT 4.0 is also available to use.
OS &
Windows NT 4.0 + SP6A or higher
Development
Windows XP + SP1 or higher
Software
Windows2000 + SP4 or higher
Windows Vista (32Bit)
Development Software
In this application note, Visual Basic 6.0 and Visual Basic. Net 2005 are used .
In case of Visual Basic. Net 2005, internal coding type is very different
compared with Visual Basic 6.0.
In case of Visual Basic 6.0, Service Pack 5 (SP5) must be downloaded.
Service Pack 5 for Visual Basic 6.0 can be downloaded by visiting the
following Website:
http://www.microsoft.com/downloads/details.aspx?FamilyID=e41b1d62-f3cb4867-b86a-a2fe4932cf70&DisplayLang=en
Note that .Net Framework 2.0 or higher must be installed in Visual Studio. Net
2005. In addition, if a program is developed through Visual Studio.Net 2005,
.Net Framework 2.0 Redistributable Package will be required.
.Net Framework can be downloaded for free in Microsoft homepage. (Click the
following URL.) (Of course, if Visual Studio.Net 2005 is installed, .Net
Framework will be automatically installed.)
2 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
http://www.microsoft.com/downloads/details.aspx?FamilyID=0856eacb4362-4b0d-8edd-aab15c5e04f5&displaylang=en
Publication AN015A-EN-P - 3
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
About
Modbus
About Modbus
•
Modbus is the international standard communication protocol
introduced by Modicon in 1979, supporting multidrop network based
on master/slave architecture.
•
Connection type: RS-232C/485/TCP-IP
•
Messaged based communication
Field
Description
Device Address
Address of receiver
Function Code
Code defining message type
Data
Data block with additional information
Error Check
Numeric check value to test for communication errors (LRC/CRC)
Basic Terms
Word
Description
Coil
Discrete output value
Discrete input
This type of data can be provided by an I/O system.
Input Register
This type of data can be provided by an I/O system
Holding Register
This type of data can be alterable by an application program.
Modbus ASCII and Modbus/RTU
•
Modbus serial mode
–
Modbus/ASCII: ASCII code format
–
Modbus/RTU: Binary format (RTU=Remote Terminal Unit)
Modbus/ASCII
Characters
Error Check
Modbus/RTU
ASCII 0~9, A~F
Binary 0 ~255
Longitudinal Redundancy Check
Cyclic Redundancy Check
(CRC) method
(LRC)
Frame Start
Characters[:]
Frame End
Characters[CR][LF]
Gaps in Message
1 sec
Start Bit
1
1
Data Bits
7
8
Parity
Even/Odd
None
Even/Odd
None
Stop Bit
1
2
1
2
4 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Modbus Addressing
•
Device address range
Device Address
Modbus Address
Description
1 ~ 10000
Address - 1
Coils (Output)
10001 ~ 20000
Address - 10001
Inputs
40001 ~ 50000
Address - 40001
Holding Registers
•
A register is displayed as an address ranging from 1 to 10000.
•
An address on Modbus message ranges from 0 to 9999. That is, -1
(or -10001 or -40001) must be subtracted to set proper address.
Protocol
Common Modbus Function Code
Code
Description
Code
Description
01
Read Coil Status
16
Preset Multiple Registers
02
Read Input Status
17
Report Slave ID
03
Read Holding Register
20
Read File Record
04
Read Input Register
21
Write File Record
05
Force Single Coil
22
Mask Write Register
06
Preset Single Register
23
Read/Write Multiple Registers
07
Read Exception Status
24
Read FIFO Queue
08
Diagnostics
43
Encapsulated Interface Transport
11
Get Comm. Event Counter
43/13
CANOpen General Reference
Request and Response
12
Get Comm. Event log
43/14
Read Device Identification
15
Force Multiple Coils
Publication AN015A-EN-P - 5
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Function 01: Read Coil Status
Function 02: Read Input Register
6 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Function 03: Read Holding Registers
Function 04: Read Input Registers
Publication AN015A-EN-P - 7
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Function 05: Force Single Coil
Function 06: Preset Single Register
Function 07: Read Exception Status
8 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Function 15: Force Multiple Coils
Function 16: Preset Multiple Registers
Publication AN015A-EN-P - 9
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Function 17: Report Slave ID
10 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
About NXIO- Specification
MNA/232
NXIO-MNA232
Adaptor Type
Slave Mode
Max. Expansion Module
32 slots
Max. Input Size
129 words (252 bytes)
Max. Output Size
129 words (252 bytes)
Max. Length Bus Line
15.00m
Max. Nodes
32 nodes
Baud Rate
1200, 2400, 9600, 19200,
38400,57600, 115200 bps
Protocol
RTU/ASCII
Interface Connector
D-Sub 9 Pin Female
Settable Node Address
1~99 with two rotary switch
System Power
24V DC
Current for I/O Module
1.5A to 5V DC
Field Power
Supply Voltage: 24V DC
Range: 11 to 28.8V DC
Weight
149.97g
Dimension
45mm x 99mm x 70mm
Publication AN015A-EN-P - 11
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
NXIO-MNA/
232 Serial
Cable
Connection
and Setting
Pin Connection
Pin specification of 9-pin D-sub connector in NXIO-MNA/232 is described as
follows:
Dip Switch Setting (Communication Parameter Setting)
Dip switch settings for data bit, parity bit, and stop bit settings and
communication speed of NXIO-MNA/232 are described as follows. For
detailed information, refer to NXIO-MNA/232 manual.
12 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Modbus/RTU Address Setting
NXIO-MNA/232 has rotary switch to set an address.
An address ranges from 0 to 99, and Address 0 is an address reserved for
broadcasting
The example on the left means
2x10 + 7x1 = 27.
Publication AN015A-EN-P - 13
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Address Map
of NXIOMNA/232
This address map describes basic information. Thus, for detailed information,
refer to NXIO-MNA/232 Specification manual.
Input/Output Address Map
Address Range
Access
Type, Size
Description
0x000 ~ 0x7ff
Read
Total 0x800
Input Data Address
0x800 ~ 0xfff
Write
Total 0x800
Output Data Address
0x1100 ~
R/W
Total 0x1e
Adaptor Information Special Register
Adapter Identification Special Register Address Map
Address
Access
Type, Size
Description
0x1000 (4096)
Read
1 word
Vendor ID = 821 (OEMax)
0x1001 (4097)
Read
1 word
Device Type =0x000C (Network Adaptor)
0x1002 (4098)
Read
1 word
Product Code =0x0300 (NXIO-MNA/232)
0x1003 (4099)
Read
1 word
Firmware Version
0x1004 (4100)
Read
2 words
Product Unique Serial Number
0x1005 (4101)
Read
String up to
34 bytes
Product Naming String
0x1006 (4102)
Read
1 word
Sum Check of EEPROM
0x1010 (4112)
Read
2 words
Firmware Release Data
0x1011 (4113)
Read
2 words
Product Manufacturing Inspection Date
0x1012 (4114)
Read
String up to
34 bytes
Vendor name string
0x101E (4126)
Read
7 words
-1 word
-1 word
-1 word
-1 word
-1 word
-2 word
14 - Publication AN015A-EN-P
0x1100 (4352), Rotary Switch Value, Slave
Node ID
0x1000 (4096), Vendor ID
0x1100 (4097), Device Type
0x1100 (4098), Product Code
0x1100 (4099), Firmware Revision
0x1100 (4100), Product Serial Number
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Adaptor Watchdog Time, Other Time Special Register
(0x1020, 4128)
Address
Access
Type, Size
Description
0x1020 (4128)
R/W
1 word
Unsigned 16bit Watchdog Time Value
0x1021 (4129)
Read
1 word
Watchdog Time Remain Value
0x1022 ~ 0x1023
Reserved
0x1024 (4132)
R/W
1 word
Transmission Response delay time.
Unsigned 16 bit
0x1025 (4133)
R/W
1 word
Valid byte-byte time gap in ASCII mode
0x1028 (4134)
Read
2 words
IO Update Time. Main Loop Time
Adaptor Information Special Register (0x1100, 4352)
Address Offset
Access
Type, Size
Description
0x1100 (4352)
R/W
1 word
High byte is two rotary switch value
Low byte is current slave node address
0x1101 (4352)
Read
1 word
High byte is current dip switch value
Low byte is used current setup value
0x1102 (4353)
Read
1 word
Start address of input image word
register=0x0000
0x1103 (4354)
Read
1 word
Start address of output image word
register=0x0800
0x1104 (4355)
Read
1 word
Size of input image word register
0x1105 (4356)
Read
1 word
Size of output image word register
0x1106 (4357)
Read
1 word
Start address of input image bit = 0x0000
0x1107 (4358)
Read
1 word
Start address of output image bit = 0x0800
0x1108 (4359)
Read
1 word
Size of input image bit
0x1109 (4360)
Read
1 word
Size of output image bit
0x110E (4366)
Read
Up to 33
words
Expansion Slot? Product Model Number
including NA.
0x1110 (4368)
Read
1 word
Number of expansion slot
0x1111 (4369)
Read
1 word
Number of Active Slot
0x1112 (4370)
Read
1 word
Number of Inactive Slot
0x1113 (4371)
Read
1 word
Expansion Slot Module ID
0x1114 (4372)*
R/W
1 word
Input process image mode, Default=2
0x1115 (4373)*
R/W
1 word
Output process image mode, Default=0
0x1116 (4374)**
R/W
2 words
Inactive slot list
0x1117 (4375)
R/W
2 word
Live slot list
0x1118 (4376)
R/W
2 word
Alarm slot list
Publication AN015A-EN-P - 15
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Address Offset
Access
Type, Size
Description
0x1119 (4377)
R/W
1 word
High byte is Modbus status
Low byte is NX-Bus status
0x111A (4378)
R/W
1 word
Reserved Adapter Scan Command
0x111B (4379)
R/W
1 word
Reserved IO state machine
0x111C (4380)
R/W
2 words
Reserved Runtime Fault Code
0x111D (4381)
R/W
1 word
Adaptor NxBUS revision
0x111E (4382)
R/W
1 word
Reserved Adapter IO Identification
Vendor Code
*, ** After the system is reset, new value is applied.
** If slot location changes, default value is automatically set.
Expansion Slot Information
Slot #
Address
Slot #
Address
Slot #1
0x2000(8192)~0x201F(8223)
Slot #17
0x2200(8704)~0x221F(8735)
Slot #2
0x2020(8224)~0x203F(8255)
Slot #18
0x2220(8736)~0x223F(8767)
Slot #3
0x2040(8256)~0x205F(8287)
Slot #19
0x2240(8768)~0x225F(8799)
Slot #4
0x2060(8288)~0x207F(8319)
Slot #20
0x2260(8800)~0x227F(8831)
Slot #5
0x2080(8320)~0x209F(8351)
Slot #21
0x2280(8832)~0x229F(8863)
Slot #6
0x20A0(8352)~0x20BF(8383)
Slot #22
0x22A0(8864)~0x22BF(8895)
Slot #7
0x20C0(8384)~0x20DF(8415)
Slot #23
0x22C0(8896)~0x22DF(8927)
Slot #8
0x20E0(8416)~0x20FF(8447)
Slot #24
0x22E0(8928)~0x22FF(8959)
Slot #9
0x2100(8448)~0x211F(8479)
Slot #25
0x2300(8960)~0x231F(8991)
Slot #10
0x2120(8480)~0x213F(8511)
Slot #26
0x2320(8992)~0x233F(9023)
Slot #11
0x2140(8512)~0x215F(8543)
Slot #27
0x2340(9024)~0x235F(9055)
Slot #12
0x2160(8544)~0x217F(8575)
Slot #28
0x2360(9056)~0x237F(9087)
Slot #13
0x2180(8576)~0x219F(8607)
Slot #29
0x2380(9088)~0x239F(9119)
Slot #14
0x21A0(8608)~0x21BF(8639)
Slot #30
0x23A0(9120)~0x23BF(9151)
Slot #15
0x21C0(8640)~0x21DF(8671)
Slot #31
0x23C0(9152)~0x23DF(9183)
Slot #16
0x21E0(8672)~0x21FF(8703)
Slot #32
0x23E0(9184)~0x23FF(9215)
16 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Address Offset
Access
Type, Size
Description
+ 0x00(+0)
Read
1 word
Slot module ID. Refer to Appendix A.1
Product List.
+0x01(+1)
Read
1 word
Expansion Slot IO code. Refer to Table IO
Data Code Format.
+0x02(+2) **
Read
1 word
Input start register address of input image
word this slot
+0x03(+3) **
Read
1 word
Input word’s bit offset of input image word
this slot
+0x04(+4) **
Read
1 word
Output start register address of output image
word this slot
+0x05(+5) **
Read
1 word
Output word’s bit offset of output image
word this slot
+0x06(+6) **
Read
1 word
Input bit start address of input image bit this
slot.
+0x07(+7) **
Read
1 word
Output bit start address of output image bit
this slot
+0x08(+8) **
Read
1 word
Size of input bit this slot
+0x09(+9) **
Read
1 word
Size of output bit this slot
+0x0A(+10)**
Read
n word
Read input data this slot
+0x0B(+11)**
Read/
Write
n word
Read/write output data this slot
+ 0x0C(+12) *
Read/
Write
1 word
Inactive slot, 0x0000:active,
0x0001:inactive
+ 0x0E(+14)
Read
1 word
Module number, if NXIO-XP4N, returns
0x1324
+ 0x0F(+15)
Read
String up
to 74byte
First 1 word is length of valid character
string.
If NXIO-XP4N, returns
"00 21 4E 58 49 4F 2D 58 50 34 4E 2C 4E
58 49 4F 34 20 53 6F 75 72 63 69 6E 67 20
49 6E 20 34 38 56 64 63 00"
Valid character size = 0x0021 =33
characters,
"NXIO-XP4N, NXIO 4 Sourcing In
48VDC"
+ 0x10(+16)
Read
1 word
Size of configuration parameter byte
+ 0x11(+17)**
Read/
Write
n word
Read/write Configuration parameter data, up
to 8 byte. Refer to A.2 ***.
+ 0x12(+18)
Read
1 word
Size of memory byte
+ 0x13(+19)**
Read/
Write
n word
Read/Write memory data. Offset of memory
is fixed with 0
+ 0x14(+20)**
Read/
Write
n word
Read/Write memory data. First 2byte of
write data is memory offset
+ 0x15(+21)
Read
2 words
Product code Refer to Appendix A.1 Product
List.
Publication AN015A-EN-P - 17
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Address Offset
Access
Type, Size
Description
+ 0x16(+22)
Read
2 words
Catalog Number Refer to Appendix A.1
Product List
+ 0x17(+23)
Read
1 word
Firmware Revision
+ 0x18(+24)
Read
1 word
NxBus Revision
+ 0x1A(+26)
Read/
Write
n word
Reserved Read/write expansion class access
+ 0x1B(+27)
Read/
Write
n word
Reserved Read/write maintenance data
access
* After the system is reset, the new “Set Value” action is applied.
** Nothing of output, input, memory or configuration parameter corresponding slot returns
Exception 02.
*** Slot configuration parameter saved by internal EEPROM during power cycle until slot
position changed.
*** All of output modules and special modules have the slot configuration parameter data. Refer
to A.2.
18 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Communication
through Visual
Basic v6.0
MSComm Control (ActiveX) Setting
To use ActiveX, which is control for
RS232C communication in Visual
Basic, Microsoft Comm Control 6.0
items must be selected as shown in
the left window.
If this control is selected, telephoneshaped control will be displayed in
the left of the Visual Basic 6.0
window.
Then, basic external environment to
communicate with NXIO-MNA/232
is completely set.
Microsoft Comm Control 6.0 Setting
The figure on the left displays the attribute
window when Microsoft Comm Control 6.0 is
positioned on the form.
Note that
InputMode = comInputModeBinary
RThreshold : 1
must be set.
InputMode indicates that default
communication input type is binary, and
RThreshold =1 indicates that an event is
generated whenever 1 byte data is transmitted
to receiver buffer.
Data transmitted to receiver buffer can be
checked regularly without generating an event,
but this method makes the processing time too
slow.
Publication AN015A-EN-P - 19
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Checksum Calculation
When Modbus/RTU communication is performed, CRC calculation method
should be considered.
This application note uses the table type, which is the simplest way to
implement.
NXIO Slave
Address
Modbus
Function
[VB v6.0 type Table]
Address Hi
Address Lo
DATA
CRC calculation range
CRC
Low
CRC
High
CRC (2 byte)
CRCTable = Array(0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, _
1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, _
3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297, 6336, _
6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, _
7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, _
53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, _
61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721, _
13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, _
65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, _
59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, _
61249, 11520, 60865, 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, _
59009, 9792, 8704, 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, _
24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 42881, _
26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, _
28608, 28288, 44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, _
26688, 30720, 47297, 47489, 31040, 47873, 31680, 31360, 47681, 48641, _
32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 30080, _
46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, _
29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, _
37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937,_
23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, _
23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, _
35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, _
35905, 17408, 33985, 34177, 17728, 34561, 18368, 18048, 34369, 33281, _
17088, 17280, 33601, 16640, 33217, 32897, 16448)
Thus, basic CRC calculation routine in Visual Basic 6.0 is as follows:
CRC = 65535
For i = 0 To DataLength
CRC = Int(CRC / 256) Xor CRCTable((CRC Xor DataBuffer(i)) And 255)
Next i
By using the above table and basic routine, subroutine can be made as follows:
Private Sub CalcCRC(ByRef Buffer() As Byte, ByVal L As Integer)
On Error GoTo CC
Dim i As Integer
Dim CRC As Long
'Check Length whether it is 0
If L <= 0 Then Exit Sub
Buffer(L -2) = 0
Buffer(L - 1) = 0
20 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
CRC = 65535
For i = 0 To L - 3
CRC = CLng(Int(CRC / 256)) Xor CRCTable(CInt((CRC Xor Buffer(i)) And 255))
Next i
'------------------------------------------------------------------------Buffer(L - 2) = CByte(CRC And 255)
'CRC Low Byte
Buffer(L - 1) = CByte((CRC And 65280) / 256) 'CRC High Byte
Exit Sub
CC:
MsgBox "Unknown Error Occured", vbOKOnly, "Unknown Error"
End Sub
L is length to calculate CRC, and Buffer(i) is buffer to transmit data.
In addition, NX PLC uses the same table and type as those of CRC calculation
routine.
Data Transmission
The method of transmitting data to PLC through RS232C control (that is,
MSComm control) after transmission packet is generated is described. After
data is entered in Byte Array, transmission Byte Array is entered in Output
Property of MSComm Control. Then, data is automatically transmitted.
Be careful of the transmission buffer size and data type.
Transmission buffer size must be equal to transmission packet size, and data
type must be equal to byte type.
Additionally, InputMode = comInputModeBinary mentioned in 5.2 should be
set to transmit control characters such as special characters like &H00 (Null)
and &H04 (EOT).
The following Visual Basic Sub Routine is an example of entering Modbus
packet data in transmission buffer and transmitting it.
'------------------------------------'Make Packet
'------------------------------------Private Sub MakeNXIOModbusPacket()
On Error GoTo MPMP1
ReDim PLCSB(0 To 7)
Dim S As String
Dim SS As String
Dim i As Integer
S
SS = ""
'Address of Slave Device
PLCSB(0) = CByte(txtNXIOSlaveAddress.Text)
'Function Code
PLCSB(1) = CByte(Mid(cbNXIOFunction.Text, 1, 2))
'Address
Select Case chkHexNXIO.Value
Case 1
S = Hex(CInt("&H" + txtNXIOAddress.Text))
Case 0
S = Hex(CInt(txtNXIOAddress.Text))
End Select
SS = Right("0000" + S, 4)
PLCSB(2) = CByte("&H" + Mid(SS, 1, 2))
Publication AN015A-EN-P - 21
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
PLCSB(3) = CByte("&H" + Mid(SS, 3, 2))
'Data Size
Select Case chkHexSizeNXIO.Value
Case 1 'Hex
S = Hex(CInt("&H" + txtNXIODataSize.Text))
Case 0 'Decimal
S = Hex(CInt(txtNXIODataSize.Text))
End Select
SS = Right("0000" + S, 4)
PLCSB(4) = CByte("&H" + Mid(SS, 1, 2))
PLCSB(5) = CByte("&H" + Mid(SS, 3, 2))
'CRC
PLCSB(6) = 0
PLCSB(7) = 0
CalcCRC PLCSB, 8
ReDim PLCSBB(0 To UBound(PLCSB, 1))
S
For i = 0 To UBound(PLCSBB, 1)
PLCSBB(i) = PLCSB(i)
S = S + Right("00" + Hex(PLCSBB(i)), 2) + " "
Next i
txtSendData.Text = S
Exit Sub
MPMP1:
MsgBox "Unknown Error Occured", vbOKOnly, "Unknown Error"
End Sub
Data Reception
It is mentioned that RThreshold should be set to 1 in MSComm control. The
reason is setting to 1 leads effective data transmission even though it may be
changed depending on the program type.
RThreshold = 1
If the RThreshold function is not used, set to 0 (default). At the time, receiver
buffer can be checked on a regular basis by using timer. However, each case
has strong and weak points so that it is hard to select one among the cases.
RThreshold=1 indicates that OnComm() function, which is an event of
MSComm Control, is performed whenever 1 byte data is transmitted to the
receiver buffer of MSComm Control. For example, if the value is set to 10,
OnComm() function will be called when 10 byte data is transmitted.
An example of reading received data is described as follows:
Private Sub CommModbus_OnComm()
Dim DataLength As Byte
Dim T1 As Byte
Dim i As Integer
Dim AB() As Byte
Dim L As Integer
Dim OFS As Integer
Dim R() As Byte
Dim S As String
'---------------------Dim T
Dim j As Integer
22 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
'Wait for finishing data
'm_OFFSET = 0
Select Case commModbus.CommEvent
Case comEvReceive
ReceivedFlag = True 'Flag for meaning Receiveing Data
OFS = commModbus.InBufferCount
Do
R = commModbus.Input
For i = 0 To OFS - 1
RB(m_OFFSET + i) = R(i)
Next i
'Add Offset Value
m_OFFSET = m_OFFSET + OFS
'GIve a right to OS for
DoEvents
OFS = commModbus.InBufferCount
Loop While (OFS > 0)
AddCommLog11 "Received Data", RB, m_OFFSET
commModbus.InputLen = 0
commModbus.InBufferCount = 0
m_OFFSET = 0
For i = 0 To UBound(RB, 1)
RB(i) = 0
Next i
ReceivedFlag = False
End Select
End Sub
In the example above, comEvReceive should be checked carefully.
comEvReceive is a value transmitted to CommEvent when the RThreshold
value is set. Thus, after checking the value, read the data of the receiver buffer.
Other values transmitted to CommEvent are described as follows:
Constant
Value
Description
comEvSend
1
Transmits an event.
comEvReceive
2
Receives an event.
comEvCTS
3
Changes CTS line.
comEvDSR
4
Changes DSR line.
comEvCD
5
Changes CD line
comEvRing
6
Detect ring signal
comEvEOF
7
End of file
Publication AN015A-EN-P - 23
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Communication
through Visual
Basic.Net 2005
Since Communication
Control is not default
control, select the control
already registered and
register the control in Tool
Box in Visual Basic 6.0.
However, in Visual Studio
Basic. Net 2005, this control
is registered in default
element with the name of
SerialPort. Thus, this can be
used immediately.
In addition, in Visual Basic
6.0, after a control is
registered, the control is
displayed in the
corresponding form.
However, in Visual Basic.
Net 2005, a control being
used is displayed on the
lower part of the form as
shown on the left unless the
control is used to perform
actions in the window such
as list box and combo box.
(Same interface is applied
for Visual Studio.Net 2003.)
24 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Serial Port Control Setting
The window where control property is entered
is similar to that of Visual Basic 6.0.
However, the names of events supported and
the transmission parameters are completely
different.
For example, in VB6, if data is transmitted
while Rthreshold is set, OnComm() event is
generated. However, in VB.Net 2005,
DataReceived(ByVal sender As Object,
ByVal e As
System.IO.Ports.SerialDataReceivedEventAr
gs) Handles commModbus.DataReceived is
generated and transmission parameters are
added.
However, the method of use is almost similar.
Data Transmission
Different data transmission of Visual Basic.Net 2005 is
commModbus.Write(NXIOSB, 0, NXIOSB.Length)
Write() function is used to transmit, and parameters required are Buffer, Index,
and Length.
'------------------------------------'Make Packet
'------------------------------------Private Sub MakeNXIOModbusPacket()
Try
ReDim NXIOSB(0 To 7)
Dim S As String = ""
Dim SS As String = ""
'Address of Slave Device
NXIOSB(0) = CByte(txtNXIOSlaveAddress.Text)
'Function Code
NXIOSB(1) = CByte(Microsoft.VisualBasic.Mid(cbNXIOFunction.Text, 1, 2))
'Address
Select Case chkHexNXIO.Checked
Case True
S = Hex(CInt("&H" + txtNXIOAddress.Text))
Case False
S = Hex(CInt(txtNXIOAddress.Text))
End Select
SS = Microsoft.VisualBasic.Right("0000" + S, 4)
Publication AN015A-EN-P - 25
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
NXIOSB(2) = CByte("&H" + Microsoft.VisualBasic.Mid(SS, 1, 2))
NXIOSB(3) = CByte("&H" + Microsoft.VisualBasic.Mid(SS, 3, 2))
'Data Size
Select Case chkHexSizeNXIO.Checked
Case True 'Hex
S = Hex(CInt("&H" + txtNXIODataSize.Text))
Case False 'Decimal
S = Hex(CInt(txtNXIODataSize.Text))
End Select
SS = Microsoft.VisualBasic.Right("0000" + S, 4)
NXIOSB(4) = CByte("&H" + Microsoft.VisualBasic.Mid(SS, 1, 2))
NXIOSB(5) = CByte("&H" + Microsoft.VisualBasic.Mid(SS, 3, 2))
'CRC
NXIOSB(6) = 0
NXIOSB(7) = 0
CalcCRC(NXIOSB, 8)
ReDim NXIOSBB(NXIOSB.Length)
Dim i As Integer
S = ""
Array.ConstrainedCopy(NXIOSB, 0, NXIOSBB, 0, NXIOSB.Length)
For i = 0 To NXIOSB.Length - 1
S += Microsoft.VisualBasic.Right("00" + Hex(NXIOSBB(i)), 2) + " "
Next i
txtSendData.Text = S
commModbus.Write (NXIOSB, 0, NXIOSB.Length)
Catch
MessageBox.Show("Unknown Error Occured", "Unknown Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
The example above indicates that data is transmitted after Modbus/RTU Packet
is created.
26 - Publication AN015A-EN-P
Communicate NXIO Modbus RTU with VB6 and VB.Net2005 Application
Data Reception
An example of DataReceived() Event for receiving data is described as
follows: Actually, it is almost similar to VB6 coding.
Private Sub commModbus_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) Handles
commModbus.DataReceived
Try
Dim OFS, i As Integer
OFS = 0
Dim S As String = ""
With commModbus
My.Application.DoEvents()
'Check Data Size which in Receive Buffer
OFS = .BytesToRead
Do
.Read(RB, m_OFFSET, .BytesToRead)
'Add Offset Value
m_OFFSET = m_OFFSET + OFS
OFS = .BytesToRead
Loop While (OFS > 0)
If CheckCRC(RB, m_OFFSET) = False Then
ClearFlag()
Exit Sub
End If
'Copy Original Data to RB_0
ReDim RB_0(0 To m_OFFSET - 1)
Array.ConstrainedCopy(RB, 0, RB_0, 0, m_OFFSET)
For i = 0 To m_OFFSET - 1
S += Microsoft.VisualBasic.Right("00" + Hex(RB_0(i)), 2) + " "
Next i
txtReceivedData.Text = S
m_OFFSET = 0
ClearGlobalBuffer()
End With
Catch
MessageBox.Show("Unknown Error Occured-Serial Port Receive", "Unknown
Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Publication AN015A-EN-P - 27