public static void WriteBusQueue(List <string> list) { try { WriteTwincat("GVL.EraseTable", true); TcAdsClient client = new TcAdsClient(); client.Connect(amsnetid, Convert.ToInt32(amsnetport)); int handle = client.CreateVariableHandle("GVL.DataFromBus"); foreach (string el in list) { AdsStream stream = new AdsStream(500); AdsBinaryWriter writer = new AdsBinaryWriter(stream); writer.WritePlcString(el, 500, Encoding.Unicode); client.Write(handle, stream); stream.Dispose(); writer.Dispose(); Thread.Sleep(10); } client.DeleteVariableHandle(handle); client.Dispose(); } catch (Exception ex) { Console.WriteLine("BusWrite: " + ex.Message); } }
private void btnAutoMode_Click(object sender, EventArgs e) { try { //lenght of the stream = length of string + 1 //1,先有adsstream, //2,再有binarywriter.write //3,再有clien.write() //---------把字符串分解成char 在写入plc ------- //modeWrite = new BinaryWriter(modeStream); //modeWrite.Write(strMode.ToCharArray()); //client.Write(hModeUI, modeStream); //---------把字符串分解成char 在写入plc //From version 1.0.0.10 and higher the classes AdsBinaryReader and AdsBinaryWriter // can be used to read and write strings //---------用字符串的方式写入--------------- strMode = "Auto"; modeStream = new AdsStream(30); adsWriter = new AdsBinaryWriter(modeStream); adsWriter.WritePlcString(strMode, 30, Encoding.ASCII); client.Write(hModeUI, modeStream); //-----------可以使用的------------ //---------用字符串的方式写入--------------- } catch (Exception err) { MessageBox.Show("set Auto to PLC: " + err.Message); } btnAutoMode.BackColor = Color.Green; btnCalibMode.BackColor = Color.Empty; btnManualMode.BackColor = Color.Empty; btnSettingMode.BackColor = Color.Empty; }
private void btnSettingMode_Click(object sender, EventArgs e) { strMode = "Setting"; modeStream = new AdsStream(30); adsWriter = new AdsBinaryWriter(modeStream); try { adsWriter.WritePlcString(strMode, 30, Encoding.ASCII); client.Write(hModeUI, modeStream); } catch (Exception err) { MessageBox.Show("try to write setting Mode to PLC: " + err.Message); } btnAutoMode.BackColor = Color.Empty; btnCalibMode.BackColor = Color.Empty; btnManualMode.BackColor = Color.Empty; btnSettingMode.BackColor = Color.Green; }
public static void WriteTwincat(string comando, object valore) { if (!(valore is null)) { using (TcAdsClient client = new TcAdsClient()) { try { client.Connect(amsnetid, Convert.ToInt32(amsnetport)); int handle = client.CreateVariableHandle(comando); if (valore.GetType().FullName.Contains("String")) { string el = valore.ToString(); AdsStream stream = new AdsStream(500); AdsBinaryWriter writer = new AdsBinaryWriter(stream); writer.WritePlcString(el, 500, Encoding.Unicode); client.Write(handle, stream); stream.Dispose(); writer.Dispose(); } else { client.WriteAny(handle, valore); } //client.Dispose(); client.DeleteVariableHandle(handle); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
public AdsErrorCode TryLoadType(string name, bool lookup, out ITcAdsDataType type) { if (string.IsNullOrEmpty(name)) { throw new ArgumentOutOfRangeException("name"); } if (lookup) { DataTypeInfoTable table = this; lock (table) { if (this._dataTypes.TryGetDataType(name, out type)) { return(AdsErrorCode.NoError); } } } int length = 0; bool isUnicode = false; if (DataTypeStringParser.TryParseString(name, out length, out isUnicode)) { int byteCount = 0; AdsDatatypeId dataType = AdsDatatypeId.ADST_VOID; if (isUnicode) { char[] chars = new char[] { 'a' }; byteCount = Encoding.Unicode.GetByteCount(chars); dataType = AdsDatatypeId.ADST_WSTRING; } else { char[] chars = new char[] { 'a' }; byteCount = Encoding.ASCII.GetByteCount(chars); dataType = AdsDatatypeId.ADST_STRING; } type = new TcAdsDataType(name, dataType, (uint)((length + 1) * byteCount), DataTypeCategory.String, typeof(string)); DataTypeInfoTable table2 = this; lock (table2) { this._dataTypes.Add(type); } this.OnTypeGenerated(type); return(AdsErrorCode.NoError); } AdsStream stream = new AdsStream(name.Length + 1); using (AdsBinaryWriter writer = new AdsBinaryWriter(stream)) { writer.WritePlcString(name, name.Length + 1, this._encoding); AdsStream rdDataStream = new AdsStream(0xffff); try { int num3 = this._adsClient.ReadWrite(0xf011, 0, rdDataStream, stream); } catch (AdsErrorException exception1) { if (exception1.ErrorCode != AdsErrorCode.DeviceSymbolNotFound) { throw; } type = null; } using (AdsBinaryReader reader = new AdsBinaryReader(rdDataStream)) { AdsDataTypeEntry entry = new AdsDataTypeEntry(true, this._encoding, reader); type = new TcAdsDataType(entry, this); DataTypeInfoTable table3 = this; lock (table3) { this._dataTypes.Add(type); } } } if (type != null) { return(AdsErrorCode.NoError); } this.OnResolveError(name); return(AdsErrorCode.DeviceSymbolNotFound); }
private bool WriteToStream(AdsStream dataStream, Tag tag, object value) { var binaryWriter = new AdsBinaryWriter(dataStream); switch (tag.DataType) { case IEC61131_3_DataTypes.Boolean: if (value is string) { value = Boolean.Parse((string)value); } binaryWriter.Write((bool)value); break; case IEC61131_3_DataTypes.Byte: binaryWriter.Write((byte)value); break; case IEC61131_3_DataTypes.SInt: binaryWriter.Write(Convert.ToSByte(value)); break; case IEC61131_3_DataTypes.USInt: binaryWriter.Write(Convert.ToByte(value)); break; case IEC61131_3_DataTypes.UInt: binaryWriter.Write(Convert.ToUInt16(value)); break; case IEC61131_3_DataTypes.Int: binaryWriter.Write(Convert.ToInt16(value)); break; case IEC61131_3_DataTypes.DInt: binaryWriter.Write((int)value); break; case IEC61131_3_DataTypes.LInt: binaryWriter.Write(Convert.ToInt64(value)); break; case IEC61131_3_DataTypes.Real: binaryWriter.Write((float)value); break; case IEC61131_3_DataTypes.LReal: binaryWriter.Write((double)value); break; case IEC61131_3_DataTypes.Word: binaryWriter.Write(Convert.ToUInt16(value)); break; case IEC61131_3_DataTypes.DWord: binaryWriter.Write(Convert.ToUInt32(value)); break; case IEC61131_3_DataTypes.LWord: binaryWriter.Write(Convert.ToUInt64(value)); break; case IEC61131_3_DataTypes.ULInt: binaryWriter.Write(Convert.ToUInt64(value)); break; case IEC61131_3_DataTypes.UDInt: binaryWriter.Write(Convert.ToUInt32(value)); break; default: // handle STRINT DataType if (StructuredTextSyntaxRegexHelper.StringDataTypeRegex.IsMatch(tag.DataType)) { var tmpString = (string)value; var length = tag.BitSize / 8; if (tmpString.Length > length) { throw new TagException("string is exceeds max defined lenght of " + length, tag); } binaryWriter.WritePlcString((string)value, (int)dataStream.Length); break; } if (BeckhoffHelper.IsEnum(tag.DataType, tag.BitSize)) { binaryWriter.Write(Convert.ToInt16(value)); break; } switch (tag.BitSize) { case 1: if (value is string) { value = Boolean.Parse((string)value); } binaryWriter.Write((bool)value); break; case 8: binaryWriter.Write(Convert.ToByte(value)); break; case 16: binaryWriter.Write(Convert.ToUInt16(value)); break; case 32: binaryWriter.Write(Convert.ToUInt32(value)); break; case 64: binaryWriter.Write(Convert.ToUInt64(value)); break; default: return(false); } break; } return(true); }