HslCommunication.Profinet.Melsec.MelsecMcAsciiUdp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of HslCommunication Show documentation
Show all versions of HslCommunication Show documentation
一个工业物联网的底层架构框架,专注于底层的技术通信及跨平台,跨语言通信功能,实现各种主流的PLC数据读写,实现modbus的各种协议读写等等
package HslCommunication.Profinet.Melsec;
import HslCommunication.BasicFramework.SoftBasic;
import HslCommunication.Core.Address.McAddressData;
import HslCommunication.Core.Net.NetworkBase.NetworkUdpDeviceBase;
import HslCommunication.Core.Transfer.RegularByteTransform;
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Melsec.Helper.*;
import HslCommunication.StringResources;
import HslCommunication.Utilities;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class MelsecMcAsciiUdp extends NetworkUdpDeviceBase implements IReadWriteMc {
public MelsecMcAsciiUdp() {
this.WordLength = 1;
this.setByteTransform(new RegularByteTransform());
}
public MelsecMcAsciiUdp(String ipAddress, int port) {
this();
this.setIpAddress(ipAddress);
this.setPort(port);
}
private byte NetworkNumber = 0x00; // 网络号
private byte NetworkStationNumber = 0x00; // 网络站号
/**
* 获取网络号
*
* @return 网络号
*/
public byte getNetworkNumber() {
return NetworkNumber;
}
/**
* 设置网络号
*
* @param networkNumber 网络号
*/
public void setNetworkNumber(byte networkNumber) {
NetworkNumber = networkNumber;
}
/**
* 获取网络站号
*
* @return 网络站号
*/
public byte getNetworkStationNumber() {
return NetworkStationNumber;
}
/**
* 设置网络站号
*
* @param networkStationNumber 网络站号
*/
public void setNetworkStationNumber(byte networkStationNumber) {
NetworkStationNumber = networkStationNumber;
}
//endregion
/**
* 获取当前的PLC类型
* @return 类型信息
*/
public McType getMcType() {
return McType.MCAscii;
}
/**
* 分析地址的方法,允许派生类里进行重写操作
* @param address 地址信息
* @param length 读取的长度信息
* @return 解析后的数据信息
*/
public OperateResultExOne McAnalysisAddress( String address, short length ) {
return McAddressData.ParseMelsecFrom(address, length);
}
protected byte[] PackCommandWithHeader( byte[] command ) {
return McAsciiHelper.PackMcCommand(command, this.NetworkNumber, this.NetworkStationNumber);
}
protected OperateResultExOne UnpackResponseContent( byte[] send, byte[] response ) {
OperateResult check = McAsciiHelper.CheckResponseContentHelper(response);
if (!check.IsSuccess) return OperateResultExOne.CreateFailedResult(check);
return OperateResultExOne.CreateSuccessResult(SoftBasic.BytesArrayRemoveBegin(response, 22));
}
public byte[] ExtractActualData( byte[] response, boolean isBit ) {
return McAsciiHelper.ExtractActualDataHelper( response, isBit );
}
public OperateResultExOne Read(String address, short length) {
return McHelper.Read(this, address, length);
}
public OperateResultExOne ReadBool(String address, short length) {
return McHelper.ReadBool(this, address, length);
}
public OperateResult Write(String address, byte[] value) {
return McHelper.Write(this, address, value);
}
public OperateResult Write(String address, boolean[] values) {
return McHelper.Write(this, address, values);
}
/**
* 随机读取PLC的数据信息,可以跨地址,跨类型组合,但是每个地址只能读取一个word,也就是2个字节的内容。收到结果后,需要自行解析数据
* Randomly read PLC data information, which can be combined across addresses and types, but each address can only read one word,
* which is the content of 2 bytes. After receiving the results, you need to parse the data yourself
* @param address 所有的地址的集合
* @return 结果
*/
public OperateResultExOne ReadRandom( String[] address ) {
return McHelper.ReadRandom(this, address);
}
/**
* 随机读取PLC的数据信息,可以跨地址,跨类型组合,每个地址是任意的长度。收到结果后,需要自行解析数据,目前只支持字地址,比如D区,W区,R区,不支持X,Y,M,B,L等等
* Read the data information of the PLC randomly. It can be combined across addresses and types. Each address is of any length. After receiving the results,
* you need to parse the data yourself. Currently, only word addresses are supported, such as D area, W area, R area. X, Y, M, B, L, etc
* @param address 所有的地址的集合
* @param length 每个地址的长度信息
* @return 结果
*/
public OperateResultExOne ReadRandom( String[] address, short[] length ) {
return McHelper.ReadRandom(this, address,length);
}
/**
* 随机读取PLC的数据信息,可以跨地址,跨类型组合,但是每个地址只能读取一个word,也就是2个字节的内容。收到结果后,自动转换为了short类型的数组
* Randomly read PLC data information, which can be combined across addresses and types, but each address can only read one word,
* which is the content of 2 bytes. After receiving the result, it is automatically converted to an array of type short.
* @param address 所有的地址的集合
* @return 结果
*/
public OperateResultExOne ReadRandomInt16( String[] address ) {
return McHelper.ReadRandomInt16(this, address);
}
/**
* 读取PLC的标签信息,需要传入标签的名称,读取的字长度,标签举例:A; label[1]; bbb[10,10,10]
* To read the label information of the PLC, you need to pass in the name of the label,
* the length of the word read, and an example of the label: A; label [1]; bbb [10,10,10]
* @param tag 标签名
* @param length 读取长度
* @return 是否成功
*/
public OperateResultExOne ReadTags( String tag, short length ) {
return ReadTags( new String[] { tag }, new short[] { length } );
}
/**
* 读取PLC的标签信息,需要传入标签的名称,读取的字长度,标签举例:A; label[1]; bbb[10,10,10]
* To read the label information of the PLC, you need to pass in the name of the label,
* the length of the word read, and an example of the label: A; label [1]; bbb [10,10,10]
* @param tags 标签名
* @param length 读取长度
* @return 是否成功
*/
public OperateResultExOne ReadTags(String[] tags, short[] length ) {
return McBinaryHelper.ReadTags(this, tags, length);
}
/**
* 读取扩展的数据信息,需要在原有的地址,长度信息之外,输入扩展值信息
* To read the extended data information, you need to enter the extended value information in addition to the original address and length information
* @param extend 扩展信息
* @param address 地址
* @param length 数据长度
* @return 返回结果
*/
public OperateResultExOne ReadExtend( short extend, String address, short length ) {
return McHelper.ReadExtend(this, extend, address, length);
}
/**
* 读取缓冲寄存器的数据信息,地址直接为偏移地址
* Read the data information of the buffer register, the address is directly the offset address
* @param address 偏移地址
* @param length 读取长度
* @return 读取的内容
*/
public OperateResultExOne ReadMemory(String address, short length ) {
return McHelper.ReadMemory(this, address, length);
}
/**
* 远程Run操作
* Remote Run Operation
* @return 是否成功
*/
public OperateResult RemoteRun( ) {
return McHelper.RemoteRun(this);
}
/**
* 远程Stop操作
* Remote Stop operation
* @return 是否成功
*/
public OperateResult RemoteStop( ) {
return McHelper.RemoteStop(this);
}
/**
* 远程Reset操作
* Remote Reset Operation
* @return 是否成功
*/
public OperateResult RemoteReset() {
return McHelper.RemoteReset(this);
}
/**
* 读取PLC的型号信息,例如 Q02HCPU
* Read PLC model information, such as Q02HCPU
* @return 返回型号的结果对象
*/
public OperateResultExOne ReadPlcType( ) {
return McHelper.ReadPlcType(this);
}
/**
* LED 熄灭 出错代码初始化
* LED off Error code initialization
* @return 是否成功
*/
public OperateResult ErrorStateReset( ) {
return McHelper.ErrorStateReset(this);
}
public String toString() {
return "MelsecMcAsciiUdp[" + getIpAddress() + ":" + getPort() + "]";
}
}