private void button1_Click(object sender, EventArgs e) { System.Diagnostics.Stopwatch sp = new System.Diagnostics.Stopwatch(); sp.Start(); string bitAdd = txtBitAdd.Text.Trim(); short re = ENT.SetBitState(PlcMemory.DM, bitAdd, BitState.ON); sp.Stop(); txtUseTimeB.Text = sp.Elapsed.TotalMilliseconds.ToString() + "ms"; if (re != 0) { //写入数据出错 MessageBox.Show("打开指定位失败!"); } else { MessageBox.Show("打开指定位成功!"); } }
public bool WriteSingleElement(PlcScanItems item, object value) { PlcMemory memorytype = item.AddressType; string bitstartaddress = string.Empty; short startaddress = 0; if (item.DataType == DataType.BIT) { bitstartaddress = item.Address; } else { startaddress = short.Parse(item.Address); } short result = 0; lock (readLock) { switch (item.DataType) { case DataType.BIT: bool boolvalue = (bool)value; if (boolvalue) { result = omronethernetplc.SetBitState(memorytype, bitstartaddress.ToString(), BitState.ON); } else { result = omronethernetplc.SetBitState(memorytype, bitstartaddress.ToString(), BitState.OFF); } break; case DataType.INT16: //Int16 int16value = (Int16)value; Int16 int16value = Convert.ToInt16(value.ToString()); result = omronethernetplc.WriteInt16(memorytype, startaddress, int16value); break; case DataType.INT32: Int32 int32value = Convert.ToInt32(value.ToString()); result = omronethernetplc.WriteInt32(memorytype, startaddress, int32value); break; case DataType.REAL: float floatvalue = Convert.ToSingle(value.ToString()); result = omronethernetplc.WriteFloat(memorytype, startaddress, floatvalue); break; case DataType.UINT16: UInt16 uint16value = Convert.ToUInt16(value.ToString()); result = omronethernetplc.WriteUint16(memorytype, startaddress, uint16value); break; case DataType.UINT32: UInt32 uint32value = Convert.ToUInt32(value.ToString()); result = omronethernetplc.WriteUint32(memorytype, startaddress, uint32value); break; } return(result == 0 ? true : false); } }