public string SetSendDataStr(string message, int id) { string r = "无效操作"; //首先验证数据是否符合规范:解析数量、查看是否都是数字,id是否存在 var rows = YingHeXinList.Where(o => o.ID == id).ToList(); if (rows.Count > 0) { //查到有ID string[] sArray = message.Split(','); //这是当前想要发的数据 string Sstrs = PublicClassRule.GetValues(rows[0].HR_Make_ID.ToString(), "tcpSendBytes"); //配置中要发送数据类型[这是正确的数据] string[] sRightArray = Sstrs.Split(','); if (sArray.Length == sRightArray.Length) { //与配置项相符,再验证数据是否与要求类型相符 try { byte[] sendByteDataList = PublicClassRule.MakeDataToBytes(rows[0].HR_Make_ID, message); if (sendByteDataList == null) { //无效 r = "数据无效"; } else { //可以记录,发送 rows[0].SendData = message; rows[0].Web_Heart++;//web心跳 r = "发送数据变更成功"; } } catch (Exception) { r = "数据转换不正确,可能有的数据超过类型限制。"; } } else { r = "Message字符结构不正确,请正确输入正确数量的数据。"; } } else { r = "ID无效"; } return(r); }
/// <summary> /// 读取配置文件,输入输出都是什么数据。 /// </summary> /// <param name="HR_id"></param> /// <param name="tcpReceiveSendBytesName">tcpReceiveBytes/tcpSendBytes</param> /// <returns></returns> public static int Read_Setting_Length(int HR_id, string tcpReceiveSendBytesName) { string Rstrs = PublicClassRule.GetValues(HR_id.ToString(), tcpReceiveSendBytesName);//接收数据类型 string[] rArray = Rstrs.Split(','); int rLength = rArray.Length; //rLength = GetTCP_Receive_Length(rArray, rLength);//这个方法用错了!!留着启示后人 2019年12月13日17:02:03 就不应该用 return(rLength); }
/// <summary> /// 制作要发送的字节 /// </summary> /// <param name="HR_Make_ID"></param> /// <param name="sendData"></param> /// <returns></returns> internal static byte[] MakeDataToBytes(int HR_Make_ID, string sendData) { byte[] realByteOut = null; string Sstrs = PublicClassRule.GetValues(HR_Make_ID.ToString(), "tcpSendBytes"); //要发送数据类型 string[] sArray = Sstrs.Split(','); //这是数据类型数组 string[] dataArray = sendData.Split(','); //这是要发送的数据数组 List <byte[]> outBytesList = new List <byte[]>(); //这里最后要发送出去的byte[]组 //标注是否翻转,true不翻转 false翻转 bool isTCP_IEC = IsTCP_IEC(HR_Make_ID); if (sArray.Length == dataArray.Length) { //数据数量相同 #region 制作byte for (int i = 0; i < dataArray.Length; i++) { byte[] temp = MakeOneBytes(sArray[i], dataArray[i], isTCP_IEC); if (temp != null) { outBytesList.Add(temp); } else { } } //制作结束,封装开始。 realByteOut = PublicClassTools.MergeBytes(outBytesList); #endregion } else { //数据数量不同,不执行发送指令 } return(realByteOut); }
/// <summary> /// 启动simlink的过程 /// </summary> /// <param name="row"></param> /// <returns></returns> private async Task MakeSimlinkRun(ManagerMessage row) { await Task.Run(() => { //要运行的路径是row.CopyModel_To_TargetPath //文件夹里面的命令需要复制过来、你看看怎么能启动起来 #region 将来不会用这里的代码 2019年12月9日09:40:17 //System.Diagnostics.Process cmd_MakeRun = new System.Diagnostics.Process(); //cmd_MakeRun.StartInfo.FileName = @"D:\uuu\UID44\44_20191209114531New\MGtcp\hello.bat"; ////cmd_MakeRun.StartInfo.UserName = "******"; //cmd_MakeRun.StartInfo.UseShellExecute = false; //cmd_MakeRun.Start(); //row.PID = CMDClass.FindPID("MATLAB R2018a", @"C:\Program Files\MATLAB\R2018a\bin\win64\MATLAB.exe"); ////row.PID = cmd_MakeRun.Id;//把程序启动起来的PID记录下来 ////cmd_MakeRun.Kill(); //CMDClass.KillProcess("QQ", cmd_MakeRun.StartInfo.FileName);//两种方式都能把!QQ干掉!! ////CMDClass.KillProcess(row.PID); #endregion string arguments = PublicClassRule.GetValues(row.HR_Make_ID.ToString(), "arguments"); //原始参数 arguments = PublicClassRule.MakeArguments(arguments, row.HR_Make_ID, row.CopyModel_To_TargetPath); //结构化之后的最终使用参数 //修改M文件中的 input 的ID 本段属于文件操作,修改.m文件内容 string m_path = string.Format("{0}{1}", row.CopyModel_To_TargetPath, PublicClassRule.GetValues(row.HR_Make_ID.ToString(), "mFileName")); PublicClassRule.Edit_M_File(m_path, row.HR_Make_ID, row.ID, row.M_Setting_Arguments); MakeRun(arguments, ref row); //等待程序和服务TCP通讯,!!!注意,此时,这个进程不会停止,不会自动关闭。只能等待kill PID 2019年12月9日14:40:17 }); }
/// <summary> /// 数据解析:从byte[]到数据 /// </summary> /// <param name="recvDataBuffer"></param> /// <param name="HR_Make_ID"></param> /// <returns></returns> internal static List <double> MakebytesToData(byte[] recvDataBuffer, int HR_Make_ID) { List <double> r = new List <double>(); //int length = PublicClassTools.Read_Setting_Length(HR_Make_ID, "tcpReceiveBytes");//计算长度 string Rstrs = PublicClassRule.GetValues(HR_Make_ID.ToString(), "tcpReceiveBytes");//接收数据类型 string[] rArray = Rstrs.Split(','); int startIndex = 0; for (int i = 0; i < rArray.Length; i++) { //做数据 r.Add(MakeOneData(rArray[i], startIndex, recvDataBuffer)); //重置位置 startIndex = startIndex + PublicClassTools.GetTypeByteLength(rArray[i].Trim()); } return(r); }
/// <summary> /// 启动simlink模型 /// </summary> /// <param name="arguments">启动参数,这是启动MATLAB的名称参数,在配置文件的属性中可以看到DEMO</param> /// <param name="row">PID记录在这里</param> private void MakeRun(string arguments, ref ManagerMessage row) { try { //string arguments = @"-nosplash -nodesktop -sd D:\MyWorkGGTest\one\MGtcp -r test"; System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(PublicClassRule.GetValues("root", "MATLAB_Process_FullPath"), arguments); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; System.Diagnostics.Process myprocess = new System.Diagnostics.Process(); myprocess.StartInfo = startInfo; myprocess.StartInfo.UseShellExecute = false; myprocess.StartInfo.CreateNoWindow = true; myprocess.StartInfo.RedirectStandardOutput = true; myprocess.Start(); row.PID = myprocess.Id; //获取PID myprocess.WaitForExit(); //进程会一直处于等待退出状态。 } catch (Exception ex) { } }
/// <summary> /// 此方法提供单个试验配置的协议,提供给网站使用,便于动态创建页面 /// </summary> /// <param name="hrID"></param> /// <param name="xieyi"></param> /// <returns></returns> public string GetTag_By_HRID_And_XIEYI(int hrID, ShiYanXieYi xieyi) { return(PublicClassRule.GetValues(hrID.ToString(), xieyi.ToString()));//读取配置文件 }