private FXReadCommentResult ReadCommentInside() { FXReadCommentResult result = new FXReadCommentResult(); try { FXRegionInfoResult regionInfo = ReadParamRegionInside(); FXRegionInfo infoAddr = regionInfo.Items[(int)FXParamType.CommentRegisterFrom]; FXRegionInfo infoLen = regionInfo.Items[(int)FXParamType.CommentRegisterBlockLen]; int commentAddr = Convert.ToInt32(infoAddr.ValueStr, 16) + 0x8000; //获得注释起始地址 int commentLen = Convert.ToInt32(infoLen.ValueStr, 16) * 1000; //一块是1000字节 if (!this.mSerialPort.IsOpen) this.mSerialPort.Open(); int commentOffset = 0; List<byte[]> cmdList; List<byte[]> readResultList; while (true) { cmdList = this.CreateRegionCMDList(FXSeriesCmd.ReadProgram, commentAddr + commentOffset, COMMENTREADLEN, null); //每次读取20长度,直到读到第一个就是00结束 readResultList = this.mSerialPort.Read(cmdList); string aaa = BitConverter.ToString(readResultList[0]); if (readResultList[0][0] == (byte)FXSeriesControl.NAK) //如果设备返回NAK,那直接返回 { result.Msg = "device return NAK"; break; } string str = Utils.BytesToStrInRightOrder(false, readResultList[0]); if (str.StartsWith("00")) //如果读到的字符串以00开头,说明没有注释了,可以直接返回 break; FXCommentInfo comment = new FXCommentInfo(); //添加这个注释 comment.SetComment(str); result.Comments.Add(comment); commentOffset += COMMENTREADLEN; if (commentOffset + COMMENTREADLEN > commentLen) //如果已经超过注释区大小了,那么可以直接返回 break; } this.mSerialPort.Close(); } catch (System.Exception ex) { result.Msg = ex.Message; } return result; }
private string WriteCommentInside(FXCommentInfo[] infos) { string result = null; try { FXRegionInfoResult regionInfo = ReadParamRegionInside(); FXRegionInfo infoAddr = regionInfo.Items[(int)FXParamType.CommentRegisterFrom]; FXRegionInfo infoLen = regionInfo.Items[(int)FXParamType.CommentRegisterBlockLen]; int commentAddr = Convert.ToInt32(infoAddr.ValueStr, 16) + 0x8000; //获得注释起始地址 int commentLen = Convert.ToInt32(infoLen.ValueStr, 16) * 1000; //一块是1000字节 string[] dataStrArray = new string[commentLen / 2]; for (int i = 0; i < dataStrArray.Length; i++) //初始化注释区 dataStrArray[i] = "00"; if (infos != null) { for (int i = 0; i < infos.Length; i++) //在注释区中添加注释 { string[] temp = infos[i].GetCommentStrArray(); Array.Copy(temp, 0, dataStrArray, i * FXCommentInfo.CommentBytesLen, temp.Length); } } List<byte[]> cmdList = CreateRegionCMDList(FXSeriesCmd.WriteProgram, commentAddr, dataStrArray.Length, dataStrArray); //生成写入注释命令 List<byte[]> readResultList; if (!this.mSerialPort.IsOpen) this.mSerialPort.Open(); readResultList = this.mSerialPort.Read(cmdList); this.mSerialPort.Close(); } catch (System.Exception ex) { result = ex.Message; } return result; }
/// <summary> /// 这个方法在执行开始前打开串口,并负责在执行后关闭 /// </summary> /// <param name="infos"></param> /// <returns></returns> public string WriteComment(FXCommentInfo[] infos) { FXReadStatusResult status = ReadPLCStatus(); if (status.Msg == null) { if (status.Status == FXStatus.Run) RemoteControl(FXStatus.Stop); string msg = this.WriteCommentInside(infos); if (status.Status == FXStatus.Run) RemoteControl(FXStatus.Run); return msg; } else return status.Msg; }