private void Button_Click(object sender, RoutedEventArgs e) { try { _tcClient.ReadWrite(0x1, 0x1, adsReadStream, adsWriteStream); byte[] dataBuffer = adsReadStream.ToArray(); Box.Text = "Counter started value = " + BitConverter.ToUInt32(dataBuffer, 0); } catch (Exception err) { MessageBox.Show(err.Message); } }
private void adsSendData(double shoulderLevelShift, double elbow, double wrist, double hand) { adsReadStream = new AdsStream(40); adsWriteStream = new AdsStream(sizeof(double) * 4); AdsBinaryWriter binWriter = new AdsBinaryWriter(adsWriteStream); adsWriteStream.Position = 0; binWriter.Write(shoulderLevelShift); binWriter.Write(elbow); binWriter.Write(wrist); binWriter.Write(hand); _tcClient.ReadWrite(0x1, 0x1, null, adsWriteStream); }
private static AdsStream BatchRead(TcAdsClient adsClient, IList <IAddressable> variables) { if (!variables.Any()) { throw new InvalidOperationException("Variables should not be empty"); } // Allocate memory int rdLength = variables.Count * ErrorSize; int wrLength = (SymbolValueByHandleSize + VariableHandleSize + BitSizeSize) * variables.Count; // Write data for handles into the ADS Stream using (var writer = new BinaryWriter(new AdsStream(wrLength))) { // Byt till Immutable list! foreach (var readableAddress in variables) { writer.Write((int)AdsReservedIndexGroups.SymbolValueByHandle); writer.Write(readableAddress.Address.VariableHandle); writer.Write(readableAddress.Address.BitSize); rdLength += readableAddress.Address.BitSize; } // Sum command to read variables from the PLC AdsStream dataAndErrorStream = new AdsStream(rdLength); adsClient.ReadWrite(0xF080, variables.Count, dataAndErrorStream, (AdsStream)writer.BaseStream); // Return the ADS error codes return(dataAndErrorStream); } }
//0xF081指令,将Stream写入PLC的内存地址,注意方法名为ReadWrite private AdsStream BlockWrite(VariableInfo variables, double[,] joint_states) { //分配内存 int rdLength = 4; int wrLength = 12 + 48 * 5000; BinaryWriter writer = new BinaryWriter(new AdsStream(wrLength)); // Write data for handles into the ADS stream writer.Write(variables.indexGroup); writer.Write(variables.indexOffset); writer.Write(variables.length); // Write data to send to PLC behind the structure for (int i = 0; i < 5000; i++) { for (int j = 0; j < 6; j++) { writer.Write(joint_states[i, j]); } } // Sum command to write the data into the PLC AdsStream rdStream = new AdsStream(rdLength); tcclient.ReadWrite(0xF081, 1, rdStream, (AdsStream)writer.BaseStream); return(rdStream); }
private static AdsStream BatchWrite(TcAdsClient adsClient, IList <IWritableAddress> addresses) { if (!addresses.Any()) { throw new InvalidOperationException("addresses should not be empty"); } // Allocate memory int rdLength = addresses.Count * ErrorSize; var totalMemorySize = addresses.Sum(x => x.Address.BitSize); int wrLength = (addresses.Count * (SymbolValueByHandleSize + VariableHandleSize + BitSizeSize)) + totalMemorySize; using (var writer = new BinaryWriter(new AdsStream(wrLength))) { // Write data for handles into the ADS stream foreach (var writableAddress in addresses) { writer.Write((int)AdsReservedIndexGroups.SymbolValueByHandle); writer.Write(writableAddress.Address.VariableHandle); writer.Write(writableAddress.Address.BitSize); } foreach (var writableAddress in addresses) { writableAddress.WriteValueToStream(writer); } AdsStream errorStream = new AdsStream(rdLength); adsClient.ReadWrite(0xF081, addresses.Count, errorStream, (AdsStream)writer.BaseStream); foreach (var writableAddress in addresses) { writableAddress.NotifyWritten(); } // Return the ADS error codes return(errorStream); } }