/// <summary> /// Definition of the AsyncRead funcion for the SOEM functionality. /// It takes the slave number to communicate to, the index, and subindex of an object /// and it will call the respective needed low level SDO function to read the value of the object. /// The return value will be set on the value of the object dictionary /// </summary> /// <param name="slave_number">number of position of the slave to address</param> /// <param name="index">index of the object to read</param> /// <param name="subindex">subindex of the object to read</param> public override void AsyncRead(int slave_number, ushort index, byte subindex) { try { DictItem item = MW.ObjectDictionary.GetItem(index, subindex); switch (item.Type) { case "SINT": lock (comm_locker) item.Value = SdoReadInt8(slave_number, index, subindex); break; case "BOOL": case "USINT": lock (comm_locker) item.Value = SdoReadUInt8(slave_number, index, subindex); break; case "INT": lock (comm_locker) item.Value = SdoReadInt16(slave_number, index, subindex); break; case "UINT": lock (comm_locker) item.Value = SdoReadUInt16(slave_number, index, subindex); break; case "DINT": lock (comm_locker) item.Value = SdoReadInt32(slave_number, index, subindex); break; case "UDINT": lock (comm_locker) item.Value = SdoReadUInt32(slave_number, index, subindex); break; case "STRING": byte[] buf = new byte[item.Length]; lock (comm_locker) SdoReadString(slave_number, index, subindex, buf, item.Length); item.Value = buf; break; default: throw new NotImplementedException(); } DictionaryBuilder.FormatDisplayString(item); /* formats the display string of the value (dec, bin, hex) */ item.TimeStamp = Convert.ToUInt32(stopwatch.ElapsedMilliseconds); } catch (Exception err) { MessageBox.Show(err.ToString()); } }
private void GetObjectsFromBuffer(byte[] rcv_client_msg) { byte cmd, subindex; ushort index; ushort num_obj = BitConverter.ToUInt16(rcv_client_msg, 0); uint timestamp = BitConverter.ToUInt32(rcv_client_msg, 2); DictItem item; try { PCS device = Devices[0] as PCS; int pointer = 6; //+ _num_pdos * 8; for (int i = 1; i <= num_obj; i++) { cmd = rcv_client_msg[pointer]; pointer += 1; index = BitConverter.ToUInt16(rcv_client_msg, pointer); pointer += 2; subindex = rcv_client_msg[pointer]; pointer += 1; try { item = MW.ObjectDictionary.GetItem(index, subindex); } catch (Exception err) { MessageBox.Show(err.ToString()); continue; } switch (cmd) /* For the meaning of the commands see the definition of the static class CMD */ { case CMD.sdo_R: //0x00: switch (item.Type) { case "SINT": item.Value = unchecked ((sbyte)rcv_client_msg[pointer]); break; case "BOOL": case "USINT": item.Value = rcv_client_msg[pointer]; break; case "INT": item.Value = BitConverter.ToInt16(rcv_client_msg, pointer); break; case "UINT": item.Value = BitConverter.ToUInt16(rcv_client_msg, pointer); break; case "DINT": item.Value = BitConverter.ToInt32(rcv_client_msg, pointer); break; case "UDINT": item.Value = BitConverter.ToUInt32(rcv_client_msg, pointer); break; case "STRING": item.Value = rcv_client_msg.SubArray(pointer, item.Length); break; default: throw new NotImplementedException(); } DictionaryBuilder.FormatDisplayString(item); item.TimeStamp = timestamp; break; case CMD.sdo_W: //0x01: uint abort_ret = BitConverter.ToUInt32(rcv_client_msg, pointer); if (abort_ret == 0) { item.Access = "RWG"; } else { item.Access = "RWR"; MW.Dispatcher.Invoke(() => { MW.ErrorNoti.Items.Insert(0, new ErrorNotifications { TimeStamp = DateTime.Now.ToLongTimeString(), Notification = string.Format("SDO Abort Transfer: {0} (0x{1:X8})", MW.ErrorNoti.AbortCodes[abort_ret], abort_ret) }); }); } break; case CMD.pdo_R: //0x10: if (item.Index == 0x6041) { device.stateMachineDsp402.StateWord = BitConverter.ToUInt16(rcv_client_msg, pointer); } else if (item.Index == 0x6064) { device.ActualPosition = BitConverter.ToInt32(rcv_client_msg, pointer); } else if (item.Index == 0x606C) { device.ActualVelocity = BitConverter.ToInt32(rcv_client_msg, pointer); } else if (item.Index == 0x6061) { device.CurrentMode = unchecked ((sbyte)rcv_client_msg[pointer]); } break; case CMD.pdo_W: //0x11: break; default: break; } pointer += Math.Max(item.Length, 4); } } catch (Exception err) { MessageBox.Show(err.ToString()); } }