void nextsdoplease(SDO sdo) { if (ih.pages.ContainsKey(currentpage)) { byte[] sdodata = new byte[3 * 1024 + 3]; //pagesize + header; UInt32 pageaddr = currentpage * 2 * 1024; //awesome byte[] pagebits = BitConverter.GetBytes(pageaddr); //24 bit page address sdodata[0] = pagebits[0]; //LSB sdodata[1] = pagebits[1]; sdodata[2] = pagebits[2]; //c# MSB for (int x = 0; x < (3 * 1024); x++) { sdodata[x + 3] = ih.pages[currentpage][x]; } textBox1.Invoke(new MethodInvoker(delegate() { progressBar1.Value = (int)currentpage; label2.Text = (String.Format("writing page {0}/{1}\r\n", currentpage, ih.maxpageno)); })); currentpage++; //nextsdoplease(null); lco.SDOwrite((byte)numericUpDown_node.Value, 0x1f50, 0x01, sdodata, nextsdoplease); } else { textBox1.Invoke(new MethodInvoker(delegate() { textBox1.AppendText(String.Format("Skipping page {0}\r\n", currentpage)); })); currentpage++; if (currentpage <= ih.maxpageno) { nextsdoplease(null); } else { textBox1.Invoke(new MethodInvoker(delegate() { label2.Text = "DONE"; textBox1.AppendText("DONE ALL PAGES\r\n"); busy = false; button1.Enabled = true; button_flash.Enabled = true; textBox1.AppendText("** booting main program **\r\n"); lco.NMT_start(node); })); } } }
private void button_read_Click(object sender, EventArgs e) { if (!lco.isopen()) { MessageBox.Show("CAN not open"); return; } if (numericUpDown_node.Value == 0) { MessageBox.Show("You cannot read from Node 0, please select a node"); return; } listView1.Invoke(new MethodInvoker(delegate { button_read.Enabled = false; foreach (ListViewItem lvi in listView1.Items) { lvi.BackColor = Color.White; sdocallbackhelper help = (sdocallbackhelper)lvi.Tag; SDO sdo = lco.SDOread((byte)numericUpDown_node.Value, (UInt16)help.od.Index, (byte)help.od.Subindex, gotit); help.sdo = sdo; lvi.Tag = help; } })); }
void upsucc(SDO sdo) { //button_read_Click(null, null); listView1.Invoke(new MethodInvoker(delegate { foreach (ListViewItem lvi in listView1.Items) { sdocallbackhelper help = (sdocallbackhelper)lvi.Tag; if (help.sdo != sdo) { continue; } sdo = lco.SDOread((byte)numericUpDown_node.Value, (UInt16)help.od.Index, (byte)help.od.Subindex, gotit); help.sdo = sdo; lvi.Tag = help; break; } })); }
private void listView1_DoubleClick(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 0) { return; } if (!lco.isopen()) { MessageBox.Show("CAN not open"); return; } sdocallbackhelper h = (sdocallbackhelper)listView1.SelectedItems[0].Tag; ValueEditor ve = new ValueEditor(h.od, listView1.SelectedItems[0].SubItems[5].Text); if (h.od.StorageLocation == "ROM") { MessageBox.Show("Should not edit ROM objects"); } ve.UpdateValue += delegate(string s) { DataType dt = h.od.datatype; if (dt == DataType.UNKNOWN && h.od.parent != null) { dt = h.od.parent.datatype; } SDO sdo = null; switch (dt) { case DataType.REAL32: { float val = (float)new SingleConverter().ConvertFromString(ve.newvalue); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.REAL64: { double val = (double)new DoubleConverter().ConvertFromString(ve.newvalue); byte[] payload = BitConverter.GetBytes(val); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, payload, upsucc); break; } case DataType.INTEGER8: { sbyte val = (sbyte)new SByteConverter().ConvertFromString(ve.newvalue); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.INTEGER16: { Int16 val = (Int16) new Int16Converter().ConvertFromString(ve.newvalue); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.INTEGER32: { Int32 val = (Int32) new Int32Converter().ConvertFromString(ve.newvalue); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.UNSIGNED8: { byte val = (byte)new ByteConverter().ConvertFromString(ve.newvalue); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.UNSIGNED16: { UInt16 val = (UInt16) new UInt16Converter().ConvertFromString(ve.newvalue); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.UNSIGNED32: { UInt32 val = (UInt32) new UInt32Converter().ConvertFromString(ve.newvalue); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.INTEGER64: { Int64 val = (Int64) new Int64Converter().ConvertFromString(ve.newvalue); byte[] payload = BitConverter.GetBytes(val); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, payload, upsucc); break; } case DataType.UNSIGNED64: { UInt64 val = (UInt64) new UInt64Converter().ConvertFromString(ve.newvalue); byte[] payload = BitConverter.GetBytes(val); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, payload, upsucc); break; } case DataType.VISIBLE_STRING: { byte[] payload = Encoding.ASCII.GetBytes(ve.newvalue); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, payload, upsucc); break; } default: break; } h.sdo = sdo; listView1.SelectedItems[0].Tag = h; }; //SDO sdo = null; if (ve.ShowDialog() == DialogResult.OK) { /* * DataType dt = h.od.datatype; * * if (dt == DataType.UNKNOWN && h.od.parent != null) * dt = h.od.parent.datatype; * * switch (dt) * { * case DataType.REAL32: * { * * float val = (float)new SingleConverter().ConvertFromString(ve.newvalue); * sdo=lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, val, upsucc); * break; * } * * case DataType.REAL64: * { * * double val = (double)new DoubleConverter().ConvertFromString(ve.newvalue); * byte[] payload = BitConverter.GetBytes(val); * sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, payload, upsucc); * break; * } * * case DataType.INTEGER8: * { * sbyte val = (sbyte)new SByteConverter().ConvertFromString(ve.newvalue); * sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, val, upsucc); * break; * } * * case DataType.INTEGER16: * { * Int16 val = (Int16)new Int16Converter().ConvertFromString(ve.newvalue); * sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, val, upsucc); * break; * } * * * case DataType.INTEGER32: * { * Int32 val = (Int32)new Int32Converter().ConvertFromString(ve.newvalue); * sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, val, upsucc); * break; * } * case DataType.UNSIGNED8: * { * byte val = (byte)new ByteConverter().ConvertFromString(ve.newvalue); * sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, val, upsucc); * break; * } * case DataType.UNSIGNED16: * { * UInt16 val = (UInt16)new UInt16Converter().ConvertFromString(ve.newvalue); * sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, val, upsucc); * break; * } * * case DataType.UNSIGNED32: * { * UInt32 val = (UInt32)new UInt32Converter().ConvertFromString(ve.newvalue); * sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, val, upsucc); * break; * } * * case DataType.INTEGER64: * { * * Int64 val = (Int64)new Int64Converter().ConvertFromString(ve.newvalue); * byte[] payload = BitConverter.GetBytes(val); * sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, payload, upsucc); * break; * } * * case DataType.UNSIGNED64: { * * UInt64 val = (UInt64)new UInt64Converter().ConvertFromString(ve.newvalue); * byte[] payload = BitConverter.GetBytes(val); * sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, payload, upsucc); * break; * } * * case DataType.VISIBLE_STRING: * { * * byte [] payload = Encoding.ASCII.GetBytes(ve.newvalue); * sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.index, (byte)h.od.subindex, payload, upsucc); * break; * } * * * * default: * * break; * } */ // h.sdo = sdo; // listView1.SelectedItems[0].Tag = h; } }
void gotit(SDO sdo) { try { listView1.Invoke(new MethodInvoker(delegate { if (lco.getSDOQueueSize() == 0) { button_read.Enabled = true; } label_sdo_queue_size.Text = string.Format("SDO Queue Size: {0}", lco.getSDOQueueSize()); foreach (ListViewItem lvi in listView1.Items) { sdocallbackhelper h = (sdocallbackhelper)lvi.Tag; if (h.sdo == sdo) { if (sdo.state == SDO.SDO_STATE.SDO_ERROR) { lvi.SubItems[5].Text = " **ERROR **"; return; } //if (sdo.exp == true) { DataType meh = h.od.datatype; if (meh == DataType.UNKNOWN && h.od.parent != null) { meh = h.od.parent.datatype; } //item 5 is the read value item 4 is the actual value switch (meh) { case DataType.REAL32: float myFloat = System.BitConverter.ToSingle(BitConverter.GetBytes(h.sdo.expitideddata), 0); lvi.SubItems[5].Text = myFloat.ToString(); float fout; if (float.TryParse(lvi.SubItems[4].Text, out fout)) { if (fout != myFloat) { lvi.BackColor = Color.Red; } } break; case DataType.REAL64: double myDouble = System.BitConverter.ToDouble(h.sdo.databuffer, 0); lvi.SubItems[5].Text = myDouble.ToString(); break; case DataType.INTEGER8: case DataType.INTEGER16: case DataType.INTEGER32: case DataType.UNSIGNED8: case DataType.UNSIGNED16: case DataType.UNSIGNED32: int i1, i2; lvi.SubItems[5].Text = String.Format("{0}", h.sdo.expitideddata); if (int.TryParse(lvi.SubItems[5].Text, out i1) && int.TryParse(lvi.SubItems[4].Text, out i2)) { if (i1 != i2) { lvi.BackColor = Color.Red; } } break; case DataType.VISIBLE_STRING: lvi.SubItems[5].Text = System.Text.Encoding.UTF8.GetString(h.sdo.databuffer); break; case DataType.OCTET_STRING: StringBuilder sb = new StringBuilder(); foreach (byte b in h.sdo.databuffer) { sb.Append(string.Format("{0:x} ", b)); } lvi.SubItems[5].Text = sb.ToString(); break; case DataType.UNSIGNED64: { UInt64 data = (UInt64)System.BitConverter.ToUInt64(h.sdo.databuffer, 0); lvi.SubItems[5].Text = String.Format("{0:x}", data); } break; case DataType.INTEGER64: { Int64 data = (Int64)System.BitConverter.ToInt64(h.sdo.databuffer, 0); lvi.SubItems[5].Text = String.Format("{0:x}", data); } break; default: lvi.SubItems[5].Text = " **UNSUPPORTED **"; break; } } break; } h.od.actualvalue = lvi.SubItems[5].Text; } })); } catch (Exception e) { } return; }
public SDOAdapter(SDO target) { _target = target; }
private SDO dovalueupdate(sdocallbackhelper h, string sval) { DataType dt = h.od.datatype; if (dt == DataType.UNKNOWN && h.od.parent != null) { dt = h.od.parent.datatype; } SDO sdo = null; switch (dt) { case DataType.REAL32: { float val = (float)new SingleConverter().ConvertFromString(sval); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.REAL64: { double val = (double)new DoubleConverter().ConvertFromString(sval); byte[] payload = BitConverter.GetBytes(val); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, payload, upsucc); break; } case DataType.INTEGER8: { sbyte val = (sbyte)new SByteConverter().ConvertFromString(sval); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.INTEGER16: { Int16 val = (Int16) new Int16Converter().ConvertFromString(sval); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.INTEGER32: { Int32 val = (Int32) new Int32Converter().ConvertFromString(sval); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.UNSIGNED8: { byte val = (byte)new ByteConverter().ConvertFromString(sval); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.UNSIGNED16: { UInt16 val = (UInt16) new UInt16Converter().ConvertFromString(sval); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.UNSIGNED32: { UInt32 val = (UInt32) new UInt32Converter().ConvertFromString(sval); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, val, upsucc); break; } case DataType.INTEGER64: { Int64 val = (Int64) new Int64Converter().ConvertFromString(sval); byte[] payload = BitConverter.GetBytes(val); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, payload, upsucc); break; } case DataType.UNSIGNED64: { UInt64 val = (UInt64) new UInt64Converter().ConvertFromString(sval); byte[] payload = BitConverter.GetBytes(val); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, payload, upsucc); break; } case DataType.VISIBLE_STRING: { byte[] payload = Encoding.ASCII.GetBytes(sval); sdo = lco.SDOwrite((byte)numericUpDown_node.Value, (UInt16)h.od.Index, (byte)h.od.Subindex, payload, upsucc); break; } default: break; } return(sdo); }