/* * Status will be set to IDLE after valid frame is processed. */ private 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]]; value = 0; if (curr_conf[name].HasValue) { value = curr_conf[name].Value; } UInt32 multiplier = reg_mulitply_tbl[ifrm.data[j]]; valfresh[name] = true; curr_conf[name] = 256 * multiplier * (UInt32)(value / (256 * multiplier)) + multiplier * ifrm.data[j + 1] + (value % multiplier); } catch (System.Exception e) { Program.MsgShow(e.Message); } } status = NodeStatus.ST_IDLE; } #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 }
private void write_vregs(string[] names, UInt32[] values) { 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 outptr = 0; for (int i = 0; i < names.GetLength(0); 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); }
public SPort(string PortName, int BaudRate, Parity Parity, int DataBits, StopBits StopBit) { _serial = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBit); _serial.DataReceived += new SerialDataReceivedEventHandler(_serial_DataReceived); _serial.Handshake = Handshake.None; CmdToSend = new Queue <byte[]>(); InFrameHandlers = new Dictionary <byte, IOInFrameHandler>(); ifrm = new FrameBuffer(); ifrm.ResetFlag(); Status = PortStatus.CLOSED; }
//write to register with absolute address, for bootloader 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 outptr = 0; for (int i = 0; i < regaddr.GetLength(0); i++) { regbuffer[outptr] = regaddr[i]; valbuffer[outptr] = (byte)value[i]; outptr++; } ofrm.generate_write_frame(outbuffer, regbuffer, valbuffer, 0, (byte)(outptr)); port.AddVCommand(outbuffer); }
//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); }
/* * Generate read frame and add to buffer */ protected 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 outptr = 0; for (int i = 0; i < names.GetLength(0); 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); last_cmd = outbuffer; }
public SPort(string PortName, int BaudRate, Parity Parity, int DataBits, StopBits StopBit) { _serial = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBit); _serial.DataReceived +=new SerialDataReceivedEventHandler(_serial_DataReceived); _serial.Handshake = Handshake.None; CmdToSend = new Queue<byte[]>(); InFrameHandlers = new Dictionary<byte, IOInFrameHandler>(); ifrm = new FrameBuffer(); ifrm.ResetFlag(); Status = PortStatus.CLOSED; }
/* * Status will be set to IDLE after valid frame is processed. */ private 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]]; value = 0; if (curr_conf[name].HasValue) { value = curr_conf[name].Value; } UInt32 multiplier = reg_mulitply_tbl[ifrm.data[j]]; valfresh[name] = true; curr_conf[name] = 256 * multiplier * (UInt32)(value / (256 * multiplier)) + multiplier * ifrm.data[j + 1] + (value % multiplier); } catch (System.Exception e) { Program.MsgShow(e.Message); } } status = NodeStatus.ST_IDLE; } #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 }
private void write_vregs(string[] names, UInt32[] values) { 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 outptr = 0; for (int i = 0; i < names.GetLength(0); 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); }