public void ReadExample2( ) { #region ReadExample2 MelsecFxSerial melsecFx = new MelsecFxSerial( ); melsecFx.SerialPortInni(sp => { sp.PortName = "COM1"; sp.BaudRate = 9600; sp.DataBits = 7; sp.StopBits = System.IO.Ports.StopBits.One; sp.Parity = System.IO.Ports.Parity.Even; }); melsecFx.Open( ); OperateResult <byte[]> read = melsecFx.Read("D100", 4); if (read.IsSuccess) { float temp = melsecFx.ByteTransform.TransInt16(read.Content, 0) / 10f; float press = melsecFx.ByteTransform.TransInt16(read.Content, 2) / 100f; int count = melsecFx.ByteTransform.TransInt32(read.Content, 2); // do something } else { // failed } #endregion }
private void button25_Click(object sender, EventArgs e) { try { OperateResult <byte[]> read = melsecSerial.Read(textBox6.Text, ushort.Parse(textBox9.Text)); if (read.IsSuccess) { textBox10.Text = "结果:" + HslCommunication.BasicFramework.SoftBasic.ByteToHexString(read.Content); } else { MessageBox.Show("读取失败:" + read.ToMessageShowString( )); } } catch (Exception ex) { MessageBox.Show("读取失败:" + ex.Message); } }
public byte[] ReadBytes(DeviceAddress address, ushort size) { string addr; addr = GetAddress(address); try { lock (_async) { if (address.DBNumber < 3) { var read = _fxSerial.ReadBool(addr, (ushort)(size * 16)); if (read.IsSuccess) { byte[] retBytes = new byte[size * 2]; Buffer.BlockCopy(read.Content, 0, retBytes, 0, size * 2); return(retBytes); } else { if (OnError != null) { OnError(this, new IOErrorEventArgs(read.Message)); } return(null); } } else { var read = _fxSerial.Read(addr, size); if (read.IsSuccess) { return(read.Content); } else { if (OnError != null) { OnError(this, new IOErrorEventArgs(read.Message)); } return(null); } } } } catch (Exception e) { if (OnError != null) { OnError(this, new IOErrorEventArgs(e.Message)); } return(null); } }
private void test5() { // The complex situation is that you need to parse the byte array yourself. // Here's just one example. OperateResult <byte[]> read = melsecSerial.Read("D100", 10); if (read.IsSuccess) { int count = melsecSerial.ByteTransform.TransInt32(read.Content, 0); float temp = melsecSerial.ByteTransform.TransSingle(read.Content, 4); short name1 = melsecSerial.ByteTransform.TransInt16(read.Content, 8); string barcode = Encoding.ASCII.GetString(read.Content, 10, 10); } }
public override object ReadTag(Tag tag) { if (tag.AccessType == TagAccessType.Read || tag.AccessType == TagAccessType.ReadWrite) { try { if (tag.TagType == "bool") { var res = PLC.ReadBool(tag.Address); if (res.IsSuccess) { tag.TagValue = res.Content; tag.Quality = Quality.Good; } else { tag.Quality = Quality.Bad; } } else if (tag.TagType == "string") { if (tag.Address.Contains(".")) { try { string address = tag.Address.Split('.')[0]; ushort len = Convert.ToUInt16(tag.Address.Split('.')[1]); var res = PLC.ReadString(tag.Address.Split('.')[0], len); if (res.IsSuccess) { tag.TagValue = res.Content.Replace("\0", ""); tag.Quality = Quality.Good; } else { tag.Quality = Quality.Bad; } } catch (Exception) { LOG.Error($"Tag Address Error {tag.Address}"); } } else { LOG.Error($"Tag Address Error {tag.Address}"); } } else { ushort len = ConvertUtils.GetLength(tag); var res = PLC.Read(tag.Address, len); ConvertUtils.DecodeTagValue(tag, res); } return(tag.TagValue); } catch (Exception ex) { LOG.Error($"Datasource[{SourceName}] read error. Tag[{tag.TagName}] Address[{tag.Address}] Message[{ex.Message}]"); tag.TagValue = null; tag.Quality = Quality.Bad; return(tag.TagValue); } } else { return(null); } }