In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Nmodubs4 how to achieve single-register and multi-register AO read and write, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Project effect drawing
Create a plc connection
/ / reference the plc_connect method with the delegate object.
Delegate_Connect connect1 = new Delegate_Connect (plc_connect)
A_status = 1
Connect1 ()
If (a_status = = 0)
{
MessageBox.Show ("gateway connection failed!")
Return
}
2. Plc connects IP and port to create tcp client
Private void plc_connect ()
{
Wg_ip = INI.InitClass.ReadString ("application", "WG_IP", "")
Wg_port = INI.InitClass.ReadString ("application", "WG_PORT", "")
Try
{
Tcp_wg = new TcpClient (wg_ip, Convert.ToInt32 (wg_port))
My_wg = ModbusIpMaster.CreateIp (tcp_wg)
A_status = 1
}
Catch (Exception exception)
{
A_status = 0
}
}
Third, multi-thread processing timer
/ / start timer multithreading and refresh online test data every 1 second
Timer_plc_qry.Interval = 2000
/ / timer_plc_qry.Enabled = true
Timer_plc_qry.Stop ()
Timer_plc_qry.Elapsed + = TimersTimerHandler
Timer_plc_qry.Start ()
/ / multithreaded timer event online testing
Private void TimersTimerHandler (object sender, EventArgs args)
{
If (a_status = = 0) return
If (Interlocked.Exchange (ref inTimer_plc_qry, 1) = = 0 & & Start)
{
/ / connect the device
Read_plc ()
Read_plc_switch ()
}
Interlocked.Exchange (ref inTimer_plc_qry, 0)
}
IV. Single register and multiple registers
If (list_ core [I] .data _ type = = "word")
{
Try
{
Read_result = my_wg.ReadHoldingRegisters (1, Convert.ToUInt16 (list_ core [I] .address), 1)
If (read_result! = null)
{
Read = read_result [0]; / / read a single register ushort type
This.arcScaleComponent1.Value = read;// progress bar replication
Set_main_UAB (read.ToString ()); / / label tag copy
}
}
Catch (Exception e)
{
}
}
Else if (list_ core [I] .data _ type = = "float")
{
Read_result = my_wg.ReadHoldingRegisters (1, Convert.ToUInt16 (list_ core [I] .address), 2)
If (read_result! = null)
{
If (Islow)
{
Read_float = ModbusUtility.GetSingle (read_result [1], read_result [0]); / / read multiple registers, using the function ModbusUtility conversion
This.arcScaleComponent1.Value = read_float
Set_main_UAB (read_float.ToString ()); / / sets the main form control
} else
{
Read_float = ModbusUtility.GetSingle (read_result [0], read_result [1])
This.arcScaleComponent1.Value = read_float
Set_main_UAB (read_float.ToString ())
}
}
}
5. Common methods of Nmodbus4:
Method name
Action
Required parameters
Return value
Corresponding function code
ReadCoils
Read the status of the DO
Slave address (8 bits)
Byte slaveAddress
Starting address (16 bits)
Ushort startAddress
Number of reads (16 bits)
Ushort numberOfPoints
Bool []
01
ReadInputs
Read the status of the DI
Slave address (8 bits)
Byte slaveAddress
Starting address (16 bits)
Ushort startAddress
Number of reads (16 bits)
Ushort numberOfPoints
Bool []
02
ReadHoldingRegisters
Read the value of AO
Slave address (8 bits)
Byte slaveAddress
Starting address (16 bits)
Ushort startAddress
Number of reads (16 bits)
Ushort numberOfPoints
Ushort []
03
ReadInputRegisters
Read the value of AI
Slave address (8 bits)
Byte slaveAddress
Starting address (16 bits)
Ushort startAddress
Number of reads (16 bits)
Ushort numberOfPoints
Ushort []
04
WriteSingleCoil
Write values to DO
Slave address (8 bits)
Byte slaveAddress
Coil address (16 bits)
Ushort coilAddress
Write value (Boolean)
Bool value
No return value
05
WriteSingleRegister
Write values to AO
Slave address (8 bits)
Byte slaveAddress
Register address (16 bits)
Ushort registerAddress
Write value (16 bits)
Ushort value
No return value
06
WriteMultipleCoils
Write multi-coil register
Slave address (8 bits)
Byte slaveAddress
Starting address (16 bits)
Ushort startAddress
Write value (Boolean array)
Bool [] data
No return value
fifteen
WriteMultipleRegisters
Write multiple hold registers
Slave address (8 bits)
Byte slaveAddress
Starting address (16 bits)
Ushort startAddress
Register value (16-bit integer array)
Ushort [] data
No return value
sixteen
ReadWriteMultipleRegisters
Read and write multiple hold registers
Slave address (8 bits)
Byte slaveAddress
Read starting address (16 bits)
Ushort startReadAddress
Number of reads (16 bits)
Ushort numberOfPointsToRead
Write starting address (16 bits)
Ushort startWriteAddress
Write value (16-bit integer array)
Ushort [] writeData
Ushort []
twenty-three
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.