问题描述
privatevoidMeterSetDelayState(string[]sPara){csInterfacecsInter=newcsInterface();intiSum=int.Parse(sPara[4]);string[]strValue=newstring[iSum];for(intTi=1;Ti<=iSum;Ti++){strValue[Ti-1]=csInter.INIReadValue(sPara[2],"P"+Ti.ToString(),"",strFile485);}stringDelayValue="";objectArgSTR;stringDataValue="";DataValue=sPara[2]+"|"+strValue[1]+"|"+DelayValue;ArgSTR=(object)DataValue;////ThreadmyTD=newThread(newParameterizedThreadStart(ThreadSetDelayData));////myTD.Start(ArgSTR);////myTD.IsBackground=true;ThreadSetDelayData(ArgSTR);csInter.INIWriteValue(sPara[2],"OverTag","OK",strFile485);}privatevoidThreadSetDelayData(objectArgSTR){stringMydata=GetIdentityData();while(true){strResult="";for(intTi=1;Ti<=GroupBws;Ti++){intBwhID=ErrBoardNO*GroupBws+Ti;boolbTag=true;if(Bswitch!="A"){if(Bswitch.Substring(BwhID-1,1)=="0"){bTag=false;strResult=strResult+"N";}}if(bTag==true&&bOK[BwhID-1]==false){stringESAMSTR="";boolRtnOK=false;stringPassSTR="";stringDataValue="";stringMeterIDvalue=csInter.INIReadValue("485InitAddress",BwhID.ToString(),"",strFile485);bTag=GetESAMData(BwhID,MeterIDvalue,Mydata,outESAMSTR);if(MeterSendType==1){csInter.WaitTime(0.1);ESAMSTR=DLT645ReadDataResult(BwhID,outRtnOK);}if(RtnOK==true&&ESAMSTR.Length>=2*12){csInter.INIWriteValue("ESAM",BwhID.ToString(),ESAMSTR,strFile485);//SetText("ESAM"+"->"+BwhID.ToString()+"="+ESAMSTR,1,2);RtnOK=DLT6452007SetDelayStateParameter(BwhID,DelayValue,ESAMSTR,outPassSTR);csInter.WaitTime(0.1);if(RtnOK==true&&PassSTR.Length>=20*2){csInter.INIWriteValue("delaypassData",BwhID.ToString(),PassSTR,strFile485);//SetText("delayPassData->"+BwhID.ToString()+"="+PassSTR,1,0);}stringSafeDJ=csInter.INIReadValue("密文等级","dj","98",strFile485);if(PassSTR.Length>0){RtnOK=DLT6452007SetDelayState(BwhID,MeterIDvalue,PassData[BwhID-1].Substring(0,6),SafeDJ,PassSTR,outDataValue);if(MeterSendType==1){csInter.WaitTime(0.1);DataValue=DLT645ReadDataResult(BwhID,outRtnOK);}if(RtnOK==true){strResult=strResult+"S";bOK[BwhID-1]=true;csInter.INIWriteValue(Section,BwhID.ToString(),"1",strFile485);SetText(Section+BwhID.ToString()+"="+RtnOK.ToString(),1,0);}else{strResult=strResult+"F";}}}}elseif(bOK[BwhID-1]==true&&bTag==true){strResult=strResult+"S";}strReadOK=csInter.INIReadValue(Section,"OverTag","",strFileErr);WorkState=csInter.INIReadValue("system","workstate","",strFileErr);if(StopThread==true||WorkState!="1")//{ThreadOK=true;break;//主程序停止工作了,线程也要停止工作,目的防止线程没有停止}Application.DoEvents();}csInter.INIWriteValue(Section,"SetOkTag",strResult,strFile485);boolbAllOK=CheckIsOK(Bswitch,bOK);if(bAllOK==false)iCS=iCS+1;strReadOK=csInter.INIReadValue(Section,"OverTag","",strFileErr);WorkState=csInter.INIReadValue("system","workstate","",strFileErr);if(bAllOK==true||iCS>2||strReadOK.ToUpper()=="OK"||StopThread==true||WorkState!="1")//{ThreadOK=true;break;//主程序停止工作了,线程也要停止工作,目的防止线程没有停止}if(IsDebugTag==2)CheckSoftRunState();Application.DoEvents();}ThreadOK=true;NetStateTR.Enabled=true;}privatevoidThreadWriteLogData(objectArgMsg){lock(LockObject){char[]spitTag={'#'};string[]myPara=ArgMsg.ToString().Split(spitTag);csInterfacecsInter=newcsInterface();stringstrValue="";stringstrMsg=myPara[0];intiTag=int.Parse(myPara[1]);stringIsErrValue=myPara[2];stringstrTime=DateTime.Now.ToString("yyyy-MM-ddHH:mm:ssffff");if(iTag==0){strValue=strValue+"->Tx:";}elseif(iTag==1){strValue=strValue+"->Rx:";}strValue=strValue+strMsg;GiveSetText(strTime+strValue+"rn");csInter.MaxRow=csInter.MaxRow+1;if(csInter.MaxRow>=100){ShowMessage.Clear();csInter.MaxRow=0;}stringwtData=strTime+strValue;objectArgSTR=(object)wtData;//ThreadmyTD=newThread(newParameterizedThreadStart(WriteLogFile));//myTD.Start(ArgSTR);//myTD.IsBackground=true;WriteLogFile(ArgSTR);Application.DoEvents();}//WriteLogFile(strTime+strValue,IsErr);}privatevoidWriteLogFile(objectArgSTR){lock(LockObject){csInterfacecsInter=newcsInterface();//csInter.WaitTime(0.1);stringstrLogFile=Application.StartupPath+"\datalog\";if(Directory.Exists(strLogFile)==false){Directory.CreateDirectory(strLogFile);}strLogFile=strLogFile+"Err"+DateTime.Now.ToString("yyyyMMdd")+".log";csInter.WriteDataLog(strLogFile,ArgSTR.ToString()+"rn");Application.DoEvents();}}
解决方案
解决方案二:
///<summary>///stringSendByteArrToCOM发送命令到串口///boolIsFFCmdtrue是广播命令,false不是广播命令主要用于采用通过误差通道发送的时候使用///outstringoutputString返回数据内容///</summary>///<paramname="SendB"></param>///<paramname="IsFFCmd"></param>///<paramname="outputString"></param>///<returns></returns>privateboolSendByteArrToCOM(byte[]SendB,boolIsFFCmd,outstringoutputString){//try//{lock(LockObject)//加锁防止客户出现抢发命令{RevData=null;boolbTag=InitCOMparameter(strCOM,strBTL,strParity,strBit,strStop);csDLT6452007cs645=newcsDLT6452007();csErrAnd485cs485=newcsErrAnd485();csWirelesscsWL=newcsWireless();if(bTag==true){sptCOM.Write(SendB,0,SendB.Length);}else{outputString="";returnfalse;}bRevOK=false;stringstrData="";DateTimedtStart;DateTimedtEnd;TimeSpantsWait;dtStart=DateTime.Now;while(bRevOK==false){if(IsFFCmd==true)break;//广播命令不需要回答if(ErrBoardWireless==1&&IsErr==0){bRevOK=csWL.CheckRevDataCS(RevData);}elseif(MeterWireless==1&&(IsErr==1||IsErr==2)){bRevOK=csWL.CheckRevDataCS(RevData);}else{if(IsErr==1){bRevOK=cs645.CheckReceivedOK(RevData,0x16,0);}else{bRevOK=cs485.CheckReceivedOK(RevData,cs485.bEndNumber,0);}}dtEnd=DateTime.Now;tsWait=dtEnd.Subtract(dtStart);////SetText(tsWait.TotalSeconds.ToString(),1,0);if(tsWait.TotalSeconds>=1.5&&RevData==null)break;//若500毫秒没收到数据就不需再等待了if(tsWait.TotalSeconds>=1.5&&RevData.Length==0)break;//若500毫秒没收到数据就不需再等待了if(tsWait.TotalSeconds>2)break;//最多等待1.5秒钟//Application.DoEvents();}sptCOM.Close();//SetText("ihaverunhere",1,0);stringstrValue="";boolbTagJG=false;if(IsFFCmd==true)bTagJG=true;if(bRevOK==true){strData=cs485.GetByteArrToString(RevData);SetText(strData,1,IsErr);if(ErrBoardWireless==1&&IsErr==0){strData=csWL.ExplainData(strData);SetText(strData,1,IsErr);}elseif(MeterWireless==1&&(IsErr==1||IsErr==2)){strData=csWL.ExplainData(strData);SetText(strData,1,IsErr);}if(IsErr==1)//645命令直接从过串口发送返回的数据{bTagJG=cs645.ExplainDataFor645(strData,outstrValue);}elseif(IsErr==0)//误差板本身的命令{bTagJG=cs485.ExplainErrBoardData(strData,outstrValue);}elseif(IsErr==2)//通过误差板转发的645命令{bTagJG=cs485.ExplainErrBoardData(strData,outstrData);if(strData.Length>0&&bTagJG==true){bTagJG=cs645.ExplainDataFor645(strData,outstrValue);}else{bTagJG=false;}}}else{strData=cs485.GetByteArrToString(RevData);if(strData.Length>0)SetText(strData,1,IsErr);}outputString=strValue;returnbTagJG;}//}//catch(ExceptionErrMsg)//{//SetText("收到故障信息:"+ErrMsg.ToString(),1,0);//outputString="";//returnfalse;//}}privateboolInitCOMparameter(stringstrCOMName,stringstrBTL,stringstrJY,stringstrBit,stringstrStop){boolbTagOpen=false;//try//{sptCOM.PortName=strCOMName;sptCOM.BaudRate=int.Parse(strBTL);if(strJY.ToUpper()=="E"){sptCOM.Parity=Parity.Even;}elseif(strJY.ToUpper()=="O"){sptCOM.Parity=Parity.Odd;}elseif(strJY.ToUpper()=="N"){sptCOM.Parity=Parity.None;}elseif(strJY.ToUpper()=="S"){sptCOM.Parity=Parity.Space;}elseif(strJY.ToUpper()=="M"){sptCOM.Parity=Parity.Mark;}if(strStop=="0"){sptCOM.StopBits=StopBits.None;}elseif(strStop=="1"){sptCOM.StopBits=StopBits.One;}elseif(strStop=="1.5"){sptCOM.StopBits=StopBits.OnePointFive;}elseif(strStop=="2"){sptCOM.StopBits=StopBits.Two;}sptCOM.DataBits=int.Parse(strBit);if(sptCOM.IsOpen==true)sptCOM.Close();sptCOM.Open();if(sptCOM.IsOpen==true)bTagOpen=true;//}//catch(Exceptionee)//{//sptCOM.Close();//SetText("故障信息:"+ee.Message,1,0);//bTagOpen=false;//}returnbTagOpen;}
解决方案三:
问题现象是:向串口发送数据后,串口无回答数据,程序死机,程序死机又没有让系统死机,该程序占用CPU是0,点击代码任意处设置断点后,VS代码编辑器鼠标后面无响应了,只能关闭程序界面的"X"后,vs代码编辑器又可以响应了,请高手帮我分析是啥原因
解决方案四:
太长,你检查一下是否是死循环
解决方案五:
3楼的,谢谢,没有死循环问题,如果是死循环的话,cpu就是不是0
解决方案六:
看看是否有死锁
解决方案七:
5楼,谢谢,您好!我使用了lock,应该是不会有问题的。问题是发送数据到串口后,串口响应了一会儿后,后面就没响应了,数据也没有回答。
解决方案八:
串口通信没有问题吗?数据是否有发出去或接收数据?