/* * Status will be set to IDLE after valid frame is processed. */ public void HandleInFrame(FrameBuffer ifrm) { UInt32 value; if (ifrm.cmd == 'W') //write by bytes { for (int j = 0; j < ifrm.datalen; j = j + 2) { try { /* * just replace the specified 8 bits in register */ string name = reg_pos2name_tbl[ifrm.data[j]]; if (this[name].HasValue) { value = this[name].Value; } else { value = 0; } UInt32 multiplier = reg_mulitply_tbl[ifrm.data[j]]; this[name] = 256 * multiplier * (UInt32)(value / (256 * multiplier)) + multiplier * ifrm.data[j + 1] + (value % multiplier); } catch (System.Exception e) { Debug.WriteLine(e.Message); } } } /* #region obsolete_readword_code * * if (ifrm.cmd == 'X') * { * for (int j = 0; j < ifrm.framelen;j=j+3 ) * { * try * { * //queryEvent(ifrm.data[j], (UInt32)(ifrm.data[j + 1]) * 256 + (UInt32)(ifrm.data[j + 2])); * } * catch (System.Exception e) * { * Program.MsgBox(e.Message); * } * } * } #endregion **/ }
// Generate read frame and add to buffer public void read_regs(string[] names) { FrameBuffer ofrm = new FrameBuffer(); ofrm.cmd = (byte)'R'; ofrm.addr = node_id; byte[] regbuffer = new byte[16]; byte[] outbuffer = new byte[16]; int offset = 0; int step = SubNode.GROUPSIZE; while (offset < names.GetLength(0)) { int outptr = 0; int upper = Math.Min(offset + step, names.GetLength(0)); for (int i = offset; i < upper; i++) { if (reg_type_tbl[names[i]].size == 8) { regbuffer[outptr] = reg_type_tbl[names[i]].pos; outptr++; } if (reg_type_tbl[names[i]].size == 16) { regbuffer[outptr] = reg_type_tbl[names[i]].pos; regbuffer[outptr + 1] = (byte)(regbuffer[outptr] + 1); outptr = outptr + 2; } if (reg_type_tbl[names[i]].size == 32) { regbuffer[outptr] = reg_type_tbl[names[i]].pos; regbuffer[outptr + 1] = (byte)(regbuffer[outptr] + 1); regbuffer[outptr + 2] = (byte)(regbuffer[outptr] + 2); regbuffer[outptr + 3] = (byte)(regbuffer[outptr] + 3); outptr = outptr + 4; } } ofrm.generate_read_frame(outbuffer, regbuffer, 0, (byte)(outptr)); port.AddVCommand(outbuffer); offset = offset + step; } }
//write to group address public void writebyte_group_reg(byte group_addr, string[] regaddr, byte[] value) { FrameBuffer ofrm = new FrameBuffer(); ofrm.cmd = (byte)'W'; ofrm.addr = group_addr; byte[] regbuffer = new byte[16]; byte[] valbuffer = new byte[16]; byte[] outbuffer = new byte[32]; int outptr = 0; for (int i = 0; i < regaddr.GetLength(0); i++) { regbuffer[outptr] = reg_type_tbl[regaddr[i]].pos; valbuffer[outptr] = (byte)value[i]; outptr++; } ofrm.generate_write_frame(outbuffer, regbuffer, valbuffer, 0, (byte)(outptr)); port.AddVCommand(outbuffer); }
public SPort(string PortName, int BaudRate, Parity Parity, int DataBits, StopBits StopBit) { int hit = 20; while (hit-- > 0) { try { if (SerialPort.GetPortNames().Contains <string>(PortName)) { _serial = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBit); _serial.Handshake = Handshake.None; break; } } catch { Thread.Sleep(1000); } } if (hit <= 0) { throw new Exception("Failed to open port " + PortName); } //_serial.DataReceived +=new SerialDataReceivedEventHandler(_serial_DataReceived); // _serial.ErrorReceived += new SerialErrorReceivedEventHandler(_serial_ErrorReceived); // _serial.RtsEnable = false; ifrm = new FrameBuffer(); ifrm.ResetFlag(); Status = PortStatus.CLOSED; }
/* * Status will be set to IDLE after valid frame is processed. */ public void HandleInFrame(FrameBuffer ifrm) { UInt32 value; if (ifrm.cmd == 'W') //write by bytes { for (int j = 0; j < ifrm.datalen; j=j+2 ) { try { /* * just replace the specified 8 bits in register */ string name = reg_pos2name_tbl[ifrm.data[j]]; if (this[name].HasValue) value = this[name].Value; else value = 0; UInt32 multiplier = reg_mulitply_tbl[ifrm.data[j]]; this[name] = 256 * multiplier * (UInt32)(value / (256 * multiplier)) + multiplier * ifrm.data[j + 1] + (value % multiplier); } catch (System.Exception e) { Debug.WriteLine(e.Message); } } } /* #region obsolete_readword_code if (ifrm.cmd == 'X') { for (int j = 0; j < ifrm.framelen;j=j+3 ) { try { //queryEvent(ifrm.data[j], (UInt32)(ifrm.data[j + 1]) * 256 + (UInt32)(ifrm.data[j + 2])); } catch (System.Exception e) { Program.MsgBox(e.Message); } } } #endregion **/ }
//normal register write public void write_vregs(string[] names, UInt32[] values) { if (NodeAgent.IsDebug) { for (int i = 0;i< names.Length;i++) { if(curr_conf.ContainsKey(names[i])) curr_conf[names[i]] = values[i]; } return; } FrameBuffer ofrm = new FrameBuffer(); ofrm.cmd = (byte)'W'; if (!curr_conf["addr"].HasValue) { throw new Exception("Invalid node address"); } ofrm.addr = (byte)curr_conf["addr"].Value; byte[] regbuffer = new byte[16]; byte[] valbuffer = new byte[16]; byte[] outbuffer = new byte[32]; int offset = 0; int step = SubNode.GROUPSIZE; while (offset < names.GetLength(0)) { int outptr = 0; int upper = Math.Min(offset + step, names.GetLength(0)); for (int i = offset; i < upper; i++) { if (reg_type_tbl[names[i]].size == 8) { regbuffer[outptr] = reg_type_tbl[names[i]].pos; valbuffer[outptr] = (byte)values[i]; outptr++; } if (reg_type_tbl[names[i]].size == 16) { valbuffer[outptr] = (byte)(values[i] % 256); valbuffer[outptr + 1] = (byte)(values[i] / 256); regbuffer[outptr] = reg_type_tbl[names[i]].pos; regbuffer[outptr + 1] = (byte)(regbuffer[outptr] + 1); outptr = outptr + 2; } if (reg_type_tbl[names[i]].size == 32) { valbuffer[outptr + 0] = (byte)(values[i] % 256); values[i] = (UInt32)(values[i] / 256); valbuffer[outptr + 1] = (byte)(values[i] % 256); values[i] = (UInt32)(values[i] / 256); valbuffer[outptr + 2] = (byte)(values[i] % 256); values[i] = (UInt32)(values[i] / 256); valbuffer[outptr + 3] = (byte)(values[i] % 256); regbuffer[outptr] = reg_type_tbl[names[i]].pos; regbuffer[outptr + 1] = (byte)(regbuffer[outptr] + 1); regbuffer[outptr + 2] = (byte)(regbuffer[outptr] + 2); regbuffer[outptr + 3] = (byte)(regbuffer[outptr] + 3); outptr = outptr + 4; } } ofrm.generate_write_frame(outbuffer, regbuffer, valbuffer, 0, (byte)(outptr)); port.AddVCommand(outbuffer); offset = offset + step; } }
//write to register with absolute address, for bootloader and group command use public void writebyte_abs_reg(byte[] regaddr, byte[] value) { FrameBuffer ofrm = new FrameBuffer(); ofrm.cmd = (byte)'W'; ofrm.addr = this.node_id; byte[] regbuffer = new byte[16]; byte[] valbuffer = new byte[16]; byte[] outbuffer = new byte[32]; int offset = 0; int step = SubNode.GROUPSIZE; while (offset < regaddr.GetLength(0)) { int outptr = 0; int upper = Math.Min(offset + step, regaddr.GetLength(0)); for (int i = offset; i < upper; i++) { regbuffer[outptr] = regaddr[i]; valbuffer[outptr] = (byte)value[i]; outptr++; } ofrm.generate_write_frame(outbuffer, regbuffer, valbuffer, 0, (byte)(outptr)); port.AddVCommand(outbuffer); offset = offset + step; } }
//normal register write public void write_vregs(string[] names, UInt32[] values) { if (NodeAgent.IsDebug) { for (int i = 0; i < names.Length; i++) { if (curr_conf.ContainsKey(names[i])) { curr_conf[names[i]] = values[i]; } } return; } FrameBuffer ofrm = new FrameBuffer(); ofrm.cmd = (byte)'W'; if (!curr_conf["addr"].HasValue) { throw new Exception("Invalid node address"); } ofrm.addr = (byte)curr_conf["addr"].Value; byte[] regbuffer = new byte[16]; byte[] valbuffer = new byte[16]; byte[] outbuffer = new byte[32]; int offset = 0; int step = SubNode.GROUPSIZE; while (offset < names.GetLength(0)) { int outptr = 0; int upper = Math.Min(offset + step, names.GetLength(0)); for (int i = offset; i < upper; i++) { if (reg_type_tbl[names[i]].size == 8) { regbuffer[outptr] = reg_type_tbl[names[i]].pos; valbuffer[outptr] = (byte)values[i]; outptr++; } if (reg_type_tbl[names[i]].size == 16) { valbuffer[outptr] = (byte)(values[i] % 256); valbuffer[outptr + 1] = (byte)(values[i] / 256); regbuffer[outptr] = reg_type_tbl[names[i]].pos; regbuffer[outptr + 1] = (byte)(regbuffer[outptr] + 1); outptr = outptr + 2; } if (reg_type_tbl[names[i]].size == 32) { valbuffer[outptr + 0] = (byte)(values[i] % 256); values[i] = (UInt32)(values[i] / 256); valbuffer[outptr + 1] = (byte)(values[i] % 256); values[i] = (UInt32)(values[i] / 256); valbuffer[outptr + 2] = (byte)(values[i] % 256); values[i] = (UInt32)(values[i] / 256); valbuffer[outptr + 3] = (byte)(values[i] % 256); regbuffer[outptr] = reg_type_tbl[names[i]].pos; regbuffer[outptr + 1] = (byte)(regbuffer[outptr] + 1); regbuffer[outptr + 2] = (byte)(regbuffer[outptr] + 2); regbuffer[outptr + 3] = (byte)(regbuffer[outptr] + 3); outptr = outptr + 4; } } ofrm.generate_write_frame(outbuffer, regbuffer, valbuffer, 0, (byte)(outptr)); port.AddVCommand(outbuffer); offset = offset + step; } }
public SPort(string PortName, int BaudRate, Parity Parity, int DataBits, StopBits StopBit) { int hit = 20; while (hit-- > 0) { try { if (SerialPort.GetPortNames().Contains<string>(PortName)) { _serial = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBit); _serial.Handshake = Handshake.None; break; } } catch { Thread.Sleep(1000); } } if (hit <= 0) throw new Exception("Failed to open port "+PortName); //_serial.DataReceived +=new SerialDataReceivedEventHandler(_serial_DataReceived); // _serial.ErrorReceived += new SerialErrorReceivedEventHandler(_serial_ErrorReceived); // _serial.RtsEnable = false; ifrm = new FrameBuffer(); ifrm.ResetFlag(); Status = PortStatus.CLOSED; }