protected override void OnInit(EventArgs e) { Key = Convert.ToInt32(Request["id"]); H5Div container = new H5Div(); container.CssClass = "container"; container.Attributes.Add("value", FunctionCode.ToString()); Gcontainer = container; Form.Controls.Add(container); RenderTitle(); H5Div work_follow_content = new H5Div(); work_follow_content.CssClass = "work_follow_content"; Gwork_follow_content = work_follow_content; Gcontainer.Controls.Add(work_follow_content); RenderRequestPerson(); RenderReasonBox(); RenderWorkFollow(); RenderFunction(); ComfirmReasonBox(); }
private static void addFunction(FunctionCode code, ArgumentBuilder arg, int flag) { var key = code.ToString(); if (Config.ICFunction) { key = key.ToUpper(); } funcDic.Add(key, new FunctionIdentifier(key, code, arg, flag)); }
private static void addFunction(FunctionCode code, AbstractInstruction inst, int additionalFlag) { var key = code.ToString(); if (Config.ICFunction) { key = key.ToUpper(); } funcDic.Add(key, new FunctionIdentifier(key, code, inst, additionalFlag)); }
public void OnAuthorization(AuthorizationFilterContext context) { var claimsIdentity = context.HttpContext.User.Identity as ClaimsIdentity; var permissionsClaim = context.HttpContext.User.Claims.SingleOrDefault(c => c.Type == SystemConstants.UserClaim.Permissions); if (permissionsClaim != null) { var permissions = JsonConvert.DeserializeObject <List <string> >(permissionsClaim.Value); var functionArr = _function.ToString().Split("_"); string functionId = string.Join(".", functionArr); if (!permissions.Contains(functionId + "_" + _action)) { context.Result = new ForbidResult(); } } else { context.Result = new ForbidResult(); } }
///<summary> /// 报文 ///</summary> ///<returns></returns> public override string Serialize() { //0xF1 10 00 00 01,41,00,00,脚本长度4字节小端,密码长度4字节小端 CRC // 包头 F1 // 功能码 10 // 子功能码 00 // 操作码 00 // 脚本长度4字节小端 01,41 // 密码长度4字节小端 00,00 // CRC校验 2bytes var data = new List <string> { Header, FunctionCode.ToString(), Convert.ToString(SubFunctionCode, 16), Convert.ToString(OperateCode, 16), "01", "41", "00", "00" }; var cData = new List <string>(); for (var i = 0; i < 4; i++) { cData.Add(Convert.ToString((byte)(DataLength >> i * 8 & 0xff), 16)); } data.AddRange(cData); cData.Clear(); for (var i = 0; i < 4; i++) { cData.Add(Convert.ToString((byte)(PwdLength >> i * 8 & 0xff), 16)); } data.AddRange(cData); var crc = CrcHelper.GetCrc16(data); data.AddRange(crc); return(data.Join(",")); }
/// <summary> /// Creates a new bvlc message based on a function code /// </summary> /// <param name="function">The function code of the message type to create</param> /// <returns>The newly created message instance</returns> private IBvlcMessage _createMessage(FunctionCode function) { switch (function) { case FunctionCode.Result: return(new ResultMessage()); case FunctionCode.RegisterForeignDevice: return(new RegisterForeignDeviceMessage()); case FunctionCode.ForwardedNpdu: return(new ForwardedNpduMessage()); case FunctionCode.OriginalUnicastNpdu: return(new OriginalUnicastNpduMessage()); case FunctionCode.OriginalBroadcastNpdu: return(new OriginalBroadcastNpduMessage()); } throw new Exception("Could not create message with function " + function.ToString()); }
public override void OnActionExecuting(ActionExecutingContext context) { byte[] value = null; if (context.HttpContext.Session.TryGetValue("functions", out value)) { var functions = (List <Function>)SerializeHelper.DeserializeWithBinary(value); if (!functions.Any(o => o.Code.Contains(_functionCode.ToString()))) { context.Result = new ContentResult() { Content = "请设置权限" }; context.HttpContext.Response.Redirect("/Home/Index"); } } else { context.Result = new ContentResult() { Content = "请设置权限" }; context.HttpContext.Response.Redirect("/Home/Index"); } }
private static void addPrintDataFunction(FunctionCode code) { addFunction(code, new PRINT_DATA_Instruction(code.ToString())); }
/// <summary> /// Creates a new bvlc message based on a function code /// </summary> /// <param name="function">The function code of the message type to create</param> /// <returns>The newly created message instance</returns> private IBvlcMessage _createMessage(FunctionCode function) { switch(function) { case FunctionCode.Result: return new ResultMessage(); case FunctionCode.RegisterForeignDevice: return new RegisterForeignDeviceMessage(); case FunctionCode.ForwardedNpdu: return new ForwardedNpduMessage(); case FunctionCode.OriginalUnicastNpdu: return new OriginalUnicastNpduMessage(); case FunctionCode.OriginalBroadcastNpdu: return new OriginalBroadcastNpduMessage(); } throw new Exception("Could not create message with function " + function.ToString()); }
private static void addPrintFunction(FunctionCode code) { addFunction(code, new PRINT_Instruction(code.ToString())); }
//画像表示 private static void addPictureFunction(FunctionCode code) { addFunction(code, new PICTURE_Instruction(code.ToString())); }
//音楽再生 private static void addMusicFunction(FunctionCode code) { addFunction(code, new MUSIC_Instruction(code.ToString())); }
private static void addFunction(FunctionCode code, ArgumentBuilder arg, int flag) { string key = code.ToString(); if (Config.ICFunction) key = key.ToUpper(); funcDic.Add(key, new FunctionIdentifier(key, code, arg, flag)); }
private static void addFunction(FunctionCode code, AbstractInstruction inst, int additionalFlag) { string key = code.ToString(); if (Config.ICFunction) key = key.ToUpper(); funcDic.Add(key, new FunctionIdentifier(key, code, inst, additionalFlag)); }
private void timer1_Tick(object sender, EventArgs e) { try { byte slaveID = 1; ushort startAddress = 0; ushort numofPoints = 4; //read DI(1xxxx) bool[] status = master.ReadInputs(slaveID, startAddress, numofPoints); for (int i = 0; i < numofPoints; i++) { if (status[i] == true) { listDI[i].BackColor = Color.DodgerBlue; } else { listDI[i].BackColor = Color.Navy; } } //read DO(0xxxx) bool[] coilstatus = master.ReadCoils(slaveID, startAddress, numofPoints); for (int i = 0; i < numofPoints; i++) { if (coilstatus[i] == true) { listDO[i].BackColor = Color.Red; } else { listDO[i].BackColor = Color.DarkRed; } } //read AI(3xxxx) ushort[] register = master.ReadInputRegisters(slaveID, startAddress, numofPoints); for (int i = 0; i < numofPoints; i++) { listAI[i].Text = register[i].ToString(); //If you need to show the value with other unit, you have to caculate the gain and offset //eq. 0 to 0kg, 32767 to 1000kg //0 (kg) = gain * 0 + offset //1000 (kg) = gain *32767 + offset //=> gain=1000/32767, offset=0 //double value = (double)register[i] * 10.0 / 32767; //listAI[i].Text = value.ToString("0.00"); } //read AO(4xxxx) ushort[] holdingregister = master.ReadHoldingRegisters(slaveID, startAddress, numofPoints); for (int i = 0; i < numofPoints; i++) { listAO[i].Text = holdingregister[i].ToString(); //If you need to show the value with other unit, you have to caculate the gain and offset //eq. 0 to 0 mA, 32767 to 20 mA //0 (mA) = gain * 0 + offset //20 (mA) = gain *32767 + offset //=> gain=20/32767, offset=0 //double holdvalue = (double)holdingregister[i] * 20.0 / 32767; //listAO[i].Text = holdvalue.ToString("0.00"); } } catch (Exception exception) { //Connection exception //No response from server. //The server maybe close the com port, or response timeout. if (exception.Source.Equals("System")) { Console.WriteLine(DateTime.Now.ToString() + " " + exception.Message); } //The server return error code. //You can get the function code and exception code. if (exception.Source.Equals("nModbusPC")) { string str = exception.Message; int FunctionCode; string ExceptionCode; str = str.Remove(0, str.IndexOf("\r\n") + 17); FunctionCode = Convert.ToInt16(str.Remove(str.IndexOf("\r\n"))); Console.WriteLine("Function Code: " + FunctionCode.ToString("X")); str = str.Remove(0, str.IndexOf("\r\n") + 17); ExceptionCode = str.Remove(str.IndexOf("-")); switch (ExceptionCode.Trim()) { case "1": Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!"); break; case "2": Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!"); break; case "3": Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!"); break; case "4": Console.WriteLine("Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!"); break; } /* * //Modbus exception codes definition * Code * Name * Meaning * 01 ILLEGAL FUNCTION The function code received in the query is not an allowable action for the server. * * 02 ILLEGAL DATA ADDRESS The data addrdss received in the query is not an allowable address for the server. * * 03 ILLEGAL DATA VALUE A value contained in the query data field is not an allowable value for the server. * * 04 SLAVE DEVICE FAILURE An unrecoverable error occurred while the server attempting to perform the requested action. * * 05 ACKNOWLEDGE This response is returned to prevent a timeout error from occurring in the client (or master) * when the server (or slave) needs a long duration of time to process accepted request. * * 06 SLAVE DEVICE BUSY The server (or slave) is engaged in processing a long–duration program command , and the * client (or master) should retransmit the message later when the server (or slave) is free. * * 08 MEMORY PARITY ERROR The server (or slave) attempted to read record file, but detected a parity error in the memory. * * 0A GATEWAY PATH UNAVAILABLE The gateway is misconfigured or overloaded. * * 0B GATEWAY TARGET DEVICE FAILED TO RESPOND No response was obtained from the target device. Usually means that the device is not present on the network. */ } } }
private void btnReadHoldingRegister_Click(object sender, EventArgs e) { if (!ModbusTool.bConnect) { MessageBox.Show("请先打开串口!"); return; } txtWrong.Text = ""; string strData = ""; try { ushort startAddress; try { startAddress = Convert.ToUInt16(txtStartAddr.Text, 16); } catch (Exception) { MessageBox.Show("请输入正确的起始地址!"); return; } ushort numofPoints; bool isValid = ushort.TryParse(txtAmount.Text, out numofPoints); if (isValid == false) { MessageBox.Show("请输入正确的数量!"); return; } ushort[] register = ModbusTool.ReadHoldingRegisters(startAddress, numofPoints); //for (int i = 0; i < numofPoints; i++) //{ // strData = strData + register[i].ToString() + " "; //} StringBuilder sb = new StringBuilder(); for (int i = 0; i < numofPoints; i++) { sb.Append(register[i].ToString("X4")); if ((i + 1) % 2 == 0) { sb.Append(" "); } } txtRead.Text = sb.ToString(); } catch (Exception ex) { //Connection exception //No response from server. //The server maybe close the com port, or response timeout. if (ex.Source.Equals("System")) { txtWrong.Text = DateTime.Now.ToString() + " " + ex.Message; } //The server return error code. //You can get the function code and exception code. if (ex.Source.Equals("Modbus")) { string str = ex.Message; int FunctionCode; string ExceptionCode; str = str.Remove(0, str.IndexOf("\r\n") + 17); FunctionCode = Convert.ToInt16(str.Remove(str.IndexOf("\r\n"))); Console.WriteLine("Function Code: " + FunctionCode.ToString("X")); str = str.Remove(0, str.IndexOf("\r\n") + 17); ExceptionCode = str.Remove(str.IndexOf("-")); switch (ExceptionCode.Trim()) { case "1": txtWrong.Text = "Exception Code: " + ExceptionCode.Trim() + "----> Illegal function!"; break; case "2": txtWrong.Text = "Exception Code: " + ExceptionCode.Trim() + "----> Illegal data address!"; break; case "3": txtWrong.Text = "Exception Code: " + ExceptionCode.Trim() + "----> Illegal data value!"; break; case "4": txtWrong.Text = "Exception Code: " + ExceptionCode.Trim() + "----> Slave device failure!"; break; default: txtWrong.Text = "Exception Code: " + ExceptionCode.Trim() + "----> Read failure!"; break; } } } }