IECComm.c 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600
  1. /********************************************************************
  2. 版权所有:
  3. 文件版本: V1.00
  4. 文件名称: IECComm.c
  5. 生成日期: 2007年04月26日
  6. 作 者:王建军
  7. 使用范围:
  8. 功 能: 外部通信处理函数
  9. 更新信息:
  10. 更新日志1:
  11. 修改者:
  12. 修改日期:
  13. 修改内容:
  14. 修改原因:
  15. *********************************************************************/
  16. #include "head.h"
  17. #ifdef LINUX_KERNEL_APP
  18. #include "uart_user.h"
  19. #include <linux/delay.h>
  20. #else
  21. #include <sys/prctl.h>
  22. #include <errno.h>
  23. #endif
  24. u8 rmt_str_cnt=0;
  25. para_chg_queque g_para_chg_queque[COMM_CHANNEL_NUM];
  26. DWORD dTpartime;
  27. //#define SEC_BOX_CODE 1
  28. bool g_bCommLed; //通信灯状态
  29. #ifdef COMM_STATUS_LED
  30. bool g_bCommStatus;
  31. #endif
  32. u32 g_comm_link_status; //通信连接状态,每个通道用一个bit表示
  33. bool byc_filter; //遥测滤波,防止遥测没有稳定下来突发上送
  34. BYTE g_arrIECBuf[IEC_TBUF_LEN]; // 通信规约处理时使用的数据缓冲区
  35. SERIAL_COMM g_tRsComm[CFG_UART_NUM_MAX]; // 4个串行通信口的通信结构定义
  36. union u_RsIEC g_tRsIEC[CFG_UART_NUM_MAX]; // 串口规约共用结构buf区,目前有101 104规约
  37. #ifdef FUN_FUXI_ESAM
  38. s8 UART_CHANNEL[CFG_UART_NUM_MAX]={CFG_UART_232_0,CFG_UART_232_1,CFG_UART_HMI,CFG_UART_485_0,CFG_UART_485_1, CFG_UART_485_2,CFG_UART_CHIP_S1};
  39. #else
  40. // s8 UART_CHANNEL[CFG_UART_NUM_MAX]={CFG_UART_232_0,CFG_UART_232_1,CFG_UART_HMI,CFG_UART_485_0,CFG_UART_485_1, CFG_UART_485_2};
  41. s8 UART_CHANNEL[CFG_UART_NUM_MAX]={6,4,13,11,8,3,14,9,5,1,15}; //临时调试设置
  42. #endif
  43. u8 uart_test_begin=0;
  44. u8 uart_test_flag[CFG_UART_NUM_MAX][2];
  45. //定义预置/固化参数时用于比较的数组及变量
  46. BYTE buf_forCompare[256]={0}; //预置报文和固化报文的比较存储数组
  47. int i_forCompare=0,j_forCompare=0; //比较数组存储时计数用
  48. bool bfirst_sel=false; //预置标志
  49. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  50. bool gb_bready = true;
  51. #endif
  52. #ifdef CPU_FUXI
  53. int g_uart_isr_thread_create_flag[CFG_UART_NUM_MAX] = {0};
  54. static struct task_struct * ts[CFG_UART_NUM_MAX];
  55. static struct sched_param sp[CFG_UART_NUM_MAX];
  56. static int l_ch[CFG_UART_NUM_MAX];
  57. #endif
  58. int g_link_uart_chnl=-1; //级联串口号
  59. static void IEC_Recv(BYTE cDat,BYTE chnl);
  60. #ifdef FUN_GPRS // modfiy for xxxxxx 20220706 GPRS没有使用。
  61. extern void (*g_IEC_Recv)(unsigned char cDat, unsigned char chnl);
  62. extern int gprs_net_init(void);
  63. extern int g_gh_vs_is_idle;
  64. extern int g_update_dz;
  65. #else
  66. //接收数据处理回调函数
  67. void (*g_IEC_Recv)(unsigned char cDat, unsigned char chnl);
  68. #endif
  69. #ifdef LINUX_USER_APP
  70. static int l_ch[CFG_UART_NUM_MAX];
  71. static pthread_t l_id[CFG_UART_NUM_MAX];
  72. extern int main_mod_is_exit(void);
  73. extern void mainloop_wakeup(void);
  74. #endif
  75. #ifdef CPU_FUXI
  76. int uart_isr_thread_create(int ch);
  77. extern int main_mod_is_exit(void);
  78. #endif
  79. extern void FaultComm_Init(IEC101_DEF *pt,BYTE chnl);
  80. extern void FaultComm_Applay(IEC101_DEF *pt);
  81. extern void FaultComm_Recv(IEC101_DEF *pt,BYTE byRevData) ;
  82. #define CFG_LINK_EQU_SOE_NUM 10 // 级联装置保存SOE数量
  83. u8 g_ti_select[]=
  84. {
  85. 0x2d, // 单点命令(45)
  86. 0x2e, // 双点命令(46)
  87. 0xcb, // 写参数和定值(203) 报文格式不同,若需要,应特殊处理
  88. 0xd3, // 软件升级(211)
  89. };
  90. u8 TI_SELECT_NUM= (sizeof(g_ti_select));
  91. // 文件,信息体对应表
  92. typedef struct DI_DIR
  93. {
  94. DWORD di; //485 波特
  95. char * dir; // 串口校验方式无奇偶校验等
  96. }DI_DIR;
  97. DI_DIR di_dir[]=
  98. {
  99. {0x0000, " "},
  100. {0x6804, HF_SOE_101_DIR}, // 6804
  101. {0x6805, HF_CO_101_DIR},
  102. {0x0cea, HF_EXV_101_DIR}, // 6802
  103. {0x6805, HF_FIXPT_101_DIR},
  104. {0x680A, HF_WAVE_DIR},
  105. };
  106. const int DI_DIR_NUMBER = sizeof( di_dir ) / sizeof(di_dir[0]); // 个数
  107. #ifdef GEN_QUEUE
  108. s32 queue_init(QTYPE *queque, s32 maxsize,void *data,s32 s_size)
  109. {
  110. queque->count = 0;
  111. queque->front = 0;
  112. queque->rear = 0;
  113. queque->maxsize = maxsize;
  114. queque->s_size = s_size;
  115. queque->data = data; /*元素实体*/
  116. return 0;
  117. }
  118. s32 get_queue_count(QTYPE *queue)
  119. {
  120. return queue->count;
  121. }
  122. /*函数功能说明:队列操作的通用写接口*/
  123. s32 add_data_queque(QTYPE *queue,u8 *data, u8 op_type)
  124. {
  125. s32 rear=0;
  126. u32 pos=0;
  127. if((queue->count >= queue->maxsize) && (op_type == KEEP_QUEUE)) /*队列满,保持原数据*/
  128. {
  129. return -1;
  130. }
  131. rear = queue->rear;
  132. pos = rear*queue->s_size;
  133. memcpy(&queue->data[pos], data, queue->s_size);
  134. queue->rear = (queue->rear+1)%queue->maxsize;
  135. queue->count ++;;
  136. /*************/
  137. if((queue->count > queue->maxsize)&& (op_type == OVER_QUEUE))
  138. {
  139. queue->count = queue->maxsize;
  140. queue->front = (queue->front+1)%queue->maxsize;
  141. }
  142. /*************/
  143. return 0;
  144. }
  145. /*队列操作的通用读接口*/
  146. s32 get_data_queque(QTYPE *queue,u8 *data)
  147. {
  148. u32 pos=0;
  149. if(queue->count == 0) return -1; /*无数据*/
  150. pos = queue->front * queue->s_size;
  151. memcpy(data, &queue->data[pos], queue->s_size);
  152. queue->front = (queue->front+1)%queue->maxsize;
  153. queue->count--;
  154. return 0;
  155. }
  156. /*队列操作的通用检查接口*/
  157. s32 check_data_queque(QTYPE *queue,u8 *data)
  158. {
  159. u32 pos=0;
  160. if(queue->count == 0) return -1; /*无数据*/
  161. pos = queue->front * queue->s_size;
  162. memcpy(data, &queue->data[pos], queue->s_size);
  163. return 0;
  164. }
  165. /*队列操作的通用减接口*/
  166. void del_data_queue(QTYPE *queue)
  167. {
  168. if(queue->count > 0)
  169. {
  170. queue->front = (queue->front+1)%queue->maxsize;
  171. queue->count--;
  172. }
  173. }
  174. s32 print_queque(QTYPE *queue,u8 type)
  175. {
  176. s32 i=0;
  177. s32 pos=0;
  178. s32 ret=0;
  179. s32 front=0;
  180. rt_printf("queue------count=%d,s_size=%d,maxsize=%d,front=%d,rear=%d\r\n",(int)queue->count,(int)queue->s_size
  181. ,(int)queue->maxsize,(int)queue->front,(int)queue->rear);
  182. for(i=0;i<queue->count;i++)
  183. {
  184. front = (queue->front+i)%queue->maxsize;
  185. pos = front*queue->s_size;
  186. if(type == RCD_PRINT)
  187. {
  188. newrcd_m_struct *data;
  189. data = (newrcd_m_struct *)&queue->data[pos];
  190. rt_printf("---data[%x][%x]:%s,addr=%d\r\n",(int)front,(int)pos,data->filename,data->addr);
  191. }
  192. else if(type == PARA_PRINT)
  193. {
  194. para_chg_single *data;
  195. data = (para_chg_single *)&queue->data[pos];
  196. rt_printf("---data[%x][%x]:%f,parId=%x\r\n",(int)front,(int)pos,data->val,data->parId);
  197. }
  198. }
  199. return ret;
  200. }
  201. #endif
  202. #ifdef RCD_STRAN_S
  203. void iec_commadd_rcd(char *filename)
  204. {
  205. int i=0;
  206. char data[128]={0};
  207. IEC101_DEF *pt101;
  208. IEC104_DEF *pt104;
  209. for(i=0;i<CFG_UART_NUM_MAX;i++)
  210. {
  211. if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_101) // 串口级联规约
  212. {
  213. pt101 = (IEC101_DEF *)g_tRsComm[i].ptBuf;
  214. strncpy(data,filename,sizeof(data));
  215. add_data_queque(&pt101->rcd_qt.queue,data,OVER_QUEUE);
  216. }
  217. }
  218. for(i=0;i<CFG_ETH_MAX_LOGIC;i++)
  219. {
  220. pt104=&g_t104[i];
  221. strncpy(data,filename,sizeof(data));
  222. add_data_queque(&pt104->rcd_qt.queue,data,OVER_QUEUE);
  223. }
  224. }
  225. #endif
  226. char * IEC_Getdir(DWORD di)
  227. {
  228. int i;
  229. for(i=0;i<DI_DIR_NUMBER;i++)
  230. {
  231. if(di==di_dir[i].di)return di_dir[i].dir;
  232. }
  233. return di_dir[0].dir;
  234. }
  235. #ifdef BSP_DTU2
  236. void RS_Recv_Enable(BYTE chnl)
  237. {
  238. switch(UART_CHANNEL[chnl])
  239. {
  240. case CFG_UART_485_0:
  241. GPIO_R485_OE5_OFF();
  242. break;
  243. case CFG_UART_485_1:
  244. GPIO_R485_OE6_OFF();
  245. break;
  246. case CFG_UART_HMI:
  247. GPIO_R485_OE1_OFF();
  248. break;
  249. default:
  250. break;
  251. }
  252. }
  253. void RS_Send_Enable(BYTE chnl)
  254. {
  255. switch(UART_CHANNEL[chnl])
  256. {
  257. case CFG_UART_485_0:
  258. GPIO_R485_OE5_ON();
  259. break;
  260. case CFG_UART_485_1:
  261. GPIO_R485_OE6_ON();
  262. break;
  263. case CFG_UART_HMI:
  264. GPIO_R485_OE1_ON();
  265. break;
  266. default:
  267. break;
  268. }
  269. }
  270. void RS_Ena_Init(BYTE chnl)
  271. {
  272. switch(UART_CHANNEL[chnl])
  273. {
  274. case CFG_UART_485_0:
  275. GPIO_R485_OE5_INIT();
  276. break;
  277. case CFG_UART_485_1:
  278. GPIO_R485_OE6_INIT();
  279. break;
  280. case CFG_UART_HMI:
  281. GPIO_R485_OE1_INIT();
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. #endif
  288. bool getCommStatus(void)
  289. {
  290. int i;
  291. for(i=0;i<CFG_UART_NUM_MAX+CFG_ETH_NUM_MAX;i++)
  292. {
  293. if(soe_check(EV_COMM1_ERR+i))
  294. return true;
  295. }
  296. return false;
  297. }
  298. //获取101级联通信状态
  299. bool getIecLink101Status(void)
  300. {
  301. int i;
  302. for(i=EV_LINK_EQU1;i<EV_LINK_EQU10;i++)
  303. {
  304. if( soe_check(i))
  305. return true;
  306. }
  307. return false;
  308. }
  309. /**************************************************************************
  310. 函数名称:IEC_CommInit
  311. 函数版本:1.00
  312. 作者:
  313. 创建日期:2008.9.1
  314. 函数功能说明:外部通信初始化
  315. 输入参数:
  316. 输出参数:
  317. 返回值:
  318. 更新信息:
  319. 更新日志1:
  320. 日期:
  321. 修改者:
  322. 修改内容:
  323. 修改原因:
  324. ***************************************************************************/
  325. void IEC_CommInit(void) // 外部通信初始化
  326. {
  327. int i, ret;
  328. s_stat_reset(1);//初始串口统计信息
  329. //相关通道规约应用初始化
  330. g_link_uart_chnl=-1;
  331. for(i=0;i<CFG_UART_NUM_MAX;i++)
  332. {
  333. // 不管硬件通道是否存在,都将参数设置放入串口统计信息中
  334. s_stat_protocol(i, (int)tRunPara.tUartPara[i].wProtocol); //统计规约
  335. // 如果硬件通道不存在,跳过
  336. if(UART_CHANNEL[i]<0)continue;
  337. // 如果没有初始化标志,意味着设置没有更新,不重新初始化
  338. if(!tRunPara.tUartPara[i].bInit)continue;
  339. tRunPara.tUartPara[i].bInit=false; //清初始化标志
  340. memset(&g_tRsComm[i],0,sizeof(g_tRsComm[i]));
  341. g_tRsComm[i].ptBuf=(BYTE*)&g_tRsIEC[i];
  342. if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_101
  343. ||tRunPara.tUartPara[i].wProtocol==PROTOCOL_101_PH
  344. ||tRunPara.tUartPara[i].wProtocol == PROTOCOL_MAINTAIN
  345. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  346. || tRunPara.tUartPara[i].wProtocol == PROTOCOL_WED_ENC
  347. #endif
  348. ) // 103
  349. {
  350. IEC101_Init((IEC101_DEF *)g_tRsComm[i].ptBuf,i);
  351. }
  352. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_101_M||tRunPara.tUartPara[i].wProtocol==PROTOCOL_101_PH_M) // 101级联
  353. {
  354. lnk_101_init((IECLINK_DEF *)g_tRsComm[i].ptBuf,i,(tRunPara.tUartPara[i].wProtocol==PROTOCOL_101_PH_M));
  355. }
  356. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_104) // 104规约
  357. {
  358. IEC104_Init_RS((IEC104_DEF *)g_tRsComm[i].ptBuf,i);
  359. }
  360. else if(tRunPara.tUartPara[i].wProtocol == PROTOCOL_HMI) // 面板规约
  361. {
  362. uart_comm_init((UART_COMM_STRUCT *)g_tRsComm[i].ptBuf, i);
  363. }
  364. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_FAULT) // 状态板通信
  365. {
  366. FaultComm_Init((IEC101_DEF *)g_tRsComm[i].ptBuf,i);
  367. }
  368. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_SXYB) //数显仪表
  369. {
  370. sxyb_init();
  371. }
  372. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_MODBUS) // modbus主站规约
  373. {
  374. modbus_Init((MODBUS_DEF *)g_tRsComm[i].ptBuf,i);
  375. }
  376. #if defined(ADD_ATGM366_GPS_FUN) || defined(DEV_GPS_ATGM366) || defined(DEV_GPS_ATGM332D)
  377. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_GPS) // GPS模块
  378. {
  379. GPSCommInit((GPS_COMM *)g_tRsComm[i].ptBuf,i);
  380. }
  381. #endif
  382. #ifdef BATTERY_WITH_COMM
  383. else if(tRunPara.tUartPara[i].wProtocol == PROTOCOL_POWER_MOD){
  384. PowerModuleCommInit((COMM_POW_MOD*)g_tRsComm[i].ptBuf,i);
  385. }
  386. #ifdef FUNC_SEL_BAT_MODULE
  387. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_PWRM)
  388. {
  389. pwrm_Init((pwdmodlue_def *)g_tRsComm[i].ptBuf,i);
  390. }
  391. #endif
  392. #endif
  393. #ifdef FUN_GPRS // modfiy for xxxxxx 20220706 GPRS没有使用。
  394. if(UART_CHANNEL[i] == CFG_UART_GPRS_IN)
  395. {
  396. g_gh_vs_is_idle = 1;//置为空闲.如果不空闲,后续代码会改变此变量的值.
  397. }
  398. #endif
  399. // 如果此串口不是系统控制台串口,协议也不是空闲,初始化串口
  400. if(UART_CHANNEL[i] != g_con_uart_index && tRunPara.tUartPara[i].wProtocol != PROTOCOL_IDLE)
  401. {
  402. #ifdef FUN_GPRS // modfiy for xxxxxx 20220706 GPRS没有使用。
  403. if(UART_CHANNEL[i] == CFG_UART_GPRS_IN)
  404. {
  405. g_gh_vs_is_idle = 0; //置为不空闲
  406. continue;
  407. }
  408. #endif
  409. #ifdef FUN_FUXI_ESAM
  410. else if(UART_CHANNEL[i] == CFG_UART_CHIP_S1)
  411. {
  412. continue;
  413. }
  414. #endif
  415. // 如果GPS对时打开,必须关闭串口。
  416. if(g_clock_mode_gps && UART_CHANNEL[i] == CFG_UART_GPS)
  417. {
  418. continue;
  419. }
  420. #ifdef IECCOM_DEBUG
  421. rt_printf("UART_CHANNEL[%d]=%d,tRunPara.tUartPara[%d].wProtocol=%d,tRunPara.tUartPara[%d].wBaud=%d tUartPara[%d].wParity=%d\r\n",\
  422. i,UART_CHANNEL[i],i,tRunPara.tUartPara[i].wProtocol,i,tRunPara.tUartPara[i].wBaud,i,tRunPara.tUartPara[i].wParity);
  423. #endif
  424. ret = uart_open(UART_CHANNEL[i],tRunPara.tUartPara[i].wBaud,tRunPara.tUartPara[i].wParity);
  425. if (ret < 0)
  426. {
  427. printf("%s: outside uart %d open failed!\n", __FUNCTION__, UART_CHANNEL[i]);
  428. }
  429. else
  430. {
  431. printf("%s: outside uart %d open success!\n", __FUNCTION__, UART_CHANNEL[i]);
  432. }
  433. RS_Ena_Init(i);
  434. RS_Recv_Enable(i);
  435. #if defined CPU_FUXI
  436. uart_isr_thread_create(i);
  437. #endif
  438. }
  439. //串口打开后,再初始化
  440. if(tRunPara.tUartPara[i].wProtocol == PROTOCOL_HMI) // 面板规约
  441. {
  442. g_h_boj[0].fd = UART_CHANNEL[i];
  443. user_conf_init(); //用户配置初始化(面板)
  444. }
  445. }
  446. //回调函数
  447. g_IEC_Recv=IEC_Recv;
  448. #ifdef FUN_GPRS // modfiy for xxxxxx 20220706 GPRS没有使用。
  449. g_update_dz = 1;//置标志,要求任务重新初始化虚拟串口
  450. #endif
  451. //内部GPRS初始化
  452. //gprs_net_init();
  453. }
  454. /**************************************************************************
  455. 函数名称:IEC_Recv
  456. 函数版本:1.00
  457. 作者:
  458. 创建日期:2008.9.1
  459. 函数功能说明:通信口接收处理,根据不同的规约类型,调用不同的接收函数
  460. 输入参数:
  461. 输出参数:
  462. 返回值:
  463. 更新信息:
  464. 更新日志1:
  465. 日期:
  466. 修改者:
  467. 修改内容:
  468. 修改原因:
  469. ***************************************************************************/
  470. static void IEC_Recv(BYTE cDat,BYTE chnl)
  471. {
  472. if(tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_101
  473. ||tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_101_PH
  474. ||tRunPara.tUartPara[chnl].wProtocol == PROTOCOL_MAINTAIN
  475. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  476. || tRunPara.tUartPara[chnl].wProtocol == PROTOCOL_WED_ENC
  477. #endif
  478. ) // 101规约
  479. {
  480. IEC101_Recv_Int((IEC101_DEF *)g_tRsComm[chnl].ptBuf,cDat);
  481. }
  482. else if(tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_101_M||tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_101_PH_M) // 级联
  483. {
  484. lnk_101_recv((IECLINK_DEF *)g_tRsComm[chnl].ptBuf,cDat);
  485. }
  486. else if(tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_104) //104规约
  487. {
  488. IEC104_Recv((IEC104_DEF *)g_tRsComm[chnl].ptBuf,cDat);
  489. }
  490. else if(tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_FAULT) // 状态板
  491. {
  492. FaultComm_Recv((IEC101_DEF *)g_tRsComm[chnl].ptBuf,cDat);
  493. }
  494. else if(tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_MODBUS) //modbus主站规约
  495. {
  496. modbus_Recv((MODBUS_DEF *)g_tRsComm[chnl].ptBuf,cDat);
  497. }
  498. else if(tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_GPS) //GPS模块
  499. {
  500. GPS_Recv((GPS_COMM *)g_tRsComm[chnl].ptBuf,cDat);
  501. }
  502. #ifdef BATTERY_WITH_COMM
  503. else if(tRunPara.tUartPara[chnl].wProtocol == PROTOCOL_POWER_MOD){
  504. PowerModuleCommRecv((COMM_POW_MOD*)g_tRsComm[chnl].ptBuf,cDat);
  505. }
  506. #ifdef FUNC_SEL_BAT_MODULE
  507. else if(tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_PWRM) //电源模块规约
  508. {
  509. pwrm_Recv((pwdmodlue_def *)g_tRsComm[chnl].ptBuf,cDat);
  510. }
  511. #endif
  512. #endif
  513. }
  514. #ifdef FUN_FUXI_ESAM
  515. void commtask_recv(u8 chnl)
  516. {
  517. int ret=0,i=0;
  518. BYTE buf[256];
  519. s1_process();
  520. ret = read_chip_s1(buf,256);
  521. if(ret > 0) /*收到报文收据*/
  522. {
  523. //rt_printf("s1_tclrecv:::");
  524. for(i=0;i<ret;i++)
  525. {
  526. if(tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_104) //104规约
  527. {
  528. //rt_printf("[%02x] ",buf[i]);
  529. IEC104_Recv_Int((IEC104_DEF *)g_tRsComm[chnl].ptBuf,buf[i]);
  530. }
  531. else if(tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_101
  532. ||tRunPara.tUartPara[chnl].wProtocol==PROTOCOL_101_PH
  533. )
  534. {
  535. IEC101_Recv_Int((IEC101_DEF *)g_tRsComm[chnl].ptBuf,buf[i]);
  536. }
  537. }
  538. //rt_printf("\r\n");
  539. }
  540. }
  541. #endif
  542. /**************************************************************************
  543. 函数名称:IEC_485_Task
  544. 函数版本:1.00
  545. 作者:
  546. 创建日期:2008.9.1
  547. 函数功能说明:RS485接受处理任务
  548. 输入参数:
  549. 输出参数:
  550. 返回值:
  551. 更新信息:
  552. 更新日志1:
  553. 日期:
  554. 修改者:
  555. 修改内容:
  556. 修改原因:
  557. ***************************************************************************/
  558. void IECCommTask(void)
  559. {
  560. int i,equNum;
  561. int commErrTab[CFG_UART_NUM_MAX];
  562. //int equErrTab[]={true};
  563. // 串口规约处理
  564. #ifdef COMM_STATUS_LED//通信状态灯
  565. g_bCommStatus=FALSE;
  566. #endif
  567. for(i=0;i<CFG_UART_NUM_MAX;i++)
  568. {
  569. // 如果串口不存在,跳过
  570. if(UART_CHANNEL[i]< 0 )
  571. {
  572. continue;
  573. }
  574. #ifdef FUN_FUXI_ESAM
  575. else if(UART_CHANNEL[i] == CFG_UART_CHIP_S1)
  576. {
  577. commtask_recv(i);
  578. }
  579. #endif
  580. commErrTab[i] = true;//初始化通信端口状态为异常
  581. //外部通信报文处理
  582. if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_101
  583. ||tRunPara.tUartPara[i].wProtocol==PROTOCOL_101_PH
  584. ||tRunPara.tUartPara[i].wProtocol == PROTOCOL_MAINTAIN
  585. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  586. ||tRunPara.tUartPara[i].wProtocol == PROTOCOL_WED_ENC
  587. #endif
  588. ) // 101规约
  589. {
  590. IEC101_DEF *pt;
  591. IEC101_Applay((IEC101_DEF *)g_tRsComm[i].ptBuf);
  592. IEC101_AutoTimer((IEC101_DEF *)g_tRsComm[i].ptBuf); //101 规约定时处理
  593. pt = (IEC101_DEF *)g_tRsComm[i].ptBuf;
  594. if(pt->st101 == IEC101_ST_DATA)
  595. {
  596. commErrTab[i] = false;
  597. #ifdef COMM_STATUS_LED//通信状态灯
  598. g_bCommStatus=true;
  599. #endif
  600. }
  601. }
  602. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_101_M||tRunPara.tUartPara[i].wProtocol==PROTOCOL_101_PH_M) // 串口级联规约
  603. {
  604. IECLINK_DEF *pt;
  605. pt = (IECLINK_DEF *)g_tRsComm[i].ptBuf;
  606. lnk_101_app((IECLINK_DEF *)g_tRsComm[i].ptBuf);
  607. //equNum = pt->slave_current;//按级联地址不同开始计数开始算级联个数 MAX=CFG_LINK_101_NUM
  608. equNum = pt->slave_addr;//按级联地址开始算级联个数 MAX=32
  609. commErrTab[i] = false;//不监视装置状态的时候,通道默认为正常
  610. if(equNum && equNum<=pt->slave_num)
  611. {
  612. if(tRunPara.dLinkStateCh==i)//监测该通道级联装置信息
  613. {
  614. if(equNum<=CFG_LINK_EQU_SOE_NUM && soe_check(EV_LINK_EQU1+equNum-1) != (!pt->tlnk.bLinkOK))
  615. soe_record_ev(EV_LINK_EQU1+equNum-1, (!pt->tlnk.bLinkOK), 0,0,0 ); //级联装置状态
  616. //rt_printf("slave_current = %d slave_addr=%d slave_num=%d\r\n",pt->slave_current,pt->slave_addr,pt->slave_num);
  617. for(equNum=0;(equNum<pt->slave_num&&equNum<=CFG_LINK_EQU_SOE_NUM);equNum++)
  618. {
  619. if(soe_check(EV_LINK_EQU1+equNum))//当前有一装置异常
  620. {
  621. commErrTab[i] = true;//该通信端口异常
  622. break;
  623. }
  624. }
  625. }
  626. //else //if(soe_check(EV_LINK_EQU1+equNum))//当前有一装置异常
  627. {
  628. //commErrTab[i] = true;//该通信端口异常
  629. //break;
  630. }
  631. }
  632. }
  633. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_104) // 104规约
  634. {
  635. IEC104_DEF *pt104;
  636. IEC104_App((IEC104_DEF *)g_tRsComm[i].ptBuf);
  637. pt104 = (IEC104_DEF *)g_tRsComm[i].ptBuf;
  638. if(pt104->state == IEC104_ST_STARTED)
  639. {
  640. commErrTab[i] = false;
  641. }
  642. }
  643. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_HMI) // 面板规约
  644. {
  645. Uart_Comm_Recv();
  646. HMI_Comm_App();
  647. PDA_app();
  648. #ifdef FUN_GPRS // modfiy for xxxxxx 20220706 GPRS没有使用。
  649. gprs_comm_app();
  650. #endif
  651. //发送面板,PDA等数据
  652. Uart_Comm_Send();
  653. if(hmi_status)
  654. commErrTab[i] =false;
  655. }
  656. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_FAULT) // 状态版
  657. {
  658. FaultComm_Applay((IEC101_DEF *)g_tRsComm[i].ptBuf);
  659. }
  660. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_SXYB) //数显仪表
  661. {
  662. sxyb_App();
  663. }
  664. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_MODBUS) // modbus主站规约
  665. {
  666. modbus_Applay((MODBUS_DEF *)g_tRsComm[i].ptBuf);
  667. modbus_AutoTimer((MODBUS_DEF *)g_tRsComm[i].ptBuf);
  668. }
  669. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_GPS) // GPS模块
  670. {
  671. GPS_Comm_App((GPS_COMM *)g_tRsComm[i].ptBuf);
  672. }
  673. #ifdef BATTERY_WITH_COMM
  674. else if(tRunPara.tUartPara[i].wProtocol == PROTOCOL_POWER_MOD)
  675. {
  676. PowerModuleCommRun((COMM_POW_MOD*)g_tRsComm[i].ptBuf);
  677. }
  678. #ifdef FUNC_SEL_BAT_MODULE
  679. else if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_PWRM) // 电源模块规约
  680. {
  681. pwrm_recv_proc((pwdmodlue_def *)g_tRsComm[i].ptBuf);
  682. pwrm_send_entry((pwdmodlue_def *)g_tRsComm[i].ptBuf);
  683. }
  684. #endif
  685. #endif
  686. else //if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_NULL)
  687. {
  688. commErrTab[i] =false;
  689. //continue;
  690. }
  691. if(soe_check(EV_COMM1_ERR+i) != commErrTab[i])
  692. {
  693. soe_record_ev(EV_COMM1_ERR+i, commErrTab[i], 0,0,0 ); //记录该端口通信状态
  694. }
  695. }
  696. // 以太网级联从设备
  697. for(i=0;i<CFG_LINK_104_NUM;i++)
  698. {
  699. if(tRunPara.ip104Link[i])
  700. {
  701. //if(tRunPara.dLinkStateCh==8)//监测该通道级联装置信息
  702. {
  703. if(!net_104link_is_connect(i))
  704. {
  705. if(soe_check(EV_LINK_EQU1+i) != 1)
  706. {
  707. //rt_printf("级联装置状态改变产生异常SOE,断开连接\n\r");
  708. soe_record_ev(EV_LINK_EQU1+i,1,0,0,0 ); //级联装置状态
  709. }
  710. }
  711. else if (net_104link_is_connect(i) && g_link_104[i].bData)
  712. {
  713. if(soe_check(EV_LINK_EQU1+i) != 0)
  714. {
  715. //rt_printf("级联装置状态改变产生恢复SOE,建立连接\n\r");
  716. soe_record_ev(EV_LINK_EQU1+i,0,0,0,0 ); //级联装置状态
  717. }
  718. }
  719. }
  720. lnk_104_app(i);
  721. }
  722. }
  723. }
  724. u8 get_is_invert(u16 no)
  725. {
  726. int i;
  727. for(i=0;i<g_table_head->di_num;i++)
  728. {
  729. if(g_di_table[i].cp == no)
  730. return g_di_table[i].is_inverse;
  731. }
  732. return 0;
  733. }
  734. #ifdef IECTABLE_NEWDPI_V106
  735. u8 get_is_dpi(u16 no)
  736. {
  737. int i;
  738. for(i=0;i<g_table_head->di_num;i++)
  739. {
  740. if(g_di_table[i].cp == no)
  741. return g_di_table[i].dpi;
  742. }
  743. return 0;
  744. }
  745. #endif
  746. #ifdef SOE_RELINK_DISCOS
  747. bool is_resume_soe(u8 ch)
  748. {
  749. if(g_soe_queue.bSoeRelink[ch]) /*发生续传中*/
  750. {
  751. if(g_soe_queue.tail_send[ch].n != g_soe_queue.relink_tail_bak[ch].n)
  752. {
  753. rt_printf("------通讯续传中tail_send:%d,bak:%d\r\n",g_soe_queue.tail_send[ch].n,g_soe_queue.relink_tail_bak[ch].n);
  754. return true;
  755. }
  756. else
  757. {
  758. rt_printf("------通讯续传结束\r\n");
  759. g_soe_queue.bSoeRelink[ch]=false; /*续传结束*/
  760. }
  761. }
  762. return false;
  763. }
  764. #endif
  765. int asdu_get_event(u8 ch,u16 head,u8* pcos,u8 *psoe,u8* pcos1,u8 *psoe1,u16*psoe_sno,u16*psoe_dno, int baddr3,bool *bf,EVENT_STRUCT *ev,u16 *extarg,u8 *yxtype,u8 *pcosnum,u8 *pcosnum1)
  766. {
  767. bool b_siq=false;
  768. int soe_no=0,cnt_s=0,cnt_d=0,cnt_evp=0;
  769. u8 SIQ,week;
  770. u8 soe_type;
  771. u16 no,index=0;
  772. EVENT_STRUCT event;
  773. struct rtc_time_t rt;
  774. u8 isInverse=0;
  775. u8 ditype=0;
  776. *pcosnum = 0;
  777. *pcosnum1 = 0;
  778. while(g_soe_queue.tail_send[ch].n != head) // 有SOE产生
  779. {
  780. #ifdef SOE_RELINK_DISCOS
  781. bool bresuming = false;
  782. bresuming = is_resume_soe(ch);
  783. #endif
  784. // 得到一个事件
  785. index=g_soe_queue.tail_send[ch].n;
  786. soe_no = soe_get(ch,&event);
  787. if(soe_no < 0)
  788. {
  789. rt_printf_time("soe_get(%d):ret=%d.\r\n",ch,soe_no);
  790. break;
  791. }
  792. // 得到检查通讯点号,如果为0,则此事件不需要发送
  793. // 此处会滤除不需发送的事件的,例如操作事件。
  794. no = soe_lp2cp(event.ev_type,event.ev_code,event.ev_arg[1]);
  795. if(no == 0)
  796. {
  797. continue;
  798. }
  799. #ifdef IEC_JXYB_DEAL
  800. if(g_run_stu.bjx)
  801. {
  802. u8 yx_slot=(u8)(event.ev_code>>8); //板卡号
  803. u8 yx_no=(u8)(event.ev_code); //遥信号
  804. u8 yx_type = equ_pub_di_type(yx_slot,yx_no);
  805. {
  806. if(yx_type>0)
  807. {
  808. yx_type-=1;
  809. if(yx_type != PUB_DI_JX)
  810. {
  811. continue;
  812. }
  813. }
  814. else
  815. {
  816. continue;
  817. }
  818. }
  819. }
  820. #endif
  821. #ifdef IECTABLE_NEWDPI_V106
  822. {
  823. int k;
  824. u8 owner=0;
  825. isInverse = get_is_invert(no);
  826. ditype=get_is_dpi(no);
  827. // 整理好SIQ,单双点需要转换
  828. soe_type = event.ev_type&SOE_TYPE_MASK;
  829. SIQ = event.ev_value;
  830. for(k=0;k<g_table_head->di_num;k++)
  831. {
  832. if(g_di_table[k].cp == no)
  833. {
  834. owner = g_di_table[k].owner;
  835. break;
  836. }
  837. }
  838. if(tRunPara.bSDPI)
  839. {
  840. if(soe_type == SOE_TYPE_DPI || soe_type == SOE_TYPE_LNK)
  841. {
  842. if(ditype==0) // 单点上送
  843. {
  844. SIQ = (SIQ == SW_DI_TYPE_ON);//转为单点信息
  845. }
  846. }
  847. else
  848. {
  849. if(ditype==1) //双点上送
  850. {
  851. SIQ +=1;//转为双点点信息
  852. }
  853. }
  854. }
  855. else
  856. {
  857. ditype=tRunPara.bDPI?1:0;
  858. if(ditype==1) // 双点上送
  859. {
  860. if(soe_type!= SOE_TYPE_DPI && soe_type!= SOE_TYPE_LNK)
  861. {
  862. SIQ +=1;//转为双点点信息
  863. }
  864. }
  865. else
  866. {
  867. if(soe_type == SOE_TYPE_DPI || soe_type == SOE_TYPE_LNK)
  868. {
  869. SIQ = (SIQ == SW_DI_TYPE_ON);//转为单点信息
  870. }
  871. }
  872. }
  873. if(isInverse)
  874. {
  875. if(ditype==1) //双点取反
  876. {
  877. if(SIQ == 2)
  878. {
  879. SIQ = 1;
  880. }
  881. else if(SIQ == 1)
  882. {
  883. SIQ = 2;
  884. }
  885. }
  886. else // 单点取反
  887. {
  888. SIQ = (SIQ == 0)?1:0;
  889. }
  890. }
  891. }
  892. #else
  893. {
  894. u8 owner=0;
  895. int j,k,id1,id2;
  896. bool db_special = 0;//特殊双点
  897. static u8 bh_fa_keep[SWITCH_NUM_EXT] = {0};
  898. for(k=0;k<g_table_head->di_num;k++)
  899. {
  900. if(g_di_table[k].cp == no)
  901. {
  902. owner = g_di_table[k].owner;
  903. break;
  904. }
  905. }
  906. for(j=0;j<g_table_head->di_db_num;j++)
  907. {
  908. db_special = false;
  909. if(no == g_di_db_table[j].cp)
  910. {
  911. id1 = g_di_db_table[j].no[0]&0xff;
  912. id2 = g_di_db_table[j].no[1]&0xff;
  913. if(((id1 == (SW_DI_BHTT+1))&&(id2 == (SW_DI_FA_TT+1)))
  914. ||((id2 == (SW_DI_BHTT+1))&&(id1 == (SW_DI_FA_TT+1))))
  915. {
  916. //rt_printf("%s:cp=0x%04x,id1=0x%04x,id2=0x%04x\r\n",__func__,g_di_db_table[j].cp,g_di_db_table[j].no[0],g_di_db_table[j].no[1]);
  917. db_special = true;
  918. break;
  919. }
  920. }
  921. }
  922. isInverse = get_is_invert(no);
  923. // 整理好SIQ,单双点需要转换
  924. soe_type = event.ev_type&SOE_TYPE_MASK;
  925. SIQ = event.ev_value;
  926. //级联上送的SOE带品质描述
  927. if(SIQ&0x80)
  928. {
  929. if(pRunSet->bTT_QDS)
  930. {
  931. b_siq=true;
  932. SIQ=(SIQ&0x7f);
  933. }
  934. #ifdef IEC_QDS_HAVE
  935. b_siq=true;
  936. SIQ=(SIQ&0x7f);
  937. #endif
  938. }
  939. if(db_special)
  940. {
  941. if(g_sw[owner].di[SW_DI_BHTT] == 1 && g_sw[owner].di[SW_DI_FA_TT] == 1)
  942. SIQ = bh_fa_keep[owner];
  943. else
  944. {
  945. if(g_sw[owner].di[SW_DI_FA_TT] == 2)
  946. SIQ = 1;
  947. else if(g_sw[owner].di[SW_DI_BHTT] == 2)
  948. SIQ = 0;
  949. bh_fa_keep[owner] = SIQ;
  950. }
  951. if(SIQ==0)
  952. {
  953. SIQ = 1;
  954. }
  955. else
  956. {
  957. SIQ = 2;
  958. }
  959. }
  960. //rt_printf("%s(2):soe_type=%d,SIQ=0x%02x,owner=%d,no=0x%04x,indexno=%d,isInverse=%d\r\n",__func__,soe_type,SIQ,owner,no,g_di_table[k].indexno,isInverse);
  961. if(tRunPara.bSDPI)
  962. {
  963. ditype=(soe_type == SOE_TYPE_DPI)?1:0;
  964. if(soe_type == SOE_TYPE_LNK)
  965. {
  966. if(SWITCH_NUM_EXT == owner) //将默认开关31当成级联双点
  967. {
  968. ditype = 1;
  969. }
  970. else
  971. {
  972. SIQ = (SIQ == SW_DI_TYPE_ON);//转为单点信息
  973. if(isInverse)
  974. {
  975. SIQ = (SIQ == 0)?1:0;
  976. }
  977. }
  978. }
  979. else
  980. {
  981. if(soe_type != SOE_TYPE_DPI)
  982. {
  983. if(isInverse)
  984. {
  985. SIQ = (SIQ == 0)?1:0;
  986. }
  987. }
  988. else
  989. {
  990. if(isInverse)
  991. {
  992. if(SIQ == 2)
  993. {
  994. SIQ = 1;
  995. }
  996. else if(SIQ == 1)
  997. {
  998. SIQ = 2;
  999. }
  1000. }
  1001. }
  1002. }
  1003. /*
  1004. if((soe_type == SOE_TYPE_DPI)&&(isInverse))
  1005. {
  1006. SIQ =((~SIQ)&0x03);
  1007. }
  1008. */
  1009. }
  1010. else
  1011. {
  1012. if(tRunPara.bDPI)
  1013. {
  1014. if(soe_type != SOE_TYPE_DPI && soe_type != SOE_TYPE_LNK)
  1015. {
  1016. SIQ+=1; //转为双点信息
  1017. }
  1018. if(isInverse)
  1019. {
  1020. if(SIQ == 2)
  1021. {
  1022. SIQ = 1;
  1023. }
  1024. else if(SIQ == 1)
  1025. {
  1026. SIQ = 2;
  1027. }
  1028. }
  1029. }
  1030. else
  1031. {
  1032. if(soe_type == SOE_TYPE_DPI || soe_type == SOE_TYPE_LNK)
  1033. {
  1034. SIQ = (SIQ == SW_DI_TYPE_ON);//转为单点信息
  1035. }
  1036. if(soe_type != SOE_TYPE_PLC)
  1037. {
  1038. if(isInverse)
  1039. {
  1040. SIQ = (SIQ == 0)?1:0;
  1041. }
  1042. }
  1043. }
  1044. ditype=tRunPara.bDPI?1:0;
  1045. }
  1046. }
  1047. #endif
  1048. *yxtype=ditype;
  1049. if(g_run_stu.bToolRmtTest||g_run_stu.bHmiRmtTest) //测试模式,遥信置为无效
  1050. {
  1051. if(pRunSet->bTT_RmtTest)SIQ|=0x80;
  1052. }
  1053. #if !defined IEC_JXYB_DEAL && !defined FUNC_JX_YC_SIQ00
  1054. if(g_run_stu.bjx||b_siq)
  1055. SIQ|=0x80;
  1056. #endif
  1057. //单点
  1058. if(*yxtype==0)
  1059. {
  1060. //COS
  1061. #ifdef SOE_RELINK_DISCOS
  1062. if(!bresuming)
  1063. #endif
  1064. {
  1065. *pcos++=(u8)no; //信息体地址L
  1066. *pcos++=(u8)(no>>8); //信息体地址H
  1067. if(baddr3)
  1068. {
  1069. *pcos++=0; //信息体地址H2
  1070. }
  1071. *pcos++=SIQ; //遥信
  1072. *pcosnum += 1;
  1073. }
  1074. //SOE
  1075. timespec_to_rtc(event.ts,&rt,1);
  1076. week = WEEK_DAY(event.ts.tv_sec);
  1077. *psoe++=(u8)no; // 信息体地址L
  1078. *psoe++=(u8)(no>>8); // 信息体地址H
  1079. if(baddr3)
  1080. {
  1081. *psoe++=0; // 信息体地址H2
  1082. }
  1083. *psoe++=SIQ; // 遥信
  1084. *psoe++=(u8)rt.ms;
  1085. *psoe++=(u8)(rt.ms>>8);
  1086. *psoe++=(u8)rt.min;
  1087. *psoe++=(u8)rt.hour;
  1088. *psoe++=((week<<5) | rt.day);
  1089. *psoe++=(u8)rt.month;
  1090. *psoe++=(u8)rt.year;
  1091. cnt_s++;
  1092. *psoe_sno = soe_no;
  1093. }
  1094. else
  1095. {
  1096. #ifdef SOE_RELINK_DISCOS
  1097. if(!bresuming)
  1098. #endif
  1099. {
  1100. *pcos1++=(u8)no; //信息体地址L
  1101. *pcos1++=(u8)(no>>8); //信息体地址H
  1102. if(baddr3)
  1103. {
  1104. *pcos1++=0; //信息体地址H2
  1105. }
  1106. *pcos1++=SIQ; //遥信
  1107. *pcosnum1 +=1;
  1108. }
  1109. //SOE
  1110. timespec_to_rtc(event.ts,&rt,1);
  1111. week = WEEK_DAY(event.ts.tv_sec);
  1112. *psoe1++=(u8)no; // 信息体地址L
  1113. *psoe1++=(u8)(no>>8); // 信息体地址H
  1114. if(baddr3)
  1115. {
  1116. *psoe1++=0; // 信息体地址H2
  1117. }
  1118. *psoe1++=SIQ; // 遥信
  1119. *psoe1++=(u8)rt.ms;
  1120. *psoe1++=(u8)(rt.ms>>8);
  1121. *psoe1++=(u8)rt.min;
  1122. *psoe1++=(u8)rt.hour;
  1123. *psoe1++=((week<<5) | rt.day);
  1124. *psoe1++=(u8)rt.month;
  1125. *psoe1++=(u8)rt.year;
  1126. cnt_d++;
  1127. *psoe_dno = soe_no;
  1128. }
  1129. if(event.ev_type&SOE_TYPE_FAULT) // 该事件需上送故障信息
  1130. {
  1131. if(ev!=NULL)
  1132. {
  1133. *ev++=event;
  1134. *extarg++=index;
  1135. cnt_evp++;
  1136. }
  1137. //*bf=(num_p>0)&&tRunPara.bEvPara;
  1138. }
  1139. if(cnt_s>=MAX_SOE_PER_FRAME)
  1140. {
  1141. break;
  1142. }
  1143. if(cnt_d>=MAX_SOE_PER_FRAME)
  1144. {
  1145. break;
  1146. }
  1147. //if(tRunPara.bSDPI)break; // 单双点配置,SOE逐条上送
  1148. }
  1149. return (cnt_s+(cnt_d<<8)+(cnt_evp<<16));
  1150. }
  1151. int asdu_get_event_104(u8 ch,u16 head,u8* pcos,u8 *psoe,u16 *psoe_no,int baddr3,bool *bf,EVENT_STRUCT *ev,u16 *extarg,u8 *yxtype)
  1152. {
  1153. bool b_siq=false;
  1154. int soe_no=0,cnt=0;
  1155. u8 SIQ,week;
  1156. u8 soe_type;
  1157. u16 no,index=0;
  1158. EVENT_STRUCT event;
  1159. struct rtc_time_t rt;
  1160. u8 isInverse=0;
  1161. u8 ditype=0;
  1162. while(g_soe_queue.tail_send[ch].n != head) // 有SOE产生
  1163. {
  1164. // 得到一个事件
  1165. index=g_soe_queue.tail_send[ch].n;
  1166. soe_no = soe_get(ch,&event);
  1167. if(soe_no < 0)
  1168. {
  1169. rt_printf_time("soe_get(%d):ret=%d.\r\n",ch,soe_no);
  1170. break;
  1171. }
  1172. // 得到检查通讯点号,如果为0,则此事件不需要发送
  1173. // 此处会滤除不需发送的事件的,例如操作事件。
  1174. no = soe_lp2cp(event.ev_type,event.ev_code,event.ev_arg[1]);
  1175. if(no == 0)
  1176. {
  1177. continue;
  1178. }
  1179. #ifdef IEC_JXYB_DEAL
  1180. if(g_run_stu.bjx)
  1181. {
  1182. u8 yx_slot=(u8)(event.ev_code>>8); //板卡号
  1183. u8 yx_no=(u8)(event.ev_code); //遥信号
  1184. u8 yx_type = equ_pub_di_type(yx_slot,yx_no);
  1185. {
  1186. if(yx_type>0)
  1187. {
  1188. yx_type-=1;
  1189. if(yx_type != PUB_DI_JX)
  1190. {
  1191. continue;
  1192. }
  1193. }
  1194. else
  1195. {
  1196. continue;
  1197. }
  1198. }
  1199. }
  1200. #endif
  1201. #ifdef IECTABLE_NEWDPI_V106
  1202. {
  1203. int k;
  1204. u8 owner=0;
  1205. isInverse = get_is_invert(no);
  1206. ditype=get_is_dpi(no);
  1207. // 整理好SIQ,单双点需要转换
  1208. soe_type = event.ev_type&SOE_TYPE_MASK;
  1209. SIQ = event.ev_value;
  1210. for(k=0;k<g_table_head->di_num;k++)
  1211. {
  1212. if(g_di_table[k].cp == no)
  1213. {
  1214. owner = g_di_table[k].owner;
  1215. break;
  1216. }
  1217. }
  1218. if(tRunPara.bSDPI)
  1219. {
  1220. if(soe_type == SOE_TYPE_DPI || soe_type == SOE_TYPE_LNK)
  1221. {
  1222. if(ditype==0) // 单点上送
  1223. {
  1224. SIQ = (SIQ == SW_DI_TYPE_ON);//转为单点信息
  1225. }
  1226. }
  1227. else
  1228. {
  1229. if(ditype==1) //双点上送
  1230. {
  1231. SIQ +=1;//转为双点点信息
  1232. }
  1233. }
  1234. }
  1235. else
  1236. {
  1237. ditype=tRunPara.bDPI?1:0;
  1238. if(ditype==1) // 双点上送
  1239. {
  1240. if(soe_type!= SOE_TYPE_DPI && soe_type!= SOE_TYPE_LNK)
  1241. {
  1242. SIQ +=1;//转为双点点信息
  1243. }
  1244. }
  1245. else
  1246. {
  1247. if(soe_type == SOE_TYPE_DPI || soe_type == SOE_TYPE_LNK)
  1248. {
  1249. SIQ = (SIQ == SW_DI_TYPE_ON);//转为单点信息
  1250. }
  1251. }
  1252. }
  1253. if(isInverse)
  1254. {
  1255. if(ditype==1) //双点取反
  1256. {
  1257. if(SIQ == 2)
  1258. {
  1259. SIQ = 1;
  1260. }
  1261. else if(SIQ == 1)
  1262. {
  1263. SIQ = 2;
  1264. }
  1265. }
  1266. else // 单点取反
  1267. {
  1268. SIQ = (SIQ == 0)?1:0;
  1269. }
  1270. }
  1271. }
  1272. #else
  1273. {
  1274. u8 owner=0;
  1275. int j,k,id1,id2;
  1276. bool db_special = 0;//特殊双点
  1277. static u8 bh_fa_keep[SWITCH_NUM_EXT] = {0};
  1278. for(k=0;k<g_table_head->di_num;k++)
  1279. {
  1280. if(g_di_table[k].cp == no)
  1281. {
  1282. owner = g_di_table[k].owner;
  1283. break;
  1284. }
  1285. }
  1286. for(j=0;j<g_table_head->di_db_num;j++)
  1287. {
  1288. db_special = false;
  1289. if(no == g_di_db_table[j].cp)
  1290. {
  1291. id1 = g_di_db_table[j].no[0]&0xff;
  1292. id2 = g_di_db_table[j].no[1]&0xff;
  1293. if(((id1 == (SW_DI_BHTT+1))&&(id2 == (SW_DI_FA_TT+1)))
  1294. ||((id2 == (SW_DI_BHTT+1))&&(id1 == (SW_DI_FA_TT+1))))
  1295. {
  1296. //rt_printf("%s:cp=0x%04x,id1=0x%04x,id2=0x%04x\r\n",__func__,g_di_db_table[j].cp,g_di_db_table[j].no[0],g_di_db_table[j].no[1]);
  1297. db_special = true;
  1298. break;
  1299. }
  1300. }
  1301. }
  1302. isInverse = get_is_invert(no);
  1303. // 整理好SIQ,单双点需要转换
  1304. soe_type = event.ev_type&SOE_TYPE_MASK;
  1305. SIQ = event.ev_value;
  1306. //级联上送的SOE带品质描述
  1307. if(SIQ&0x80)
  1308. {
  1309. if(pRunSet->bTT_QDS)
  1310. {
  1311. b_siq=true;
  1312. SIQ=(SIQ&0x7f);
  1313. }
  1314. #ifdef IEC_QDS_HAVE
  1315. b_siq=true;
  1316. SIQ=(SIQ&0x7f);
  1317. #endif
  1318. }
  1319. if(db_special)
  1320. {
  1321. if(g_sw[owner].di[SW_DI_BHTT] == 1 && g_sw[owner].di[SW_DI_FA_TT] == 1)
  1322. SIQ = bh_fa_keep[owner];
  1323. else
  1324. {
  1325. if(g_sw[owner].di[SW_DI_FA_TT] == 2)
  1326. SIQ = 1;
  1327. else if(g_sw[owner].di[SW_DI_BHTT] == 2)
  1328. SIQ = 0;
  1329. bh_fa_keep[owner] = SIQ;
  1330. }
  1331. if(SIQ==0)
  1332. {
  1333. SIQ = 1;
  1334. }
  1335. else
  1336. {
  1337. SIQ = 2;
  1338. }
  1339. }
  1340. //rt_printf("%s(2):soe_type=%d,SIQ=0x%02x,owner=%d,no=0x%04x,indexno=%d,isInverse=%d\r\n",__func__,soe_type,SIQ,owner,no,g_di_table[k].indexno,isInverse);
  1341. if(tRunPara.bSDPI)
  1342. {
  1343. ditype=(soe_type == SOE_TYPE_DPI)?1:0;
  1344. if(soe_type == SOE_TYPE_LNK)
  1345. {
  1346. if(SWITCH_NUM_EXT == owner) //将默认开关31当成级联双点
  1347. {
  1348. ditype = 1;
  1349. }
  1350. else
  1351. {
  1352. SIQ = (SIQ == SW_DI_TYPE_ON);//转为单点信息
  1353. if(isInverse)
  1354. {
  1355. SIQ = (SIQ == 0)?1:0;
  1356. }
  1357. }
  1358. }
  1359. else
  1360. {
  1361. if(soe_type != SOE_TYPE_DPI)
  1362. {
  1363. if(isInverse)
  1364. {
  1365. SIQ = (SIQ == 0)?1:0;
  1366. }
  1367. }
  1368. else
  1369. {
  1370. if(isInverse)
  1371. {
  1372. if(SIQ == 2)
  1373. {
  1374. SIQ = 1;
  1375. }
  1376. else if(SIQ == 1)
  1377. {
  1378. SIQ = 2;
  1379. }
  1380. }
  1381. }
  1382. }
  1383. /*
  1384. if((soe_type == SOE_TYPE_DPI)&&(isInverse))
  1385. {
  1386. SIQ =((~SIQ)&0x03);
  1387. }
  1388. */
  1389. }
  1390. else
  1391. {
  1392. if(tRunPara.bDPI)
  1393. {
  1394. if(soe_type != SOE_TYPE_DPI && soe_type != SOE_TYPE_LNK)
  1395. {
  1396. SIQ+=1; //转为双点信息
  1397. }
  1398. if(isInverse)
  1399. {
  1400. if(SIQ == 2)
  1401. {
  1402. SIQ = 1;
  1403. }
  1404. else if(SIQ == 1)
  1405. {
  1406. SIQ = 2;
  1407. }
  1408. }
  1409. }
  1410. else
  1411. {
  1412. if(soe_type == SOE_TYPE_DPI || soe_type == SOE_TYPE_LNK)
  1413. {
  1414. SIQ = (SIQ == SW_DI_TYPE_ON);//转为单点信息
  1415. }
  1416. if(soe_type != SOE_TYPE_PLC)
  1417. {
  1418. if(isInverse)
  1419. {
  1420. SIQ = (SIQ == 0)?1:0;
  1421. }
  1422. }
  1423. }
  1424. ditype=tRunPara.bDPI?1:0;
  1425. }
  1426. }
  1427. #endif
  1428. *yxtype=ditype;
  1429. if(g_run_stu.bToolRmtTest||g_run_stu.bHmiRmtTest) //测试模式,遥信置为无效
  1430. {
  1431. if(pRunSet->bTT_RmtTest)SIQ|=0x80;
  1432. }
  1433. #if !defined IEC_JXYB_DEAL && !defined FUNC_JX_YC_SIQ00
  1434. if(g_run_stu.bjx||b_siq)
  1435. SIQ|=0x80;
  1436. #endif
  1437. //COS
  1438. *pcos++=(u8)no; //信息体地址L
  1439. *pcos++=(u8)(no>>8); //信息体地址H
  1440. if(baddr3)
  1441. {
  1442. *pcos++=0; //信息体地址H2
  1443. }
  1444. *pcos++=SIQ; //遥信
  1445. //SOE
  1446. timespec_to_rtc(event.ts,&rt,1);
  1447. week = WEEK_DAY(event.ts.tv_sec);
  1448. *psoe++=(u8)no; // 信息体地址L
  1449. *psoe++=(u8)(no>>8); // 信息体地址H
  1450. if(baddr3)
  1451. {
  1452. *psoe++=0; // 信息体地址H2
  1453. }
  1454. *psoe++=SIQ; // 遥信
  1455. *psoe++=(u8)rt.ms;
  1456. *psoe++=(u8)(rt.ms>>8);
  1457. *psoe++=(u8)rt.min;
  1458. *psoe++=(u8)rt.hour;
  1459. *psoe++=((week<<5) | rt.day);
  1460. *psoe++=(u8)rt.month;
  1461. *psoe++=(u8)rt.year;
  1462. cnt++;
  1463. if(event.ev_type&SOE_TYPE_FAULT) // 该事件需上送故障信息
  1464. {
  1465. if(ev!=NULL)
  1466. {
  1467. *ev=event;
  1468. *bf=true&&tRunPara.bEvPara;
  1469. *extarg=index;
  1470. break;
  1471. }
  1472. }
  1473. if(cnt>=MAX_SOE_PER_FRAME)
  1474. {
  1475. break;
  1476. }
  1477. if(tRunPara.bSDPI)break; // 单双点配置,SOE逐条上送
  1478. }
  1479. *psoe_no = soe_no;
  1480. return cnt;
  1481. }
  1482. int asdu_get_event_yc(BYTE *dat,int baddr3,u16 arg_index)
  1483. {
  1484. int evsw;
  1485. int i,j;
  1486. BYTE indexno;
  1487. BYTE ycnum=0;
  1488. EVENT_EXT_ARG *pArg;
  1489. pArg=&g_soe_queue.tExtArg[arg_index]; //附加参数
  1490. for(i=0;i<EXT_ARG_NUMBER;i++)
  1491. {
  1492. if(pArg->type[i]==SW_AC_IA
  1493. ||pArg->type[i]==SW_AC_IB
  1494. ||pArg->type[i]==SW_AC_IC
  1495. ||pArg->type[i]==SW_AC_I0)
  1496. {
  1497. evsw=pArg->sw+1;
  1498. }
  1499. else
  1500. {
  1501. evsw=0;
  1502. }
  1503. indexno=(pArg->type[i]&0x7f)+1;
  1504. for(j=0;j<g_table_head->ac_num;j++)
  1505. {
  1506. u8 no=g_ac_table[j].indexno;
  1507. if(g_ac_table[j].owner==evsw&&no==indexno) // soe参数在转发表中有配置获取点号
  1508. {
  1509. WORD di=g_ac_table[j].cp;
  1510. union{
  1511. float ff;
  1512. u8 tt[4];
  1513. }ff;
  1514. #ifdef IEC_EVENT_YC_RATE
  1515. if(pRunSet->bTT_EV_YCRate)
  1516. {
  1517. ff.ff=(float)pArg->arg[i]/65536*((float)g_ac_table[j].rate/65536.0);
  1518. }
  1519. else
  1520. {
  1521. ff.ff=(float)pArg->arg[i]/65536;
  1522. }
  1523. #else
  1524. ff.ff=(float)pArg->arg[i]/65536;
  1525. #endif
  1526. *dat++=(BYTE)di;
  1527. *dat++=(BYTE)(di>>8);
  1528. if(baddr3)*dat++=0;
  1529. dat += CopySwap(dat,ff.tt,sizeof(ff.tt),true);
  1530. ycnum++;
  1531. }
  1532. }
  1533. }
  1534. return ycnum;
  1535. }
  1536. #ifdef CPU_AM335X
  1537. void uart_isr(void) /* 串口处理*/
  1538. {
  1539. int ret = 0;
  1540. int i = 0;
  1541. //static unsigned char buf[256];
  1542. unsigned char buf[10]; //sunxi for test
  1543. int ch = 0;
  1544. volatile SERIAL_COMM *prc;
  1545. for(ch=0;ch<CFG_UART_NUM_MAX;ch++)
  1546. {
  1547. if( UART_CHANNEL[ch] < 0
  1548. || UART_CHANNEL[ch] == g_con_uart_index
  1549. || tRunPara.tUartPara[ch].wProtocol == PROTOCOL_IDLE)
  1550. {
  1551. continue;
  1552. }
  1553. //如果是使用内部GPRS,则不用再走下面的代码流程
  1554. if(UART_CHANNEL[ch]==CFG_UART_GPRS_IN )
  1555. {
  1556. continue;
  1557. }
  1558. #ifdef FUN_FUXI_ESAM
  1559. else if(UART_CHANNEL[ch]==CFG_UART_CHIP_S1 )
  1560. {
  1561. continue;
  1562. }
  1563. #endif
  1564. //面板做特殊处理
  1565. if(tRunPara.tUartPara[ch].wProtocol==PROTOCOL_HMI)
  1566. {
  1567. //规约处理
  1568. protocol_proc_task(ch);
  1569. continue;
  1570. }
  1571. prc=&g_tRsComm[ch];
  1572. #if 1
  1573. //接收 start
  1574. //ret = uart_select(UART_CHANNEL[ch]);
  1575. //有数据可读
  1576. if(1)
  1577. {
  1578. unsigned char c;
  1579. int jj;
  1580. //读取数据
  1581. memset(buf,0,sizeof(buf));
  1582. // sunxi 20221202 ret = uart_read(UART_CHANNEL[ch], buf, sizeof(buf));
  1583. for(jj = 0; jj < 3; jj ++)
  1584. {
  1585. ret = uart_read(UART_CHANNEL[ch], &c, 1);
  1586. if(ret > 0)
  1587. {
  1588. //处理101规约报文
  1589. if(g_IEC_Recv)
  1590. {
  1591. for(i=0; i<ret ;i++)
  1592. {
  1593. #ifdef FLOW_CALC_FUNC
  1594. if(ch == tRunPara.flow_port)
  1595. {
  1596. g_sw_pub.ac_in[PUB_AC_IN_FLOW]+=Q16_BASE;
  1597. }
  1598. #endif
  1599. g_IEC_Recv(c, ch);
  1600. }
  1601. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1602. {
  1603. //rt_printf("ch_%d have rev len =%d ",ch,ret);
  1604. for(i = 0; i < ret; i++)
  1605. {
  1606. rt_printf("%02x ", (unsigned char)c);
  1607. }
  1608. //rt_printf("\r\n");
  1609. }
  1610. s_stat_rx(ch, ret);//接收字节数计数
  1611. prc->us0_recv = ustimer_get_origin();//记录接收数据的时间戳
  1612. }
  1613. }
  1614. else if(ret < 0)
  1615. {
  1616. //rt_printf("\nch_%d read err!\n",ch);
  1617. s_stat_rx_err(ch, 1);//接收错误计数
  1618. if( ustimer_get_duration(prc->us0_recv) > prc->us_recv_timeout)
  1619. {
  1620. prc->b_recv_reset = 1;
  1621. }
  1622. }
  1623. else
  1624. {
  1625. if( ustimer_get_duration(prc->us0_recv) > prc->us_recv_timeout)
  1626. {
  1627. prc->b_recv_reset = 1;
  1628. }
  1629. }
  1630. }
  1631. }
  1632. //接收 end
  1633. #endif
  1634. #if 1
  1635. //发送 start
  1636. if(prc->bextsend)
  1637. {
  1638. // 规约要求帧与帧之间间隔必须大于33位。
  1639. if((prc->extsendcnt== 0 // 帧头
  1640. )
  1641. && ((ustimer_get_duration(prc->us0_recv) < 5*USTIMER_MS) // 与接收帧之间的间隔
  1642. || (ustimer_get_duration(prc->us0_send) < 5*USTIMER_MS))) // 与发送帧之间的间隔
  1643. {
  1644. continue;
  1645. }
  1646. if(prc->extsendlen > 0)
  1647. {
  1648. if(prc->extsendcnt < prc->extsendlen) // 发送没完成,继续发送
  1649. {
  1650. ret = uart_write(UART_CHANNEL[ch], (const char *)&prc->extsendbuf[prc->extsendcnt], 1);
  1651. if(ret > 0)
  1652. {
  1653. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1654. {
  1655. if(prc->extsendcnt == 0)
  1656. {
  1657. rt_printf("\r\nTX[%d] len=%d:",ch,prc->extsendlen);
  1658. }
  1659. for(i=0; i<ret ;i++)
  1660. {
  1661. rt_printf("%02x ",prc->extsendbuf[prc->extsendcnt + i]);
  1662. }
  1663. if(prc->extsendcnt >= (prc->extsendlen - 1))
  1664. {
  1665. rt_printf("\r\n");
  1666. }
  1667. }
  1668. s_stat_tx(ch, ret);//发送字节数计数
  1669. prc->extsendcnt += ret;
  1670. if(prc->extsendcnt >= prc->extsendlen)
  1671. {
  1672. prc->bextsend=false;
  1673. prc->extsendcnt = 0;
  1674. mainloop_wakeup();
  1675. prc->us0_send = ustimer_get_origin();//记录发送数据的时间戳
  1676. }
  1677. }
  1678. }
  1679. }
  1680. }
  1681. else if(prc->bSend) // 发送
  1682. {
  1683. // 规约要求帧与帧之间间隔必须大于33位。
  1684. if(prc->nSendCnt <= 1 // 帧头
  1685. && ((ustimer_get_duration(prc->us0_recv) < 5*USTIMER_MS) // 与接收帧之间的间隔
  1686. || (ustimer_get_duration(prc->us0_send) < 5*USTIMER_MS))) // 与发送帧之间的间隔
  1687. {
  1688. continue;
  1689. }
  1690. if(prc->sendbuf[0] > 0)
  1691. {
  1692. if(prc->nSendCnt <= prc->sendbuf[0])
  1693. {
  1694. ret = uart_write(UART_CHANNEL[ch], (const char *)&prc->sendbuf[prc->nSendCnt], 1);
  1695. if(ret > 0)
  1696. {
  1697. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1698. {
  1699. if(prc->nSendCnt ==0)
  1700. {
  1701. rt_printf("\r\nTX[ch:%d len=%d]:",ch,prc->sendbuf[0]);
  1702. }
  1703. for(i=0; i<ret ;i++)
  1704. {
  1705. rt_printf("%02x ",prc->sendbuf[prc->nSendCnt + i]);
  1706. }
  1707. if(prc->nSendCnt > (prc->sendbuf[0] - 1))
  1708. {
  1709. rt_printf("\r\n");
  1710. }
  1711. }
  1712. s_stat_tx(ch, ret);//发送字节数计数
  1713. if (uart_test_begin)
  1714. {
  1715. uart_test_flag[ch][0] = 1;
  1716. }
  1717. if(prc->nSendCnt > prc->sendbuf[0])
  1718. {
  1719. prc->bSend=false;
  1720. prc->nSendCnt = 0;
  1721. RS_Recv_Enable(ch);
  1722. if(tRunPara.tUartPara[ch].wProtocol==PROTOCOL_HMI)
  1723. {
  1724. Reset_Uart_Link((UART_COMM_STRUCT *)g_tRsComm[ch].ptBuf);
  1725. }
  1726. mainloop_wakeup();
  1727. prc->us0_send = ustimer_get_origin();//记录发送数据的时间戳
  1728. }
  1729. prc->nSendCnt += ret;
  1730. }
  1731. }
  1732. }
  1733. }
  1734. //发送 end
  1735. #endif
  1736. }
  1737. }
  1738. #elif defined CPU_FUXI
  1739. static int uart_isr_thread(void *arg)
  1740. {
  1741. int ret = 0;
  1742. int i = 0;
  1743. int send_fail_cnt = 0;
  1744. int send_len = 0;
  1745. static unsigned char buf[256];
  1746. #ifdef LINUX_USER_APP
  1747. char thread_name[25];
  1748. #endif
  1749. int ch = *((int *)arg);
  1750. volatile SERIAL_COMM *prc;
  1751. prc=&g_tRsComm[ch];
  1752. rt_printf("uart_isr_thread---%d\r\n", ch);
  1753. #ifdef CPU_FUXI
  1754. memset(thread_name,0,sizeof(thread_name));
  1755. sprintf(thread_name, "uart_isr_%d", ch);
  1756. prctl(PR_SET_NAME, thread_name);
  1757. uint32_t wdt_id[20];
  1758. int rc = watchdog_add_item(thread_name, &wdt_id[ch],120);
  1759. if(rc != 0) {
  1760. printf("can not add %s task to wdt\r\n",thread_name);
  1761. return -1;
  1762. }
  1763. #endif
  1764. while(1)
  1765. {
  1766. #ifdef CPU_FUXI
  1767. msleep(5);
  1768. watchdog_feed(wdt_id[ch]);
  1769. #else
  1770. msleep(1);//noted by sunxi: 20220706 面板切换太慢,5ms->1ms,以提高速度
  1771. #endif
  1772. //检查是否退出
  1773. if(main_mod_is_exit())
  1774. {
  1775. uart_close(UART_CHANNEL[ch]);
  1776. break;
  1777. }
  1778. if( UART_CHANNEL[ch] < 0
  1779. || UART_CHANNEL[ch] == g_con_uart_index
  1780. || tRunPara.tUartPara[ch].wProtocol == PROTOCOL_IDLE)
  1781. {
  1782. continue;
  1783. }
  1784. #if 0
  1785. // 如果GPS对时打开,必须关闭串口。
  1786. if(g_clock_mode_gps && chnl == CFG_UART_GPS)
  1787. {
  1788. continue;
  1789. }
  1790. #endif
  1791. //如果是使用内部GPRS,则不用再走下面的代码流程
  1792. if(UART_CHANNEL[ch]==CFG_UART_GPRS_IN )
  1793. {
  1794. continue;
  1795. }
  1796. #ifdef FUN_FUXI_ESAM
  1797. else if(UART_CHANNEL[ch]==CFG_UART_CHIP_S1 )
  1798. {
  1799. continue;
  1800. }
  1801. #endif
  1802. //面板做特殊处理
  1803. if(tRunPara.tUartPara[ch].wProtocol==PROTOCOL_HMI)
  1804. {
  1805. //规约处理
  1806. protocol_proc_task(ch);
  1807. continue;
  1808. }
  1809. /*
  1810. //sunxi for test: 暂不支持
  1811. //如果是数显仪表
  1812. if(tRunPara.tUartPara[ch].wProtocol==PROTOCOL_SXYB)
  1813. {
  1814. //数显仪表处理
  1815. //sxyb_proc(chnl);
  1816. continue;
  1817. }
  1818. */
  1819. #if 1
  1820. //接收 start
  1821. ret = uart_select(UART_CHANNEL[ch]);
  1822. //有数据可读
  1823. if(ret > 0)
  1824. {
  1825. //rt_printf("\nch_%d have select.\n",ch);
  1826. //读取数据
  1827. memset(buf,0,sizeof(buf));
  1828. ret = uart_read(UART_CHANNEL[ch], buf, sizeof(buf));
  1829. if(ret > 0)
  1830. {
  1831. //处理101规约报文
  1832. if(g_IEC_Recv)
  1833. {
  1834. //rt_printf("ch_%d have rev:\n",ch);
  1835. for(i=0; i<ret ;i++)
  1836. {
  1837. #ifdef FLOW_CALC_FUNC
  1838. if(ch == tRunPara.flow_port)
  1839. {
  1840. g_sw_pub.ac_in[PUB_AC_IN_FLOW]+=Q16_BASE;
  1841. }
  1842. #endif
  1843. g_IEC_Recv(buf[i], ch);
  1844. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1845. {
  1846. if(UART_CHANNEL[ch] == CFG_UART_232_0)
  1847. {
  1848. rt_printf("%c",buf[i]);
  1849. }
  1850. else
  1851. {
  1852. rt_printf("%02x ",buf[i]);
  1853. }
  1854. }
  1855. }
  1856. //rt_printf("\n");
  1857. s_stat_rx(ch, ret);//接收字节数计数
  1858. prc->us0_recv = ustimer_get_origin();//记录接收数据的时间戳
  1859. }
  1860. #if 0
  1861. //测试代码
  1862. rt_printf("\nch_%d RECV num=%d frame:",ch,ret);
  1863. for(i = 0; i < ret; i++)
  1864. {
  1865. rt_printf("%02x ", (unsigned char)buf[i]);
  1866. }
  1867. rt_printf("\n");
  1868. uart_write(ch, (const char *)buf, ret);//sunxi for test
  1869. #endif
  1870. }
  1871. else if(ret < 0)
  1872. {
  1873. //rt_printf("\nch_%d read err!\n",ch);
  1874. s_stat_rx_err(ch, 1);//接收错误计数
  1875. }
  1876. }
  1877. else
  1878. {
  1879. msleep(5);
  1880. if(ret < 0)
  1881. {
  1882. //rt_printf("\nch_%d select err!\n",ch);
  1883. s_stat_rx_err(ch, 1);//接收错误计数
  1884. }
  1885. if( ustimer_get_duration(prc->us0_recv) > prc->us_recv_timeout)
  1886. {
  1887. prc->b_recv_reset = 1;
  1888. }
  1889. }
  1890. //接收 end
  1891. #endif
  1892. #if 1
  1893. //发送 start
  1894. if(prc->bextsend
  1895. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  1896. || prc->bextsend_1
  1897. || prc->bextsend_2
  1898. || prc->bextsend_3
  1899. #endif
  1900. )
  1901. {
  1902. // 规约要求帧与帧之间间隔必须大于33位。
  1903. if((prc->extsendcnt== 0 // 帧头
  1904. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  1905. || prc->extsendcnt_1 == 0
  1906. || prc->extsendcnt_2 == 0
  1907. || prc->extsendcnt_3 == 0
  1908. #endif
  1909. )
  1910. && ((ustimer_get_duration(prc->us0_recv) < 5*USTIMER_MS) // 与接收帧之间的间隔
  1911. || (ustimer_get_duration(prc->us0_send) < 5*USTIMER_MS))) // 与发送帧之间的间隔
  1912. {
  1913. continue;
  1914. }
  1915. if(prc->extsendlen > 0)
  1916. {
  1917. send_fail_cnt = 0;
  1918. send_len = 0;
  1919. while(send_len<prc->extsendlen) // 发送没完成,继续发送
  1920. {
  1921. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1922. {
  1923. rt_printf("\r\nTX[%d]:",ch);
  1924. }
  1925. ret = uart_write(UART_CHANNEL[ch], (const char *)&prc->extsendbuf[prc->extsendcnt + send_len], (prc->extsendlen - send_len));
  1926. if(ret > 0)
  1927. {
  1928. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1929. {
  1930. for(i=0; i<ret ;i++)
  1931. {
  1932. rt_printf("%02x ",prc->extsendbuf[prc->extsendcnt + send_len + i]);
  1933. }
  1934. }
  1935. send_len += ret;
  1936. s_stat_tx(ch, ret);//发送字节数计数
  1937. }
  1938. else if(send_fail_cnt++ > 5)
  1939. {
  1940. send_fail_cnt = 0;
  1941. // rt_printf("\nch_%d send fail!!!\n",ch);
  1942. break;
  1943. }
  1944. }
  1945. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1946. {
  1947. rt_printf("\r\n");
  1948. }
  1949. {
  1950. prc->bextsend=false;
  1951. prc->extsendcnt = 0;
  1952. mainloop_wakeup();
  1953. prc->us0_send = ustimer_get_origin();//记录发送数据的时间戳
  1954. }
  1955. }
  1956. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  1957. else if(prc->extsendlen_1 > 0)
  1958. {
  1959. send_fail_cnt = 0;
  1960. send_len = 0;
  1961. while(send_len<prc->extsendlen_1) // 发送没完成,继续发送
  1962. {
  1963. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1964. {
  1965. rt_printf("\r\nTX[%d]:",ch);
  1966. }
  1967. ret = uart_write(UART_CHANNEL[ch], (const char *)&prc->extsendbuf_1[prc->extsendcnt_1 + send_len], (prc->extsendlen_1 - send_len));
  1968. if(ret > 0)
  1969. {
  1970. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1971. {
  1972. for(i=0; i<ret ;i++)
  1973. {
  1974. rt_printf("%02x ",prc->extsendbuf_1[prc->extsendcnt_1 + send_len + i]);
  1975. }
  1976. }
  1977. send_len += ret;
  1978. s_stat_tx(ch, ret);//发送字节数计数
  1979. }
  1980. else if(send_fail_cnt++ > 5)
  1981. {
  1982. send_fail_cnt = 0;
  1983. // rt_printf("\nch_%d send fail!!!\n",ch);
  1984. break;
  1985. }
  1986. }
  1987. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  1988. {
  1989. rt_printf("\r\n");
  1990. }
  1991. {
  1992. prc->bextsend_1=false;
  1993. prc->extsendcnt_1 = 0;
  1994. mainloop_wakeup();
  1995. prc->us0_send = ustimer_get_origin();//记录发送数据的时间戳
  1996. }
  1997. }
  1998. else if(prc->extsendlen_2 > 0)
  1999. {
  2000. send_fail_cnt = 0;
  2001. send_len = 0;
  2002. while(send_len<prc->extsendlen_2) // 发送没完成,继续发送
  2003. {
  2004. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  2005. {
  2006. rt_printf("\r\nTX[%d]:",ch);
  2007. }
  2008. ret = uart_write(UART_CHANNEL[ch], (const char *)&prc->extsendbuf_2[prc->extsendcnt_2 + send_len], (prc->extsendlen_2 - send_len));
  2009. if(ret > 0)
  2010. {
  2011. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  2012. {
  2013. for(i=0; i<ret ;i++)
  2014. {
  2015. rt_printf("%02x ",prc->extsendbuf_2[prc->extsendcnt_2 + send_len + i]);
  2016. }
  2017. }
  2018. send_len += ret;
  2019. s_stat_tx(ch, ret);//发送字节数计数
  2020. }
  2021. else if(send_fail_cnt++ > 5)
  2022. {
  2023. send_fail_cnt = 0;
  2024. // rt_printf("\nch_%d send fail!!!\n",ch);
  2025. break;
  2026. }
  2027. }
  2028. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  2029. {
  2030. rt_printf("\r\n");
  2031. }
  2032. {
  2033. prc->bextsend_2=false;
  2034. prc->extsendcnt_2 = 0;
  2035. mainloop_wakeup();
  2036. prc->us0_send = ustimer_get_origin();//记录发送数据的时间戳
  2037. }
  2038. }
  2039. else if(prc->extsendlen_3 > 0)
  2040. {
  2041. send_fail_cnt = 0;
  2042. send_len = 0;
  2043. while(send_len<prc->extsendlen_3) // 发送没完成,继续发送
  2044. {
  2045. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  2046. {
  2047. rt_printf("\r\nTX[%d]:",ch);
  2048. }
  2049. ret = uart_write(UART_CHANNEL[ch], (const char *)&prc->extsendbuf_3[prc->extsendcnt_3 + send_len], (prc->extsendlen_3 - send_len));
  2050. if(ret > 0)
  2051. {
  2052. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  2053. {
  2054. for(i=0; i<ret ;i++)
  2055. {
  2056. rt_printf("%02x ",prc->extsendbuf_3[prc->extsendcnt_3 + send_len + i]);
  2057. }
  2058. }
  2059. send_len += ret;
  2060. s_stat_tx(ch, ret);//发送字节数计数
  2061. }
  2062. else if(send_fail_cnt++ > 5)
  2063. {
  2064. send_fail_cnt = 0;
  2065. // rt_printf("\nch_%d send fail!!!\n",ch);
  2066. break;
  2067. }
  2068. }
  2069. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  2070. {
  2071. rt_printf("\r\n");
  2072. }
  2073. {
  2074. prc->bextsend_3=false;
  2075. prc->extsendcnt_3 = 0;
  2076. mainloop_wakeup();
  2077. prc->us0_send = ustimer_get_origin();//记录发送数据的时间戳
  2078. }
  2079. }
  2080. #endif
  2081. }
  2082. else if(prc->bSend) // 发送
  2083. {
  2084. // 规约要求帧与帧之间间隔必须大于33位。
  2085. if(prc->nSendCnt <= 1 // 帧头
  2086. && ((ustimer_get_duration(prc->us0_recv) < 5*USTIMER_MS) // 与接收帧之间的间隔
  2087. || (ustimer_get_duration(prc->us0_send) < 5*USTIMER_MS))) // 与发送帧之间的间隔
  2088. {
  2089. continue;
  2090. }
  2091. if(prc->sendbuf[0] > 0)
  2092. {
  2093. send_fail_cnt = 0;
  2094. send_len = 0;
  2095. while(send_len<prc->sendbuf[0]) // 发送没完成,继续发送
  2096. {
  2097. //TODO. noted by sunxi 这里也需要加select,并且write的select与read的select 的参数不一样。
  2098. ret = uart_write(UART_CHANNEL[ch], (const char *)&prc->sendbuf[prc->nSendCnt+send_len], (prc->sendbuf[0] - send_len));
  2099. if(ret > 0)
  2100. {
  2101. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  2102. {
  2103. if(send_len == 0)
  2104. {
  2105. rt_printf("\r\nTX[%d]:",ch);
  2106. }
  2107. for(i=0; i<ret ;i++)
  2108. {
  2109. if((ch+1) == CFG_UART_GPS)
  2110. {
  2111. rt_printf("%c",prc->sendbuf[prc->nSendCnt + send_len + i]);
  2112. }
  2113. else
  2114. {
  2115. rt_printf("%02x ",prc->sendbuf[prc->nSendCnt + send_len + i]);
  2116. }
  2117. }
  2118. }
  2119. send_len += ret;
  2120. s_stat_tx(ch, ret);//发送字节数计数
  2121. }
  2122. else if(send_fail_cnt++ > 5)
  2123. {
  2124. send_fail_cnt = 0;
  2125. rt_printf("\nch_%d send fail!!!\n",ch);
  2126. break;
  2127. }
  2128. if(g_print_comm_raw && (g_print_port & (1<<ch)))
  2129. {
  2130. if((ch+1) != CFG_UART_GPS)
  2131. rt_printf("\r\n");
  2132. }
  2133. }
  2134. {
  2135. #ifdef FLOW_CALC_FUNC
  2136. if(ch == tRunPara.flow_port)
  2137. {
  2138. g_sw_pub.ac_in[PUB_AC_IN_FLOW] += (prc->sendbuf[0] + 46)*Q16_BASE; //TCP+IP包头40个字节,数据部分1个字节,PPP头7E 21 ,PPP尾校验和2位+1个7E,总共是40+1+5=46个字节
  2139. }
  2140. #endif
  2141. if (uart_test_begin)
  2142. {
  2143. uart_test_flag[ch][0] = 1;
  2144. }
  2145. prc->bSend=false;
  2146. prc->nSendCnt = 0;
  2147. send_len = 0;
  2148. RS_Recv_Enable(ch);
  2149. if(tRunPara.tUartPara[ch].wProtocol==PROTOCOL_HMI)
  2150. {
  2151. Reset_Uart_Link((UART_COMM_STRUCT *)g_tRsComm[ch].ptBuf);
  2152. }
  2153. mainloop_wakeup();
  2154. prc->us0_send = ustimer_get_origin();//记录发送数据的时间戳
  2155. }
  2156. //prc->sendbuf[0] = 0;
  2157. }
  2158. }
  2159. //发送 end
  2160. #endif
  2161. }
  2162. rt_printf("\nch_%d thread out!!!!",ch);
  2163. #ifdef LINUX_KERNEL_APP
  2164. if(ts[ch])
  2165. {
  2166. kthread_stop(ts[ch]);
  2167. ts[ch] = NULL;
  2168. }
  2169. #endif
  2170. #ifdef CPU_FUXI
  2171. watchdog_remove_item(wdt_id[ch]);
  2172. #endif
  2173. return 0;
  2174. }
  2175. /*
  2176. 创建串口收发线程
  2177. ch: CFG_UART_NUM_MAX的i 序号
  2178. */
  2179. #ifdef LINUX_KERNEL_APP
  2180. int uart_isr_thread_create(int ch)
  2181. {
  2182. //int ret = 0;
  2183. char thread_name[256];
  2184. l_ch[ch] = ch;
  2185. //创建
  2186. memset(thread_name,0,sizeof(thread_name));
  2187. sprintf(thread_name, "uart_isr_%d", ch);
  2188. //printk("tobe create %s\r\n",thread_name);
  2189. ts[ch] = kthread_run(uart_isr_thread, (void *)(&l_ch[ch]), thread_name);
  2190. if(ts[ch] == NULL)
  2191. {
  2192. rt_printf("tobe create %s faild!!!!!!!!!\r\n",thread_name);
  2193. rt_printf("%s:ts=%p\r\n", __func__, ts[ch]);
  2194. return -1;
  2195. }
  2196. // 设置调度策略和优先级
  2197. sp[ch].sched_priority = 12;
  2198. sched_setscheduler(ts[ch], SCHED_FIFO, &sp[ch]);
  2199. return 0;
  2200. }
  2201. #else
  2202. int uart_isr_thread_create(int ch)
  2203. {
  2204. int ret;
  2205. l_ch[ch] = ch;
  2206. ret = pthread_create(&l_id[ch], NULL, (void *)uart_isr_thread, (void *)&l_ch[ch]);
  2207. if(ret != 0) {
  2208. printf("can not create thread uart_isr_thread: %d\r\n",errno);
  2209. return -1;
  2210. }
  2211. return 0;
  2212. }
  2213. #endif
  2214. #else
  2215. /**************************************************************************
  2216. 函数名称:uart1_isr
  2217. 函数版本:1.00
  2218. 作者:
  2219. 创建日期:2008.9.1
  2220. 函数功能说明:485口接收处理程序,在625中断中调用
  2221. 输入参数:
  2222. 输出参数:
  2223. 返回值:
  2224. 更新信息:
  2225. 更新日志1:
  2226. 日期:
  2227. 修改者:
  2228. 修改内容:
  2229. 修改原因:
  2230. ***************************************************************************/
  2231. void uart_isr(void) /* 串口处理*/
  2232. {
  2233. volatile unsigned char status;
  2234. volatile SERIAL_COMM *prc;
  2235. int cnt=0;
  2236. int i;
  2237. for(i=0;i<CFG_UART_NUM_MAX;i++)
  2238. {
  2239. s8 chnl=UART_CHANNEL[i];
  2240. if( chnl < 0
  2241. || chnl == g_con_uart_index
  2242. || tRunPara.tUartPara[i].wProtocol == PROTOCOL_IDLE)
  2243. {
  2244. continue;
  2245. }
  2246. // 如果GPS对时打开,必须关闭串口。
  2247. if(g_clock_mode_gps && chnl == CFG_UART_GPS)
  2248. {
  2249. continue;
  2250. }
  2251. //如果是使用内部GPRS,则不用再走下面的代码流程
  2252. if(chnl==CFG_UART_GPRS_IN )
  2253. {
  2254. continue;
  2255. }
  2256. #ifdef FUN_FUXI_ESAM
  2257. else if(chnl==CFG_UART_CHIP_S1 )
  2258. {
  2259. continue;
  2260. }
  2261. #endif
  2262. //面板做特殊处理
  2263. if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_HMI)
  2264. {
  2265. //规约处理
  2266. protocol_proc_task(i);
  2267. continue;
  2268. }
  2269. //如果是数显仪表
  2270. if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_SXYB)
  2271. {
  2272. //数显仪表处理
  2273. sxyb_proc(chnl);
  2274. continue;
  2275. }
  2276. prc=&g_tRsComm[i];
  2277. // 接收超时检查
  2278. status=MCF_UART_USR(chnl);
  2279. if((status& MCF_UART_USR_RXRDY) == 0)
  2280. {
  2281. if( ustimer_get_duration(prc->us0_recv) > prc->us_recv_timeout)
  2282. {
  2283. prc->b_recv_reset = 1;
  2284. }
  2285. }
  2286. // 接收
  2287. while (status& MCF_UART_USR_RXRDY) // 接收数据
  2288. {
  2289. if((!(status&MCF_UART_USR_PE))&&(!(status&MCF_UART_USR_FE)))
  2290. {
  2291. u8 c;
  2292. c = MCF_UART_URB(chnl);
  2293. IEC_Recv(c,i);
  2294. if(g_print_comm_raw && (g_print_port & (1<<i)))
  2295. {
  2296. rt_printf("%02x ",c);
  2297. }
  2298. s_stat_rx(i, 1);//接收字节数计数
  2299. prc->us0_recv = ustimer_get_origin();//记录接收数据的时间戳
  2300. g_run_stu.led[i*2+1]=1; //接收灯
  2301. }
  2302. else
  2303. {
  2304. status=MCF_UART_URB(chnl);
  2305. MCF_UART_UCR(chnl) = MCF_UART_UCR_RESET_ERROR; // 接收出错,清标志
  2306. s_stat_rx_err(i, 1);//接收错误计数
  2307. }
  2308. status=MCF_UART_USR(chnl);
  2309. if(++cnt>2)break;
  2310. }
  2311. // 发送
  2312. if(prc->bextsend
  2313. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  2314. || prc->bextsend_1
  2315. || prc->bextsend_2
  2316. || prc->bextsend_3
  2317. #endif
  2318. )
  2319. {
  2320. // 规约要求帧与帧之间间隔必须大于33位。
  2321. if((prc->extsendcnt== 0 // 帧头
  2322. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  2323. || prc->extsendcnt_1 == 0
  2324. || prc->extsendcnt_2 == 0
  2325. || prc->extsendcnt_3 == 0
  2326. #endif
  2327. )
  2328. && ((ustimer_get_duration(prc->us0_recv) < 5*USTIMER_MS) // 与接收帧之间的间隔
  2329. || (ustimer_get_duration(prc->us0_send) < 5*USTIMER_MS))) // 与发送帧之间的间隔
  2330. {
  2331. continue;
  2332. }
  2333. if(prc->extsendcnt<prc->extsendlen
  2334. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  2335. || (((prc->bextsend_1 && (prc->extsendcnt_1<prc->extsendlen_1))
  2336. || (prc->bextsend_2 && (prc->extsendcnt_2<prc->extsendlen_2))
  2337. || (prc->bextsend_3 && (prc->extsendcnt_3<prc->extsendlen_3))) && gb_bready)
  2338. #endif
  2339. ) // 发送没完成,继续发送
  2340. {
  2341. if (status& MCF_UART_USR_TXRDY) // 可发送数据
  2342. {
  2343. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  2344. if(prc->bextsend_1)
  2345. {
  2346. MCF_UART_UTB(chnl) = prc->extsendbuf_1[prc->extsendcnt_1];
  2347. prc->extsendcnt_1++;
  2348. if((prc->extsendcnt_1+1) > prc->extsendlen_1)
  2349. {
  2350. gb_bready = false;
  2351. }
  2352. }
  2353. else if(prc->bextsend_2)
  2354. {
  2355. if(!prc->bextsend_1)
  2356. {
  2357. MCF_UART_UTB(chnl) = prc->extsendbuf_2[prc->extsendcnt_2];
  2358. prc->extsendcnt_2++;
  2359. }
  2360. if((prc->extsendcnt_2+1) > prc->extsendlen_2)
  2361. {
  2362. gb_bready = false;
  2363. }
  2364. }
  2365. else if(prc->bextsend_3)
  2366. {
  2367. if(!prc->bextsend_1 && !prc->bextsend_2)
  2368. {
  2369. MCF_UART_UTB(chnl) = prc->extsendbuf_3[prc->extsendcnt_3];
  2370. prc->extsendcnt_3++;
  2371. }
  2372. if((prc->extsendcnt_3+1) > prc->extsendlen_3)
  2373. {
  2374. gb_bready = false;
  2375. }
  2376. }
  2377. else
  2378. #endif
  2379. {
  2380. MCF_UART_UTB(chnl) = prc->extsendbuf[prc->extsendcnt];
  2381. if(g_print_comm_raw && (g_print_port & (1<<i)))
  2382. {
  2383. if(prc->extsendcnt == 0)
  2384. {
  2385. rt_printf("\r\nTX[%d]:",i);
  2386. }
  2387. rt_printf("%02x ",prc->extsendbuf[prc->extsendcnt]);
  2388. if(prc->extsendcnt == prc->extsendlen-1)
  2389. {
  2390. rt_printf("\r\n");
  2391. }
  2392. }
  2393. prc->extsendcnt++;
  2394. }
  2395. s_stat_tx(i, 1);//发送字节数计数
  2396. g_run_stu.led[i*2+0]=1; //发送灯
  2397. }
  2398. }
  2399. else if(status & MCF_UART_USR_TXEMP)
  2400. {
  2401. #ifdef FUNC_ENCRY_IN_ONE_SERIAL
  2402. if(prc->bextsend_1)
  2403. prc->bextsend_1=false;
  2404. else if(prc->bextsend_2)
  2405. prc->bextsend_2=false;
  2406. else if(prc->bextsend_3)
  2407. prc->bextsend_3=false;
  2408. gb_bready = true;
  2409. if(prc->extsendcnt_1 != 0)
  2410. prc->extsendcnt_1 = 0;
  2411. else if(prc->extsendcnt_2 != 0)
  2412. prc->extsendcnt_2 = 0;
  2413. else if(prc->extsendcnt_3 != 0)
  2414. prc->extsendcnt_3 = 0;
  2415. #endif
  2416. prc->bextsend=false;
  2417. prc->extsendcnt = 0;
  2418. mainloop_wakeup();
  2419. prc->us0_send = ustimer_get_origin();//记录发送数据的时间戳
  2420. }
  2421. }
  2422. else if(prc->bSend) // 发送
  2423. {
  2424. // 规约要求帧与帧之间间隔必须大于33位。
  2425. if(prc->nSendCnt <= 1 // 帧头
  2426. && ((ustimer_get_duration(prc->us0_recv) < 5*USTIMER_MS) // 与接收帧之间的间隔
  2427. || (ustimer_get_duration(prc->us0_send) < 5*USTIMER_MS))) // 与发送帧之间的间隔
  2428. {
  2429. continue;
  2430. }
  2431. if(prc->nSendCnt<=prc->sendbuf[0]) // 发送没完成,继续发送
  2432. {
  2433. if (status& MCF_UART_USR_TXRDY) // 可发送数据
  2434. {
  2435. MCF_UART_UTB(chnl) = prc->sendbuf[prc->nSendCnt];
  2436. if(g_print_comm_raw && (g_print_port & (1<<i)))
  2437. {
  2438. if(prc->nSendCnt <= 1)
  2439. {
  2440. rt_printf("\r\nTX[%d]:",i);
  2441. }
  2442. rt_printf("%02x ",prc->sendbuf[prc->nSendCnt]);
  2443. if(prc->nSendCnt == prc->sendbuf[0])
  2444. {
  2445. rt_printf("\r\n");
  2446. }
  2447. }
  2448. prc->nSendCnt++;
  2449. s_stat_tx(i, 1);//发送字节数计数
  2450. g_run_stu.led[i*2+0]=1; //发送灯有效
  2451. }
  2452. }
  2453. else if(status & MCF_UART_USR_TXEMP)
  2454. {
  2455. if (uart_test_begin)
  2456. {
  2457. uart_test_flag[i][0] = 1;
  2458. }
  2459. prc->bSend=false;
  2460. prc->nSendCnt = 0;
  2461. RS_Recv_Enable(i);
  2462. if(tRunPara.tUartPara[i].wProtocol==PROTOCOL_HMI)
  2463. {
  2464. Reset_Uart_Link((UART_COMM_STRUCT *)g_tRsComm[i].ptBuf);
  2465. }
  2466. mainloop_wakeup();
  2467. prc->us0_send = ustimer_get_origin();//记录发送数据的时间戳
  2468. }
  2469. }
  2470. }
  2471. }
  2472. #endif
  2473. void uart_232_485_test(u8 *buf)
  2474. {
  2475. int i;
  2476. u8 dat[6] = {0x10, 0x40, 0x01, 0x00, 0x41,0x16};
  2477. memset (uart_test_flag, 0, sizeof(uart_test_flag));
  2478. // RS232测试
  2479. if (buf[0])
  2480. {
  2481. for (i=0; i<2; i++)
  2482. {
  2483. g_tRsComm[i].sendbuf[0] = 6;
  2484. memcpy(&g_tRsComm[i].sendbuf[1],dat,6);
  2485. RS_Send_Enable(i);
  2486. g_tRsComm[i].nSendCnt=1;
  2487. g_tRsComm[i].bSend=true;
  2488. rt_printf("TX_101_%d:",i);
  2489. print_msg("",&g_tRsComm[i].sendbuf[1],g_tRsComm[i].sendbuf[0]);
  2490. }
  2491. }
  2492. // RS485测试
  2493. if (buf[1])
  2494. {
  2495. for (i=3; i<6; i++)
  2496. {
  2497. g_tRsComm[i].sendbuf[0] = 6;
  2498. memcpy(&g_tRsComm[i].sendbuf[1],dat,6);
  2499. RS_Send_Enable(i);
  2500. g_tRsComm[i].nSendCnt=1;
  2501. g_tRsComm[i].bSend=true;
  2502. rt_printf("TX_101_%d:",i);
  2503. print_msg("",&g_tRsComm[i].sendbuf[1],g_tRsComm[i].sendbuf[0]);
  2504. }
  2505. }
  2506. // 延时2ms,以保证通讯完毕
  2507. ustimer_delay(200*USTIMER_MS);
  2508. msleep(200);//nodify by sunxi for 335x
  2509. IECCommTask();
  2510. {
  2511. u8 i;
  2512. for(i=0; i<CFG_UART_NUM_MAX-1; i++)
  2513. {
  2514. rt_printf("chnl = %d, send=%d, recv=%d\r\n",
  2515. i, uart_test_flag[i][0], uart_test_flag[i][1]);
  2516. }
  2517. }
  2518. }
  2519. void iec_GetMsgInfo(u8 *buf,int len,u8 *ti,u8 *cot,u8 *se,bool b101)
  2520. {
  2521. int i;
  2522. u8 AddByte=0;
  2523. u8 CotByte=0; //传送原因双字节
  2524. u8 AppByte=0; //应用单元地址双字节
  2525. u8 sel;
  2526. // 所有定长帧的长度都不大于6个字节,不定长帧都大于等于15字节
  2527. if(len < 8)
  2528. {
  2529. *ti = 0;
  2530. *cot = 0;
  2531. *se = 0;
  2532. return;
  2533. }
  2534. if(b101)
  2535. {
  2536. if(tRunPara.b101Addr2Byte) AddByte=1;
  2537. if(tRunPara.b101Cot2Byte) CotByte=1;
  2538. if(tRunPara.b101App2Byte) AppByte=1;
  2539. sel=buf[12+AddByte+CotByte+AppByte];
  2540. }
  2541. else
  2542. {
  2543. sel=buf[15];
  2544. }
  2545. // 基本信息
  2546. *ti = buf[6+AddByte];
  2547. *cot = buf[8+AddByte];
  2548. *se = 0;
  2549. if(*ti==0xcb) // 参数修改,选择位不同
  2550. {
  2551. if(!b101)
  2552. {
  2553. sel=buf[14];
  2554. }
  2555. }
  2556. // 更新se
  2557. for(i=0;i<TI_SELECT_NUM; i++)
  2558. {
  2559. if(*ti == g_ti_select[i])
  2560. {
  2561. *se = (sel&0x80)? 1 : 0;
  2562. break;
  2563. }
  2564. }
  2565. if(*ti==210) // 文件操作
  2566. {
  2567. BYTE cmd;
  2568. if(b101)
  2569. {
  2570. cmd=buf[13+AddByte+CotByte+AppByte]; // 文件操作命令码
  2571. }
  2572. else
  2573. {
  2574. cmd=buf[16];
  2575. }
  2576. if(cmd==7&&*cot==6) // 写文件激活与其他文件类操作不同
  2577. {
  2578. *se=1;
  2579. }
  2580. else
  2581. {
  2582. *se=0;
  2583. }
  2584. }
  2585. return ;
  2586. }
  2587. /*************远方修改定值,固定参数初始化************************/
  2588. #define MAX_UTF8_NUM 64
  2589. typedef struct FIXSET_UTF8
  2590. {
  2591. int num;
  2592. u16 intcode[MAX_UTF8_NUM];
  2593. u16 unicode[MAX_UTF8_NUM];
  2594. }FIXSET_UTF8_DEF;
  2595. FIXSET_UTF8_DEF tUtf8;
  2596. int iec_findchardot(u8 *buf,u8 *str)
  2597. {
  2598. u8 *ps;
  2599. int i,lenth=0;
  2600. ps=strchr(buf,',');
  2601. if(ps) // 存在','
  2602. {
  2603. lenth=ps-buf;
  2604. for(i=0;i<lenth;i++)
  2605. {
  2606. str[i]=*buf++;
  2607. }
  2608. str[i]='\0';
  2609. }
  2610. return lenth;
  2611. }
  2612. static void iec_getfixsetinf(RMT_FIXED_TABLE *pfixedset,u8 *buf)
  2613. {
  2614. u8 str[128],type=0;
  2615. int len,i;
  2616. len=iec_findchardot(buf,str);
  2617. if(len==0)return;
  2618. if(strcmp(str,"FIXSET")==0) //固定参数
  2619. {
  2620. buf+=len+1;
  2621. len=iec_findchardot(buf,str);
  2622. if(len==0)return;
  2623. type=strtoul(str,NULL,10);
  2624. for(i=0;i<pfixedset->num;i++)
  2625. {
  2626. if(type==pfixedset->set[i].type)
  2627. {
  2628. buf+=len+1;
  2629. len=iec_findchardot(buf,str); // 取输入点号
  2630. if(len==0)return;
  2631. if(type==FIXED_SET_CRC)
  2632. {
  2633. WORD crc;
  2634. crc=strtoul(str,NULL,16);
  2635. pfixedset->set[i].str[0]=(BYTE)(crc);
  2636. pfixedset->set[i].str[1]=(BYTE)(crc>>8);
  2637. pfixedset->set[i].len=2;
  2638. }
  2639. else
  2640. {
  2641. strcpy(pfixedset->set[i].str,str);
  2642. pfixedset->set[i].len=strlen(pfixedset->set[i].str);
  2643. }
  2644. break;
  2645. }
  2646. }
  2647. }
  2648. else if(strcmp(str,"UNICODE")==0) //固定参数
  2649. {
  2650. if(tUtf8.num>=MAX_UTF8_NUM)return;
  2651. buf+=len+1;
  2652. len=iec_findchardot(buf,str);
  2653. if(len==0)return;
  2654. tUtf8.intcode[tUtf8.num]=str[1]|(str[0]<<8); //汉字内码
  2655. buf+=len+1;
  2656. len=iec_findchardot(buf,str); //
  2657. tUtf8.unicode[tUtf8.num]=strtoul(str,NULL,16); // unicode
  2658. tUtf8.num++;
  2659. }
  2660. }
  2661. int iec_get_fixedset_csv(RMT_FIXED_TABLE *pfixedset)
  2662. {
  2663. u32 i,file_length;
  2664. struct file * pfile;
  2665. u8 *filebuf;
  2666. u8 filestr[64],*pstr,strlenth;
  2667. loff_t pos;
  2668. // 打开文件
  2669. pfile = rt_file_open("/app/data/fixset.csv",O_RDONLY ,0);
  2670. if(IS_ERR(pfile))
  2671. {
  2672. // rt_printf("error! 无法打开 iec_cfg.csv\r\n");
  2673. return -1;
  2674. }
  2675. // 得到文件长度
  2676. file_length = rt_file_getfile_size(pfile);
  2677. if(file_length == 0)
  2678. {
  2679. rt_file_close(pfile,0);
  2680. return -2;
  2681. }
  2682. // 分配内存
  2683. filebuf = rt_malloc(file_length);
  2684. if((filebuf) == NULL)
  2685. {
  2686. rt_file_close(pfile,0);
  2687. return -3;
  2688. }
  2689. pos = 0;
  2690. if(rt_file_read(pfile,filebuf,file_length,&pos) != file_length)
  2691. {
  2692. rt_file_close(pfile,0);
  2693. rt_free(filebuf);
  2694. return -4;
  2695. }
  2696. pstr = filestr;
  2697. strlenth=0; // 纠错处理
  2698. //找到\r\n位置,得到一行长度
  2699. tUtf8.num=0;
  2700. for(i = 0; i < file_length; i++)
  2701. {
  2702. *pstr++=filebuf[i];
  2703. strlenth++;
  2704. if(filebuf[i] == 0x0a||strlenth>=64)
  2705. {
  2706. pstr-=2; // 去掉回车换行
  2707. *pstr='\0';
  2708. iec_getfixsetinf(pfixedset,filestr);
  2709. pstr=filestr;
  2710. strlenth=0;
  2711. }
  2712. }
  2713. rt_file_close(pfile,0); // 关闭文件
  2714. rt_free(filebuf); // 释放内存
  2715. return 0;
  2716. }
  2717. void iec_init_fixedset(void *pt,RMT_FIXED_TABLE *pfixedset,bool b104)
  2718. {
  2719. int i;
  2720. WORD num,no;
  2721. num=0;
  2722. for(i=0;i<FIXEDSET_TABLE_NUMBER;i++)
  2723. {
  2724. pfixedset->set[num].type=tfixedsettable[i].index;
  2725. switch(tfixedsettable[i].index)
  2726. {
  2727. case FIXED_SET_CRC:
  2728. pfixedset->set[num].str[0]=(BYTE)(m_CodeCrc);
  2729. pfixedset->set[num].str[1]=(BYTE)(m_CodeCrc>>8);
  2730. pfixedset->set[num].len=2;
  2731. break;
  2732. case FIXED_SET_MAC1:
  2733. case FIXED_SET_MAC2:
  2734. no=tfixedsettable[i].index-FIXED_SET_MAC1;
  2735. rt_if_mac_get(no,pfixedset->set[num].str);
  2736. pfixedset->set[num].len=6;
  2737. break;
  2738. case FIXED_SET_SVR: // 软件版本
  2739. sprintf(pfixedset->set[num].str,"SV%02d.%03d",VER_NUM/1000,VER_NUM%1000);
  2740. pfixedset->set[num].len=strlen(pfixedset->set[num].str);
  2741. break;
  2742. case FIXED_SET_ID:
  2743. if(env_get_info(ENV_PID))
  2744. {
  2745. strcpy(pfixedset->set[num].str,env_get_info(ENV_PID));
  2746. }
  2747. else
  2748. {
  2749. strcpy(pfixedset->set[num].str,"");
  2750. }
  2751. pfixedset->set[num].len=strlen(pfixedset->set[num].str);
  2752. break;
  2753. case FIXED_SET_COM:
  2754. if(b104)
  2755. {
  2756. #ifndef GD_AREA_ECZD_JIEYANG_2022
  2757. strcpy(pfixedset->set[num].str,"2016版扩展104");
  2758. #else
  2759. strcpy(pfixedset->set[num].str,"104");
  2760. #endif
  2761. }
  2762. else
  2763. {
  2764. #ifndef GD_AREA_ECZD_JIEYANG_2022
  2765. IEC101_DEF *pt101=(IEC101_DEF *)pt;
  2766. if(tRunPara.tUartPara[pt101->chnl].wProtocol==PROTOCOL_101_PH)
  2767. {
  2768. strcpy(pfixedset->set[num].str,"2016版扩展平衡式101");
  2769. }
  2770. else
  2771. {
  2772. strcpy(pfixedset->set[num].str,"2016版扩展非平衡式101");
  2773. }
  2774. #else
  2775. strcpy(pfixedset->set[num].str,"101");
  2776. #endif
  2777. }
  2778. pfixedset->set[num].len=strlen(pfixedset->set[num].str);
  2779. break;
  2780. case FIXED_SET_CPT:
  2781. strcpy(pfixedset->set[num].str,VER_TIME);
  2782. pfixedset->set[num].len=strlen(pfixedset->set[num].str);
  2783. break;
  2784. default:
  2785. strcpy(pfixedset->set[num].str,tfixedsettable[i].vstr);
  2786. pfixedset->set[num].len=strlen(tfixedsettable[i].vstr);
  2787. }
  2788. pfixedset->set[num].di=tfixedsettable[i].di;
  2789. pfixedset->set[num].unit=tfixedsettable[i].unit;
  2790. num++;
  2791. }
  2792. pfixedset->num=num;
  2793. iec_get_fixedset_csv(pfixedset); // 从csv文件中更新相关固定参数信息
  2794. }
  2795. int enc_unicode_to_utf8_one(unsigned short unic, unsigned char *pOutput)
  2796. {
  2797. if ( unic <= 0x007F )
  2798. {
  2799. // * U-00000000 - U-0000007F: 0xxxxxxx
  2800. *pOutput = (unic & 0x7F);
  2801. return 1;
  2802. }
  2803. else if ( unic >= 0x0080 && unic <= 0x07FF )
  2804. {
  2805. // * U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
  2806. *(pOutput+1) = (unic & 0x3F) | 0x80;
  2807. *pOutput = ((unic >> 6) & 0x1F) | 0xC0;
  2808. return 2;
  2809. }
  2810. else if ( unic >= 0x0800 && unic <= 0xFFFF )
  2811. {
  2812. // * U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
  2813. *(pOutput+2) = (unic & 0x3F) | 0x80;
  2814. *(pOutput+1) = ((unic >> 6) & 0x3F) | 0x80;
  2815. *pOutput = ((unic >> 12) & 0x0F) | 0xE0;
  2816. return 3;
  2817. }
  2818. return 0;
  2819. }
  2820. BYTE iec_GBK2Utf8(BYTE *dst,BYTE *src,BYTE srclen)
  2821. {
  2822. int i;
  2823. int len=0;
  2824. for(i=0;i<srclen;)
  2825. {
  2826. if(src[i]>0x80) //是汉字
  2827. {
  2828. u16 intcode,unicode;
  2829. int ulflen;
  2830. int j;
  2831. intcode=src[i+1]|(src[i]<<8);
  2832. unicode=0;
  2833. for(j=0;j<tUtf8.num;j++)
  2834. {
  2835. if(tUtf8.intcode[j]==intcode) // 内码相同,取内码对应的unicode
  2836. {
  2837. unicode=tUtf8.unicode[j];
  2838. break;
  2839. }
  2840. }
  2841. if(unicode>0)
  2842. {
  2843. ulflen=enc_unicode_to_utf8_one(unicode,&dst[len]);
  2844. len+=ulflen;
  2845. }
  2846. i+=2;
  2847. }
  2848. else
  2849. {
  2850. dst[len]=src[i];
  2851. len++;
  2852. i++;
  2853. }
  2854. }
  2855. return len;
  2856. }
  2857. BYTE iec_GBK2Unicode(BYTE *dst,BYTE *src,BYTE srclen)
  2858. {
  2859. int i;
  2860. int len=0;
  2861. for(i=0;i<srclen;)
  2862. {
  2863. if(src[i]>0x80) //是汉字
  2864. {
  2865. u16 intcode,unicode;
  2866. int j;
  2867. intcode=src[i+1]|(src[i]<<8);
  2868. unicode=0;
  2869. for(j=0;j<tUtf8.num;j++)
  2870. {
  2871. if(tUtf8.intcode[j]==intcode) // 内码相同,取内码对应的unicode
  2872. {
  2873. unicode=tUtf8.unicode[j];
  2874. break;
  2875. }
  2876. }
  2877. if(unicode>0)
  2878. {
  2879. dst[len]=(u8)unicode;
  2880. dst[len+1]=(u8)(unicode>>8);
  2881. len+=2;
  2882. }
  2883. i+=2;
  2884. }
  2885. else
  2886. {
  2887. dst[len]=src[i];
  2888. len++;
  2889. i++;
  2890. }
  2891. }
  2892. return len;
  2893. }
  2894. int iec_add2frame(BYTE *pbuf,RMT_SET_VAL *pset,bool b104)
  2895. {
  2896. int onebyte=0;
  2897. #ifdef FIX_PARAM_FORMAT
  2898. bool bfixset=false;
  2899. #endif
  2900. *pbuf++=(BYTE)pset->di;
  2901. *pbuf++=(BYTE)(pset->di>>8);
  2902. if(b104)
  2903. {
  2904. *pbuf++=0; // 104规约信息体地址为三个字节
  2905. onebyte=1;
  2906. }
  2907. #ifdef FIX_PARAM_FORMAT
  2908. if((pset->di>0x8000)&&(pset->di<0x8020)&&(pset->unit == STR_R))
  2909. {
  2910. bfixset=true;
  2911. }
  2912. if(pRunSet->bTT_Utf8&&bfixset)
  2913. {
  2914. BYTE len;
  2915. BYTE tmpbuf[128];
  2916. len=iec_GBK2Utf8(tmpbuf,pset->str,pset->len);
  2917. *pbuf++=pset->unit;
  2918. *pbuf++=len;
  2919. memcpy(pbuf,tmpbuf,len);
  2920. return (len+onebyte+4);
  2921. }
  2922. else if(pRunSet->bTT_Unicode&&bfixset)
  2923. {
  2924. BYTE len;
  2925. BYTE tmpbuf[128];
  2926. len=iec_GBK2Unicode(tmpbuf,pset->str,pset->len);
  2927. *pbuf++=pset->unit;
  2928. *pbuf++=len;
  2929. memcpy(pbuf,tmpbuf,len);
  2930. return (len+onebyte+4);
  2931. }
  2932. else
  2933. #endif
  2934. {
  2935. *pbuf++=pset->unit;
  2936. *pbuf++=pset->len;
  2937. memcpy(pbuf,pset->str,pset->len);
  2938. return (pset->len+onebyte+4);
  2939. }
  2940. return 0;
  2941. }
  2942. int iec_add2frame_dz(BYTE *pbuf,RMT_SET_VAL *pset,bool b104)
  2943. {
  2944. int onebyte=0;
  2945. *pbuf++=(BYTE)pset->di;
  2946. *pbuf++=(BYTE)(pset->di>>8);
  2947. if(b104)
  2948. {
  2949. *pbuf++=0; // 104规约信息体地址为三个字节
  2950. onebyte=1;
  2951. }
  2952. memcpy(pbuf,pset->str,pset->len);
  2953. return (pset->len+onebyte+2);
  2954. }
  2955. void iec_set2rmtset( RMT_SET_VAL *rmtset,WORD Tag,WORD group,WORD setno,float fv )
  2956. {
  2957. u32 ll;
  2958. switch(Tag)
  2959. {
  2960. case BOOL_R:
  2961. case TINY_R:
  2962. case UTINY_R:
  2963. rmtset->len=1;
  2964. rmtset->str[0]=(BYTE)fv;
  2965. break;
  2966. case UINT_R:
  2967. case INT_R:
  2968. case LONG_R:
  2969. case ULONG_R:
  2970. rmtset->len=4;
  2971. ll=(u32)fv;
  2972. rmtset->str[0]=(BYTE)(ll>>0);
  2973. rmtset->str[1]=(BYTE)(ll>>8);
  2974. rmtset->str[2]=(BYTE)(ll>>16);
  2975. rmtset->str[3]=(BYTE)(ll>>24);
  2976. break;
  2977. case SHORT_R:
  2978. case USHORT_R:
  2979. rmtset->len=2;
  2980. ll=(u32)fv;
  2981. rmtset->str[0]=(BYTE)(ll>>0);
  2982. rmtset->str[1]=(BYTE)(ll>>8);
  2983. break;
  2984. case FLOAT_R:
  2985. {
  2986. union{
  2987. float ff;
  2988. u8 tt[4];
  2989. }ff;
  2990. rmtset->len=4;
  2991. ff.ff=fv;
  2992. CopySwap(rmtset->str,ff.tt,sizeof(ff.tt),true);
  2993. }
  2994. break;
  2995. case STR_R:
  2996. {
  2997. union{
  2998. float ff;
  2999. u8 tt[4];
  3000. }ff;
  3001. WORD wType=0;
  3002. ff.ff=fv;
  3003. if(group==SETGROUP_TYPE_PUB)
  3004. {
  3005. wType = tPubSetTable[setno].wType;
  3006. }
  3007. else if(group==SETGROUP_TYPE_BH)
  3008. {
  3009. wType = tSwSetTable[setno].wType;
  3010. }
  3011. else if(group==SETGROUP_TYPE_PARA)
  3012. {
  3013. wType = tEquipParaTable[setno].wType;
  3014. }
  3015. else if(group==SETGROUP_TYPE_CSTSET)
  3016. {
  3017. wType = tCstSetTable[setno].wType;
  3018. }
  3019. else
  3020. {
  3021. rmtset->len=0;
  3022. break;
  3023. }
  3024. if(wType == SETTYPE_IP)
  3025. {
  3026. sprintf(rmtset->str,"%d.%d.%d.%d",ff.tt[3],ff.tt[2],ff.tt[1],ff.tt[0]);
  3027. rmtset->len=strlen(rmtset->str);
  3028. }
  3029. else
  3030. {
  3031. rmtset->len=0;
  3032. break;
  3033. }
  3034. }
  3035. break;
  3036. default:
  3037. rmtset->len=0;
  3038. break;
  3039. }
  3040. rmtset->unit=Tag;
  3041. }
  3042. //佛山局参数低位在前,高位在后
  3043. //浮点->缓冲区
  3044. static void float2IECbuf(BYTE *buf,float f)
  3045. {
  3046. union
  3047. {
  3048. float ff;
  3049. u8 tt[4];
  3050. }ff;
  3051. ff.ff = f;
  3052. CopySwap(buf,ff.tt,sizeof(ff.tt),true);
  3053. }
  3054. //缓冲区->浮点数
  3055. static float IECbuf2Foat(BYTE *buf)
  3056. {
  3057. union
  3058. {
  3059. float ff;
  3060. u8 tt[4];
  3061. }ff;
  3062. CopySwap(ff.tt,buf,4,false);
  3063. return ff.ff;
  3064. }
  3065. //获取参数信息定义的偏移量
  3066. static int getSetTableOffset(const TSETTABLE *pTable,WORD maxTable, WORD parId,WORD set_group, bool b_write)
  3067. {
  3068. int i;
  3069. #if 0
  3070. for(i=0;i<maxTable;i++)
  3071. {
  3072. int j;
  3073. for(j=0;j<ParaIDNum;j++)
  3074. {
  3075. if(tParaID[j].parId==parId&&tParaID[j].group_type==set_group)
  3076. {
  3077. rt_printf("%s Type=%d %d i=%d\r\n",pTable[i].szName,parId,set_group,i);
  3078. return i;
  3079. }
  3080. }
  3081. }
  3082. #endif
  3083. #ifdef FUNC_YT_HARD_YB
  3084. if(set_group != SETGROUP_TYPE_BY)
  3085. #endif
  3086. {
  3087. for(i=0; i<ParaIDNum; i++)
  3088. {
  3089. if(tParaID[i].parId==parId && tParaID[i].group_type==set_group && parId)
  3090. {
  3091. #ifdef FUNC_GET_RD_ONLY
  3092. if(b_write)
  3093. {
  3094. if(!tParaID[i].b_rd_only)
  3095. {
  3096. return tParaID[i].setno;
  3097. //return i;
  3098. }
  3099. else
  3100. {
  3101. rt_printf("%x为只读定值,禁止写!\r\n",parId);
  3102. return -1;
  3103. }
  3104. }
  3105. else
  3106. #endif
  3107. {
  3108. //rt_printf("%s Type=%d %d i=%d\r\n",pTable[i].szName,parId,set_group,i);
  3109. return tParaID[i].setno;
  3110. //return i;
  3111. }
  3112. }
  3113. }
  3114. }
  3115. #ifdef FUNC_YT_HARD_YB
  3116. else
  3117. {
  3118. #ifdef FUNC_YT_MIX
  3119. for(i=0; i<(YT_SW_HARD_YB_NUM+YT_PUB_HARD_YB_NUM+MIX_YT_NUM); i++)
  3120. #else
  3121. for(i=0; i<(YT_SW_HARD_YB_NUM+YT_PUB_HARD_YB_NUM); i++)
  3122. #endif
  3123. {
  3124. if((tHardYBID[i]==parId) && (parId != 0))
  3125. {
  3126. if(b_write)
  3127. {
  3128. return -1;
  3129. }
  3130. else
  3131. {
  3132. return i;
  3133. }
  3134. }
  3135. }
  3136. }
  3137. #endif
  3138. return -1;
  3139. }
  3140. //参数预置
  3141. bool selectRunParId(WORD sw, WORD parId, BYTE *pbuf)
  3142. {
  3143. int parTable=0;
  3144. float value = 0;
  3145. if(sw > SWITCH_NUM_MAX)
  3146. return false;
  3147. #ifndef FUNC_MORE_PRESET
  3148. //放在所有参数处理的前面,包括只读定值在内的都需进行比较和统计
  3149. if(bfirst_sel) //wirteRunParId中对selectRunParId有调用,为避免数组记录内容出错,限定为预置参数时(bfirst_sel=true)才允许写比较数组
  3150. {
  3151. buf_forCompare[i_forCompare++] = (BYTE)(parId >> 8);
  3152. buf_forCompare[i_forCompare++] = (BYTE)parId;
  3153. memcpy(&buf_forCompare[i_forCompare],pbuf,4);
  3154. i_forCompare += 4;
  3155. }
  3156. #endif
  3157. #if defined GD_AREA_FOS || defined GD_AREA_FOS_2021 || defined FOS_2021
  3158. if(parId==0x5001 || parId==0x5002)//佛山局要求5001:常规继电保护模式 0x5002 常规继电保护定值区号 只读,不可更改
  3159. return false;
  3160. #ifdef FOS_2021
  3161. if(parId==0x5024) //远方整定投入软压板
  3162. {
  3163. rt_printf("远方整定投入软压板不允许远方修改\r\n");
  3164. return false;
  3165. }
  3166. #endif
  3167. #elif defined GD_AREA_SHANWEI
  3168. if(parId==0x500b || parId==0x5045)//汕尾局要求500b:常规继电保护模式 5045:常规FA自动化模式 只读,不可更改
  3169. return false;
  3170. #elif defined GD_AREA_ECZD_MAOMING
  3171. //茂名局要求5001:定值区号 5013:常规继电保护模式/5051:电压电流型继电保护模式/508d:智能分布式保护模式/50ab:转换把手位置/504c、507e:远方整定投入软压板 只读,不可更改
  3172. if(parId==0x5001 || parId==0x5013 || parId==0x5051 || parId==0x508d || parId==0x50ab || parId==0x504c || parId==0x507e)
  3173. return false;
  3174. #elif defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_ZHONGSHAN_2020_ZC || defined GD_AREA_ZHONGSHAN
  3175. /*19年中山要求5001:保护总投退 5002:电流型保护模式投退 5003:电压电流型保护模式投退 5004:智能分布式保护模式投退 5011:远方修改功能只读,不可更改
  3176. 19年中山要求5001:保护总投退 5002:电流型保护模式投退 5003:电压电流型保护模式投退 5004:智能分布式保护模式投退 5005:远方修改功能只读,不可更改*/
  3177. if(parId==0x5001 || parId==0x5002 || parId==0x5003 || parId==0x5004)
  3178. return false;
  3179. #ifdef GD_AREA_ZHONGSHAN
  3180. if(parId==0x5011)
  3181. #elif defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_ZHONGSHAN_2020_ZC
  3182. if(parId==0x5005)
  3183. #endif
  3184. return false;
  3185. #elif defined GD_AREA_MEIZHOU_FTU || defined GD_AREA_MEIZHOU_2021
  3186. if(parId==0x5001 || parId==0x5024 || parId==0x505f || parId==0x5060
  3187. || parId==0x5032 || parId==0x5033 || parId==0x5034 || parId==0x5035 || parId==0x5036
  3188. || parId==0x5037 || parId==0x5038 || parId==0x5039 || parId==0x503a || parId==0x503b
  3189. || parId==0x5501 || parId==0x5502 || parId==0x5503 || parId==0x5504 || parId==0x5505
  3190. || parId==0x5506 || parId==0x5507 || parId==0x5508 || parId==0x5509 || parId==0x550a)
  3191. return false;
  3192. /*#elif defined GD_AREA_ECZD_SHAOGUAN_2021
  3193. if(parId==0x5001 || parId==0x5002 || parId==0x5003 || parId==0x5004 || parId==0x5005 || parId==0x5006)
  3194. {//5001:保护/FA,5002:同期合闸,5003:自动解列,5004:停用,5005:远方投入,5006:定值区号
  3195. return false;
  3196. }*/
  3197. #elif defined GD_AREA_DONGGUAN_2021
  3198. if(parId==0x500C || parId==0x5065)
  3199. {
  3200. return false;
  3201. }
  3202. #elif 0//defined GD_AREA_ECZD_YUNFU_YUNAN_2022
  3203. if(parId==0x5001)//云浮郁南要求5001:定值区号 只读,不可更改
  3204. {
  3205. return false;
  3206. }
  3207. #endif
  3208. value = IECbuf2Foat(pbuf);
  3209. rt_printf("预置参数 parId %02x=%f \r\n",parId,value);
  3210. parTable=getSetTableOffset(&tPubSetTable[0],PUB_SET_NUMBER,parId,SETGROUP_TYPE_PUB, true);
  3211. if(parTable != -1)
  3212. {
  3213. rt_printf("%s PubType=%d %d i=%d\r\n",tPubSetTable[parTable].szName,parId,SETGROUP_TYPE_PUB,parTable);
  3214. if(value<=tPubSetTable[parTable].fMax && value>=tPubSetTable[parTable].fMin)
  3215. return true;
  3216. else
  3217. {
  3218. rt_printf("预置参数超范围,失败 par=%f parTable=%d max=%f min%f\r\n",value,parTable,tPubSetTable[parTable].fMax,tPubSetTable[parTable].fMin);
  3219. return false;
  3220. }
  3221. }
  3222. parTable=getSetTableOffset(&tSwSetTable[0],SW_SET_NUMBER,parId,SETGROUP_TYPE_BH, true);
  3223. if(parTable != -1)
  3224. {
  3225. //parTable -= PUB_SET_NUMBER;
  3226. //if(parTable > 0 && parTable < SW_SET_NUMBER)
  3227. {
  3228. rt_printf("%s BhType=%d %d i=%d\r\n",tSwSetTable[parTable].szName,parId,SETGROUP_TYPE_BH,parTable);
  3229. #ifdef BH_FA_INONE_SW
  3230. if(0 == strcmp(tSwSetTable[parTable].szName,"保护/FA"))
  3231. {//判断“保护/FA”时需对主站送过来的值作减1运算以满足实际列表BHorFA_List里的值
  3232. if(((value-1)<=tSwSetTable[parTable].fMax) && ((value-1)>=tSwSetTable[parTable].fMin))
  3233. return true;
  3234. else
  3235. {
  3236. rt_printf("预置参数超范围,失败 par=%f parTable=%d max=%f min%f\r\n",value,parTable,tSwSetTable[parTable].fMax,tSwSetTable[parTable].fMin);
  3237. return false;
  3238. }
  3239. }
  3240. else
  3241. #endif
  3242. {
  3243. if(value<=tSwSetTable[parTable].fMax && value>=tSwSetTable[parTable].fMin)
  3244. return true;
  3245. else
  3246. {
  3247. rt_printf("预置参数超范围,失败 par=%f parTable=%d max=%f min%f\r\n",value,parTable,tSwSetTable[parTable].fMax,tSwSetTable[parTable].fMin);
  3248. return false;
  3249. }
  3250. }
  3251. }
  3252. }
  3253. if(parTable == -1)
  3254. rt_printf("预置参数地址不存在parID=%x\r\n",parId);
  3255. else
  3256. rt_printf("预置参数非法par=%f\r\n",value);
  3257. return false;
  3258. }//写读取,生效
  3259. bool wirteRunParId(WORD sw, WORD parId, BYTE *pbuf,u8 ti)
  3260. {
  3261. int parTable=0, j=0;
  3262. #ifndef FUNC_MORE_PRESET
  3263. BYTE buf[6];
  3264. int i=0;
  3265. #endif
  3266. SET_VALUE *parBuf;
  3267. rt_printf("写参数 %02x=%f \r\n",parId,IECbuf2Foat(pbuf));
  3268. if(sw>SWITCH_NUM_MAX)
  3269. return false;
  3270. #ifdef FUNC_MORE_PRESET
  3271. if(gb_101_yt)
  3272. {
  3273. for(j=0;j<sizeof(PresetData101[g_CountSet101-1]);j++)
  3274. {
  3275. if(0 == (j+1)%7)
  3276. {//跳过设定命令限定词qos(每7位最后一位)
  3277. continue;
  3278. }
  3279. if(SetData101[g_CountSet101-1].yc_data[j] != PresetData101[g_CountSet101-1].yc_data[j])
  3280. {
  3281. rt_printf("固化定值与预置定值不一样,请检查第%d帧执行报文,parId=%x\r\n",g_CountSet101,parId);
  3282. return false;
  3283. }
  3284. }
  3285. }
  3286. else
  3287. {
  3288. for(j=0;j<sizeof(PresetData104[g_CountSet104-1]);j++)
  3289. {
  3290. if(ti==FOS_PAR_SET_ONE || ti==FOS_PAR_SET_MUL)
  3291. {
  3292. if(0 == (j+1)%8) continue;
  3293. }
  3294. else
  3295. {
  3296. if(0 == (j+1)%7) continue;
  3297. }
  3298. if(SetData104[g_CountSet104-1].yc_data[j] != PresetData104[g_CountSet104-1].yc_data[j])
  3299. {
  3300. rt_printf("固化定值与预置定值不一样,请检查第%d帧执行报文,parId=%x\r\n",g_CountSet104,parId);
  3301. return false;
  3302. }
  3303. }
  3304. }
  3305. #else
  3306. //放在所有参数处理的前面,包括只读定值在内的都需进行比较和统计
  3307. buf[i++] = (BYTE)(parId >> 8);
  3308. buf[i++] = (BYTE)parId;
  3309. memcpy(&buf[i],pbuf,4);
  3310. for(j=0;j<6;j++)
  3311. {
  3312. if(buf[j] == buf_forCompare[j+j_forCompare])
  3313. continue;
  3314. else
  3315. {
  3316. rt_printf("固化定值与预置定值不一样,请检查parId=%x\r\n",parId);
  3317. j_forCompare += 6;
  3318. return false;
  3319. }
  3320. }
  3321. j_forCompare += 6;
  3322. #endif
  3323. #if defined GD_AREA_FOS || defined GD_AREA_FOS_2021 || defined FOS_2021
  3324. if(parId==0x5001 || parId==0x5002)//佛山局要求5001:常规继电保护模式 0x5002 常规继电保护定值区号 只读,不可更改
  3325. return false;
  3326. #ifdef FOS_2021
  3327. if(parId==0x5024) //远方整定投入软压板
  3328. {
  3329. rt_printf("远方整定投入软压板不允许远方修改\r\n");
  3330. return false;
  3331. }
  3332. #endif
  3333. #elif defined GD_AREA_SHANWEI
  3334. if(parId==0x500b || parId==0x5045)//汕尾局要求500b:常规继电保护模式 5045:常规FA自动化模式 只读,不可更改
  3335. return false;
  3336. #elif defined GD_AREA_ECZD_MAOMING
  3337. //茂名局要求5001:定值区号 5013:常规继电保护模式/5051:电压电流型继电保护模式/508d:智能分布式保护模式/50ab:转换把手位置/504c、507e:远方整定投入软压板 只读,不可更改
  3338. if(parId==0x5001 || parId==0x5013 || parId==0x5051 || parId==0x508d || parId==0x50ab || parId==0x504c || parId==0x507e)
  3339. return false;
  3340. #elif defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_ZHONGSHAN_2020_ZC || defined GD_AREA_ZHONGSHAN
  3341. /*19年中山要求5001:保护总投退 5002:电流型保护模式投退 5003:电压电流型保护模式投退 5004:智能分布式保护模式投退 5011:远方修改功能只读,不可更改
  3342. 19年中山要求5001:保护总投退 5002:电流型保护模式投退 5003:电压电流型保护模式投退 5004:智能分布式保护模式投退 5005:远方修改功能只读,不可更改*/
  3343. if(parId==0x5001 || parId==0x5002 || parId==0x5003 || parId==0x5004)
  3344. return false;
  3345. #ifdef GD_AREA_ZHONGSHAN
  3346. if(parId==0x5011)
  3347. #elif defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_ZHONGSHAN_2020_ZC
  3348. if(parId==0x5005)
  3349. #endif
  3350. return false;
  3351. #elif defined GD_AREA_MEIZHOU_FTU || defined GD_AREA_MEIZHOU_2021
  3352. if(parId==0x5001 || parId==0x5024 || parId==0x505f || parId==0x5060
  3353. || parId==0x5032 || parId==0x5033 || parId==0x5034 || parId==0x5035 || parId==0x5036
  3354. || parId==0x5037 || parId==0x5038 || parId==0x5039 || parId==0x503a || parId==0x503b
  3355. || parId==0x5501 || parId==0x5502 || parId==0x5503 || parId==0x5504 || parId==0x5505
  3356. || parId==0x5506 || parId==0x5507 || parId==0x5508 || parId==0x5509 || parId==0x550a)
  3357. return false;
  3358. /*#elif defined GD_AREA_ECZD_SHAOGUAN_2021
  3359. if(parId==0x5001 || parId==0x5002 || parId==0x5003 || parId==0x5004 || parId==0x5005 || parId==0x5006)
  3360. {//5001:保护/FA,5002:同期合闸,5003:自动解列,5004:停用,5005:远方投入,5006:定值区号
  3361. return false;
  3362. }*/
  3363. #elif defined GD_AREA_DONGGUAN_2021
  3364. if(parId==0x500C || parId==0x5065)
  3365. {
  3366. return false;
  3367. }
  3368. #endif
  3369. if(selectRunParId(sw, parId, pbuf) ==false) return false;
  3370. parBuf = rt_malloc(SET_NUMBER*4);
  3371. if(!parBuf)
  3372. {
  3373. rt_printf("\r\n远方写定值,分配定值缓冲区错误!\r\n");
  3374. return false;
  3375. }
  3376. if(!ReadSet(m_runsection,(void *)parBuf)) // 读定值
  3377. {
  3378. GetDefSet((float*)parBuf);
  3379. }
  3380. parTable=getSetTableOffset(&tPubSetTable[0],PUB_SET_NUMBER,parId,SETGROUP_TYPE_PUB, true);
  3381. if(parTable != -1)
  3382. {
  3383. #ifdef GD_ECZD_FOS_FTU_2021
  3384. if(parId == 0x5026)
  3385. {//0x5026与停用压板关联,但取值相反。主站下0时压板为1,主站下1时压板为0
  3386. parBuf[SET_BH_YB].ff = IECbuf2Foat(pbuf);
  3387. if(parBuf[SET_BH_YB].ff)
  3388. {
  3389. parBuf[SET_BH_YB].ff = 0;
  3390. }
  3391. else
  3392. {
  3393. parBuf[SET_BH_YB].ff = (float)1;
  3394. }
  3395. rt_printf("写SET_BH_YB=%f\r\n",parBuf[SET_BH_YB].ff);
  3396. }
  3397. else
  3398. #endif
  3399. {
  3400. //rt_printf("%s ID=%d i=%d\r\n",tPubSetTable[parTable].szName,parId,parTable);
  3401. parBuf[parTable].ff = IECbuf2Foat(pbuf);
  3402. rt_printf("写parBuf=%f\r\n",parBuf[parTable].ff);
  3403. }
  3404. }
  3405. else
  3406. {
  3407. parTable=getSetTableOffset(&tSwSetTable[0],SW_SET_NUMBER,parId,SETGROUP_TYPE_BH, true);
  3408. if(parTable != -1)
  3409. {
  3410. //parTable -= PUB_SET_NUMBER;
  3411. //if(parTable > 0 && parTable < SW_SET_NUMBER)
  3412. {
  3413. //rt_printf("%s ID=%x i=%d\r\n",tSwSetTable[parTable].szName,parId,parTable);
  3414. #ifdef BH_FA_INONE_SW
  3415. if(0 == strcmp(tSwSetTable[parTable].szName,"保护/FA"))
  3416. {//判断“保护/FA”时需对主站送过来的值作减1运算以满足实际列表BHorFA_List里的值
  3417. parBuf[sw*parTable+PUB_SET_NUMBER].ff = (IECbuf2Foat(pbuf)-1);
  3418. rt_printf("wirteRunParId=%02x i=%d %f\r\n",parId,parTable,parBuf[sw*parTable+PUB_SET_NUMBER].ff);
  3419. }
  3420. else
  3421. #endif
  3422. {
  3423. parBuf[sw*parTable+PUB_SET_NUMBER].ff = IECbuf2Foat(pbuf);
  3424. rt_printf("wirteRunParId=%02x i=%d %f\r\n",parId,parTable,parBuf[sw*parTable+PUB_SET_NUMBER].ff);
  3425. }
  3426. }
  3427. }
  3428. }
  3429. if((parTable>=0)&&pRunSet->bTT_EDIT_YB&&SaveSet(m_runsection,(void *)parBuf)) //必须远方投入软压板为1
  3430. {
  3431. soe_record_opt(EV_SET_OK,0);
  3432. MakeRunSet(false);
  3433. rt_err_clr(ERR_CODE_SET,0);
  3434. rt_free(parBuf);
  3435. return true;
  3436. }
  3437. else
  3438. {
  3439. soe_record_opt(EV_SET_FAIL,0);
  3440. rt_printf("写参数失败\r\n");
  3441. rt_free(parBuf);
  3442. return false;
  3443. }
  3444. }
  3445. //读参数
  3446. bool readRunParId(WORD sw, WORD parId, BYTE *pbuf)
  3447. {
  3448. int parTable=0;
  3449. SET_VALUE *parBuf;
  3450. #if defined GD_AREA_ECZD_MAOMING || defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_ZHONGSHAN_2020_ZC || defined GD_AREA_MEIZHOU_FTU || defined GD_AREA_MEIZHOU_2021
  3451. SET_VALUE *pparaBuf;
  3452. #endif
  3453. #ifdef FUNC_YT_HARD_YB
  3454. struct equ_config_di *ecd;
  3455. u8 temp_yx;
  3456. #endif
  3457. if(sw>SWITCH_NUM_MAX)
  3458. return false;
  3459. parBuf = rt_malloc(SET_NUMBER*4);
  3460. if(!parBuf)
  3461. {
  3462. rt_free(parBuf);
  3463. rt_printf("\r\n远方读定值,分配定值缓冲区错误!\r\n");
  3464. return false;
  3465. }
  3466. #if defined GD_AREA_ECZD_MAOMING || defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_ZHONGSHAN_2020_ZC || defined GD_AREA_MEIZHOU_FTU || defined GD_AREA_MEIZHOU_2021
  3467. pparaBuf = rt_malloc(EQUIP_PARA_NUMBER*4);
  3468. if(!pparaBuf)
  3469. {
  3470. rt_free(pparaBuf);
  3471. rt_printf("\r\n远方读定值,分配装置参数缓冲区错误!\r\n");
  3472. return false;
  3473. }
  3474. #endif
  3475. #if defined GD_AREA_FOS || defined GD_AREA_FOS_2021 || defined FOS_2021
  3476. #ifdef GD_ECZD_FOS_FTU_2021
  3477. if(parId == 0x5026)
  3478. {
  3479. float2IECbuf(pbuf,(float)pRunSet->bTT_BH_YB);
  3480. rt_free(parBuf);
  3481. return true;
  3482. }
  3483. #else
  3484. if(parId==0x5001)//佛山局要求5001:常规继电保护模式 为保护总硬件遥信,不需要配置
  3485. {
  3486. float2IECbuf(pbuf,(float)BH_ALL_EN(0));
  3487. rt_free(parBuf);
  3488. return true;
  3489. }
  3490. #endif
  3491. #elif defined GD_AREA_SHANWEI
  3492. if(parId==0x500b)
  3493. {
  3494. float2IECbuf(pbuf,(float)BH_ALL_EN(0));
  3495. rt_free(parBuf);
  3496. return true;
  3497. }
  3498. else if(parId==0x5045)
  3499. {
  3500. float2IECbuf(pbuf,(float)FA_ALL_EN(0));
  3501. rt_free(parBuf);
  3502. return true;
  3503. }
  3504. //茂名局要求5001:定值区号 5013:常规继电保护模式/5051:电压电流型继电保护模式/508d:智能分布式保护模式/50ab:转换把手位置 只读,不可更改
  3505. #elif defined GD_AREA_ECZD_MAOMING
  3506. if(parId==0x5013)
  3507. {
  3508. float2IECbuf(pbuf,(float)BH_ALL_EN(0));
  3509. rt_free(parBuf);
  3510. return true;
  3511. }
  3512. else if(parId==0x5051)
  3513. {
  3514. float2IECbuf(pbuf,(float)FA_ALL_EN(0));
  3515. rt_free(parBuf);
  3516. return true;
  3517. }
  3518. else if(parId==0x508d)
  3519. {
  3520. float2IECbuf(pbuf,(float)BH_GOOSE_EN(0));
  3521. rt_free(parBuf);
  3522. return true;
  3523. }
  3524. else if(parId==0x50ab)
  3525. {//转换把手位置 1:投入继电保护功能;2:投入FA功能;3:投入智能分布式FA功能;4:禁用功能
  3526. if(BH_ALL_EN(0))
  3527. {
  3528. float2IECbuf(pbuf,(float)1);
  3529. rt_free(parBuf);
  3530. return true;
  3531. }
  3532. else if(FA_ALL_EN(0))
  3533. {
  3534. float2IECbuf(pbuf,(float)2);
  3535. rt_free(parBuf);
  3536. return true;
  3537. }
  3538. else if(BH_GOOSE_EN(0))
  3539. {
  3540. float2IECbuf(pbuf,(float)3);
  3541. rt_free(parBuf);
  3542. return true;
  3543. }
  3544. else
  3545. {
  3546. float2IECbuf(pbuf,(float)4);
  3547. rt_free(parBuf);
  3548. return true;
  3549. }
  3550. }
  3551. #elif defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_ZHONGSHAN_2020_ZC || defined GD_AREA_ECZD_CHAOZHOU_2022
  3552. if(parId==0x5001)
  3553. {
  3554. int i;
  3555. for(i=0;i<g_sw_num;i++)
  3556. {
  3557. if(((short)g_sw[i].di_cfg_index[SW_DI_BHTT] != INDEX_INVALLID) && (g_tRelay[i].run_stu.bh_yx==1))
  3558. {
  3559. rt_printf("SW==%d,常规保护压板合\r\n",i);
  3560. float2IECbuf(pbuf,(float)1);
  3561. rt_free(parBuf);
  3562. return true;
  3563. }
  3564. else if(((short)g_sw[i].di_cfg_index[SW_DI_FA_TT] != INDEX_INVALLID) && (g_tRelay[i].run_stu.fa_yx)==1)
  3565. {
  3566. rt_printf("SW==%d,常规FA压板合\r\n",i);
  3567. float2IECbuf(pbuf,(float)2);
  3568. rt_free(parBuf);
  3569. return true;
  3570. }
  3571. else if(((short)g_sw[i].di_cfg_index[SW_DI_FA_GOOSE] != INDEX_INVALLID) && (g_tRelay[i].run_stu.fa_g_yx)==1)
  3572. {
  3573. rt_printf("SW==%d,智能FA压板合\r\n",i);
  3574. float2IECbuf(pbuf,(float)3);
  3575. rt_free(parBuf);
  3576. return true;
  3577. }
  3578. //为调试、测试便于查找问题,预留打印信息
  3579. else if(((short)g_sw[i].di_cfg_index[SW_DI_BHTT] == INDEX_INVALLID)
  3580. || ((short)g_sw[i].di_cfg_index[SW_DI_FA_TT] == INDEX_INVALLID)
  3581. || ((short)g_sw[i].di_cfg_index[SW_DI_FA_GOOSE] == INDEX_INVALLID))
  3582. {
  3583. rt_printf("常规保护压板/常规FA压板/智能FA压板DI通道未配置!!!\r\n");
  3584. }
  3585. }
  3586. }
  3587. else if(parId==0x5002)
  3588. {
  3589. int i;
  3590. for(i=0;i<g_sw_num;i++)
  3591. {
  3592. if((short)g_sw[i].di_cfg_index[SW_DI_TQ] != INDEX_INVALLID)
  3593. {
  3594. float2IECbuf(pbuf,(float)g_tRelay[i].run_stu.bTQ);
  3595. rt_free(parBuf);
  3596. return true;
  3597. }
  3598. //为调试、测试便于查找问题,预留打印信息
  3599. else
  3600. {
  3601. rt_printf("停用同期压板DI通道未配置!!!\r\n");
  3602. }
  3603. }
  3604. }
  3605. else if(parId==0x5003)
  3606. {
  3607. int i;
  3608. for(i=0;i<g_sw_num;i++)
  3609. {
  3610. if((short)g_sw[i].di_cfg_index[SW_DI_FREQ_VOL] != INDEX_INVALLID)
  3611. {
  3612. float2IECbuf(pbuf,(float)g_tRelay[i].run_stu.freq_vol);
  3613. rt_free(parBuf);
  3614. return true;
  3615. }
  3616. //为调试、测试便于查找问题,预留打印信息
  3617. else
  3618. {
  3619. rt_printf("停用自动解列功能压板DI通道未配置!!!\r\n");
  3620. }
  3621. }
  3622. }
  3623. else if(parId==0x5004)
  3624. {
  3625. int i;
  3626. for(i=0;i<g_sw_num;i++)
  3627. {
  3628. if((short)g_sw[i].di_cfg_index[SW_DI_BHZTT] != INDEX_INVALLID)
  3629. {
  3630. float2IECbuf(pbuf,(float)!g_tRelay[i].run_stu.bhztt);
  3631. rt_free(parBuf);
  3632. return true;
  3633. }
  3634. //为调试、测试便于查找问题,预留打印信息
  3635. else
  3636. {
  3637. rt_printf("停用FA及保护DI通道未配置!!!\r\n");
  3638. }
  3639. }
  3640. }
  3641. #elif defined GD_AREA_ZHONGSHAN
  3642. if(parId==0x5001)
  3643. {
  3644. float2IECbuf(pbuf,(float)FUN_ALL_EN(0));
  3645. rt_free(parBuf);
  3646. return true;
  3647. }
  3648. else if(parId==0x5002)
  3649. {
  3650. int i;
  3651. for(i=0;i<g_sw_num;i++)
  3652. {
  3653. if((short)g_sw[i].di_cfg_index[SW_DI_BHTT] != INDEX_INVALLID)
  3654. {
  3655. float2IECbuf(pbuf,(float)g_tRelay[i].run_stu.bh_yx);
  3656. rt_free(parBuf);
  3657. return true;
  3658. }
  3659. }
  3660. }
  3661. else if(parId==0x5003)
  3662. {
  3663. int i;
  3664. for(i=0;i<g_sw_num;i++)
  3665. {
  3666. if((short)g_sw[i].di_cfg_index[SW_DI_FA_TT] != INDEX_INVALLID)
  3667. {
  3668. float2IECbuf(pbuf,(float)g_tRelay[i].run_stu.fa_yx);
  3669. rt_free(parBuf);
  3670. return true;
  3671. }
  3672. }
  3673. }
  3674. else if(parId==0x5004)
  3675. {
  3676. int i;
  3677. for(i=0;i<g_sw_num;i++)
  3678. {
  3679. if((short)g_sw[i].di_cfg_index[SW_DI_FA_GOOSE] != INDEX_INVALLID)
  3680. {
  3681. float2IECbuf(pbuf,(float)g_tRelay[i].run_stu.fa_g_yx);
  3682. rt_free(parBuf);
  3683. return true;
  3684. }
  3685. }
  3686. }
  3687. #elif defined GD_AREA_MEIZHOU_FTU || defined GD_AREA_MEIZHOU_2021
  3688. if(parId==0x5001)
  3689. {
  3690. float2IECbuf(pbuf,(float)BH_ALL_EN(0));
  3691. rt_free(parBuf);
  3692. return true;
  3693. }
  3694. else if(parId==0x5032)
  3695. {
  3696. float2IECbuf(pbuf,(float)FA_ALL_EN(0));
  3697. rt_free(parBuf);
  3698. return true;
  3699. }
  3700. #endif
  3701. #ifdef FUNC_YT_HARD_YB
  3702. parTable=getSetTableOffset(0,0,parId,SETGROUP_TYPE_BY, false);
  3703. if(parTable != -1)
  3704. {
  3705. int i;
  3706. for(i=0;i<g_sw_num;i++)
  3707. {
  3708. if((parTable>=0) && (parTable<YT_SW_HARD_YB_NUM))
  3709. {
  3710. if((short)g_sw[i].di_cfg_index[sw_yt_hard_yb_index[parTable]] != INDEX_INVALLID)
  3711. {
  3712. ecd = &g_equ_config_di[g_sw[i].di_cfg_index[sw_yt_hard_yb_index[parTable]]];
  3713. temp_yx = dido_di_is_on(ecd->slot,ecd->index);
  3714. rt_printf("读硬压板:%s=%d\r\n",g_sw_di_name[sw_yt_hard_yb_index[parTable]],temp_yx);
  3715. float2IECbuf(pbuf,(float)temp_yx);
  3716. rt_free(parBuf);
  3717. return true;
  3718. }
  3719. else
  3720. {
  3721. rt_free(parBuf);
  3722. rt_printf("硬压板[%s]未配置,读取失败\r\n",g_sw_di_name[sw_yt_hard_yb_index[parTable]]);
  3723. return false;
  3724. }
  3725. }
  3726. }
  3727. if((parTable>=YT_SW_HARD_YB_NUM) && (parTable<(YT_SW_HARD_YB_NUM+YT_PUB_HARD_YB_NUM)))
  3728. {
  3729. if((short)g_sw_pub.di_cfg_index[pub_yt_hard_yb_index[parTable-YT_SW_HARD_YB_NUM]] != INDEX_INVALLID)
  3730. {
  3731. ecd = &g_equ_config_di[g_sw_pub.di_cfg_index[pub_yt_hard_yb_index[parTable-YT_SW_HARD_YB_NUM]]];
  3732. temp_yx = dido_di_is_on(ecd->slot,ecd->index);
  3733. rt_printf("读硬压板:%s=%d\r\n",g_pub_di_name[pub_yt_hard_yb_index[parTable-YT_SW_HARD_YB_NUM]],temp_yx);
  3734. float2IECbuf(pbuf,(float)temp_yx);
  3735. rt_free(parBuf);
  3736. return true;
  3737. }
  3738. else
  3739. {
  3740. rt_free(parBuf);
  3741. rt_printf("硬压板[%s]未配置,读取失败\r\n",g_pub_di_name[pub_yt_hard_yb_index[parTable-YT_SW_HARD_YB_NUM]]);
  3742. return false;
  3743. }
  3744. }
  3745. #ifdef FUNC_YT_MIX
  3746. else if(parTable>=YT_SW_HARD_YB_NUM+YT_PUB_HARD_YB_NUM)
  3747. {
  3748. int i=0;
  3749. #ifdef GD_AREA_HUIZHOU_2021
  3750. //模式硬遥信上送:1:常规保护;2:常规FA;3:智能FA
  3751. if(MIX_MODE_SEL == (parTable-YT_SW_HARD_YB_NUM-YT_PUB_HARD_YB_NUM))
  3752. {
  3753. int ecd_yx_bh=0,ecd_yx_fa=0,ecd_yx_goose=0;
  3754. for(i=0;i<g_sw_num;i++)
  3755. {
  3756. if(((short)g_sw[i].di_cfg_index[SW_DI_BHTT] != INDEX_INVALLID)
  3757. || ((short)g_sw[i].di_cfg_index[SW_DI_FA_TT] != INDEX_INVALLID)
  3758. || ((short)g_sw[i].di_cfg_index[SW_DI_FA_GOOSE] != INDEX_INVALLID))
  3759. {//三大模式压板有配置
  3760. if((short)g_sw[i].di_cfg_index[SW_DI_BHTT] != INDEX_INVALLID)
  3761. {
  3762. ecd = &g_equ_config_di[g_sw[i].di_cfg_index[SW_DI_BHTT]];
  3763. ecd_yx_bh = dido_di_is_on(ecd->slot,ecd->index);
  3764. }
  3765. if((short)g_sw[i].di_cfg_index[SW_DI_FA_TT] != INDEX_INVALLID)
  3766. {
  3767. ecd = &g_equ_config_di[g_sw[i].di_cfg_index[SW_DI_FA_TT]];
  3768. ecd_yx_fa = dido_di_is_on(ecd->slot,ecd->index);
  3769. }
  3770. if((short)g_sw[i].di_cfg_index[SW_DI_FA_GOOSE] != INDEX_INVALLID)
  3771. {
  3772. ecd = &g_equ_config_di[g_sw[i].di_cfg_index[SW_DI_FA_GOOSE]];
  3773. ecd_yx_goose = dido_di_is_on(ecd->slot,ecd->index);
  3774. }
  3775. if(ecd_yx_bh)
  3776. temp_yx = 1; //常规保护压板上送1
  3777. else if(ecd_yx_fa)
  3778. temp_yx = 2; //常规FA压板上送2
  3779. else if(ecd_yx_goose)
  3780. temp_yx = 3; //智能FA压板上送3
  3781. else
  3782. temp_yx = 0; //三大模式压板遥信点都为0时上送0
  3783. rt_printf("读硬压板:%s=%d\r\n",mix_yt_index[MIX_MODE_SEL],temp_yx);
  3784. float2IECbuf(pbuf,(float)temp_yx);
  3785. rt_free(parBuf);
  3786. return true;
  3787. }
  3788. else
  3789. {//三大模式压板未配置,上送4
  3790. float2IECbuf(pbuf,(float)4);
  3791. rt_free(parBuf);
  3792. rt_printf("三大模式压板未配置,读取失败\r\n");
  3793. return false;
  3794. }
  3795. }
  3796. }
  3797. #endif
  3798. #ifdef GD_AREA_ECZD_JIEYANG_2022
  3799. for(i=0;i<g_sw_num;i++)
  3800. {
  3801. if(MIX_CHZ_CD == (parTable-YT_SW_HARD_YB_NUM-YT_PUB_HARD_YB_NUM))
  3802. {
  3803. temp_yx = (u8)soe_check(EV_ARCD+i*EV_SW_NUM);
  3804. rt_printf("读状态:%s=%d\r\n",mix_yt_index[MIX_CHZ_CD],temp_yx);
  3805. float2IECbuf(pbuf,(float)temp_yx);
  3806. rt_free(parBuf);
  3807. return true;
  3808. }
  3809. else if(MIX_LL_CD == (parTable-YT_SW_HARD_YB_NUM-YT_PUB_HARD_YB_NUM))
  3810. {
  3811. RUN_STU_SW *prun=&g_tRelay[i].run_stu;
  3812. temp_yx = prun->fa_ll_cd;
  3813. rt_printf("读状态:%s=%d\r\n",mix_yt_index[MIX_LL_CD],temp_yx);
  3814. float2IECbuf(pbuf,(float)temp_yx);
  3815. rt_free(parBuf);
  3816. return true;
  3817. }
  3818. else if(MIX_BS_HZ == (parTable-YT_SW_HARD_YB_NUM-YT_PUB_HARD_YB_NUM))
  3819. {
  3820. temp_yx = (u8)soe_check(EV_BS_ALL+i*EV_SW_NUM);
  3821. rt_printf("读状态:%s=%d\r\n",mix_yt_index[MIX_BS_HZ],temp_yx);
  3822. float2IECbuf(pbuf,(float)temp_yx);
  3823. rt_free(parBuf);
  3824. return true;
  3825. }
  3826. else if(MIX_TY == (parTable-YT_SW_HARD_YB_NUM-YT_PUB_HARD_YB_NUM))
  3827. {
  3828. if ((short)g_sw[i].di_cfg_index[SW_DI_BHZTT] != INDEX_INVALLID)
  3829. {
  3830. ecd = &g_equ_config_di[g_sw[i].di_cfg_index[SW_DI_BHZTT]];
  3831. temp_yx = dido_di_is_on(ecd->slot,ecd->index);
  3832. }
  3833. else
  3834. {
  3835. temp_yx = !pRunSet->bTT_BH_YB;
  3836. }
  3837. rt_printf("读状态:%s=%d\r\n",mix_yt_index[MIX_TY],temp_yx);
  3838. float2IECbuf(pbuf,(float)temp_yx);
  3839. rt_free(parBuf);
  3840. return true;
  3841. }
  3842. }
  3843. #endif
  3844. }
  3845. #endif
  3846. }
  3847. #endif
  3848. if(!ReadSet(m_runsection,(void *)parBuf)) // 读定值
  3849. {
  3850. GetDefSet((float*)parBuf);
  3851. }
  3852. #if defined GD_AREA_ECZD_MAOMING || defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_ZHONGSHAN_2020_ZC || defined GD_AREA_MEIZHOU_FTU || defined GD_AREA_MEIZHOU_2021
  3853. if(!ReadPara((void*)pparaBuf,EEP_EQUIP_PARA_ADDR,EQUIP_PARA_NUMBER,&tEquipParaTable[0]))// 读参数
  3854. {
  3855. GetDefPara((void*)pparaBuf,EQUIP_PARA_NUMBER,&tEquipParaTable[0]);
  3856. }
  3857. #endif
  3858. parTable=getSetTableOffset(&tPubSetTable[0],PUB_SET_NUMBER,parId,SETGROUP_TYPE_PUB, false);
  3859. if(parTable != -1)
  3860. {
  3861. rt_printf("%s Type=%d %d i=%d\r\n",tPubSetTable[parTable].szName,parId,SETGROUP_TYPE_PUB,parTable);
  3862. rt_printf("readRunParId=%02x parTable=%d ",parId,parTable);
  3863. rt_printf("parBuf=%f\r\n",parBuf[parTable].ff);
  3864. float2IECbuf(pbuf,parBuf[parTable].ff);
  3865. rt_free(parBuf);
  3866. return true;
  3867. }
  3868. parTable=getSetTableOffset(&tSwSetTable[0],SW_SET_NUMBER,parId,SETGROUP_TYPE_BH, false);
  3869. if(parTable != -1)
  3870. {
  3871. //parTable -= PUB_SET_NUMBER;
  3872. //if(parTable > 0 && parTable < SW_SET_NUMBER)
  3873. {
  3874. rt_printf("%s Type=%d %d i=%d\r\n",tSwSetTable[parTable].szName,parId,SETGROUP_TYPE_BH,parTable);
  3875. rt_printf("ReadRunParId=%02x swOffset=%d ,offset=%d par=%f\r\n",parId,parTable,parTable*sw+PUB_SET_NUMBER+PUB_SET_NUMBER,parBuf[parTable*sw+PUB_SET_NUMBER].ff);
  3876. #ifdef BH_FA_INONE_SW
  3877. if(0 == strcmp(tSwSetTable[parTable].szName,"保护/FA"))
  3878. {//5001 保护/FA定值上送时需主动加1,因为列表值是0/1/2,需送值为1/2/3
  3879. float2IECbuf(pbuf,(parBuf[sw*parTable+PUB_SET_NUMBER].ff) + (float)1);
  3880. }
  3881. else
  3882. #endif
  3883. {
  3884. float2IECbuf(pbuf,parBuf[sw*parTable+PUB_SET_NUMBER].ff);
  3885. }
  3886. }
  3887. rt_free(parBuf);
  3888. return true;
  3889. }
  3890. #if defined GD_AREA_ECZD_MAOMING || defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_ZHONGSHAN_2020_ZC || defined GD_AREA_MEIZHOU_FTU || defined GD_AREA_MEIZHOU_2021
  3891. parTable=getSetTableOffset(&tEquipParaTable[0],EQUIP_PARA_NUMBER,parId,SETGROUP_TYPE_PARA, false);
  3892. if(parTable != -1)
  3893. {
  3894. //parTable -= PUB_SET_NUMBER+SW_SET_NUMBER;
  3895. //if(parTable > 0 && parTable < EQUIP_PARA_NUMBER)
  3896. {
  3897. rt_printf("%s Type=%d %d i=%d\r\n",tEquipParaTable[parTable].szName,parId,SETGROUP_TYPE_PARA,parTable);
  3898. rt_printf("ReadRunParId=%02x offset=%d par=%f\r\n",parId,parTable,pparaBuf[parTable].ff);
  3899. float2IECbuf(pbuf,pparaBuf[parTable].ff);
  3900. }
  3901. rt_free(pparaBuf);
  3902. return true;
  3903. }
  3904. rt_free(pparaBuf);
  3905. #endif
  3906. #ifdef FUNC_YT_NO_ID
  3907. #ifdef YC_QUANTITY
  3908. if((parId>=0x5001) && (parId<(0x5001+pRunSet->dYC_num)))
  3909. #else
  3910. if((parId>=0x5001) && (parId<(0x5001+FOS_PAR_SW_NUM)))
  3911. #endif
  3912. {
  3913. rt_printf("遥调未配置的地址时以备用点(默认0值)上送\r\n");
  3914. float2IECbuf(pbuf,(float)0);
  3915. rt_free(parBuf);
  3916. return true;
  3917. }
  3918. #endif
  3919. rt_free(parBuf);
  3920. #ifdef FUNC_YT_NO_ID
  3921. rt_printf("0x%x>(0x5001+每间隔遥参数量)!!!\r\n",parId);
  3922. #endif
  3923. rt_printf("读参数失败\r\n");
  3924. return false;
  3925. }
  3926. RMT_SET_VAL RmtSetValDef={0,0,UINT_R,4,{0,},};
  3927. int iec_setread(void *pt,BYTE *pdat,BYTE vsq,bool b104) // dat从定值区号开始
  3928. {
  3929. int len,i;
  3930. u16 section; // 定值区号
  3931. BYTE setnum=0;
  3932. BYTE framelen = 0;
  3933. BYTE datbuf[256] = {0};
  3934. BYTE over=0;
  3935. RMT_FIXED_TABLE tfixedset;
  3936. SET_PARAID *tmp_param;
  3937. RMT_SET_VAL tmp_cst_val;
  3938. SET_VALUE *psetbuf,*pparabuf,*pcstbuf,*ppubbuf;
  3939. section=pdat[0]+(pdat[1]<<8); // 定值区号
  3940. if(section>=SEC_NUMBER) // 定值区不为零,否定应答返回
  3941. {
  3942. rt_printf("\r\n远方读定值,定值区超出范围,否定应答\r\n");
  3943. return -1;
  3944. }
  3945. ppubbuf = rt_malloc(PUB_SET_NUMBER*4);
  3946. if(!ppubbuf)
  3947. {
  3948. rt_printf("\r\n远方读定值,分配公共定值缓冲区错误!\r\n");
  3949. return -1;
  3950. }
  3951. psetbuf = rt_malloc(SW_SET_NUMBER*4);
  3952. if(!psetbuf)
  3953. {
  3954. rt_free(ppubbuf);
  3955. rt_printf("\r\n远方读定值,分配开关定值缓冲区错误!\r\n");
  3956. return -2;
  3957. }
  3958. pparabuf = rt_malloc(EQUIP_PARA_NUMBER*4);
  3959. if(!pparabuf)
  3960. {
  3961. rt_free(ppubbuf);
  3962. rt_free(psetbuf);
  3963. rt_printf("\r\n远方读定值,分配参数缓冲区错误!\r\n");
  3964. return -3;
  3965. }
  3966. pcstbuf = rt_malloc(CSTSET_NUMBER*4);
  3967. if(!pcstbuf)
  3968. {
  3969. rt_free(ppubbuf);
  3970. rt_free(psetbuf);
  3971. rt_free(pparabuf);
  3972. rt_printf("\r\n远方读定值,分配内部定值缓冲区错误!\r\n");
  3973. return -2;
  3974. }
  3975. if(!ReadPara((void*)ppubbuf,EEP_PUB_ADDR,PUB_SET_NUMBER,&tPubSetTable[0]))
  3976. {
  3977. GetDefPara((void*)ppubbuf,PUB_SET_NUMBER,&tPubSetTable[0]);
  3978. }
  3979. if(!ReadPara((void*)psetbuf,EEP_SET_ADDR+m_runsection*SETSIZE,SW_SET_NUMBER,&tSwSetTable[0]))
  3980. {
  3981. GetDefPara((void*)psetbuf,SW_SET_NUMBER,&tSwSetTable[0]);
  3982. }
  3983. if(!ReadPara((void*)pparabuf,EEP_EQUIP_PARA_ADDR,EQUIP_PARA_NUMBER,&tEquipParaTable[0]))
  3984. {
  3985. GetDefPara((void*)pparabuf,EQUIP_PARA_NUMBER,&tEquipParaTable[0]);
  3986. }
  3987. if(!ReadPara((void*)pcstbuf,EEP_CSTSET_ADDR,CSTSET_NUMBER,&tCstSetTable[0]))
  3988. {
  3989. GetDefPara((void*)pcstbuf,CSTSET_NUMBER,&tCstSetTable[0]);
  3990. }
  3991. iec_init_fixedset(pt,&tfixedset,b104); // 获取固定定值
  3992. datbuf[0]=(BYTE)section; // 区号
  3993. datbuf[1]=(BYTE)(section>>8);
  3994. datbuf[2]=0x01; // 参数特征标识,有后续
  3995. framelen=3;
  3996. setnum=0;
  3997. if(vsq==0) // 读全部定值
  3998. {
  3999. for(i=0;i<tfixedset.num;i++) // 固定定值
  4000. {
  4001. tmp_cst_val=tfixedset.set[i];
  4002. len=iec_add2frame(datbuf+framelen,&tmp_cst_val,b104);
  4003. framelen+=len;
  4004. setnum++;
  4005. if(framelen>200)
  4006. {
  4007. if(b104)
  4008. {
  4009. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,setnum,IEC_COT_ACTCON,202); //
  4010. }
  4011. else
  4012. {
  4013. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,setnum,IEC_COT_ACTCON,202,FRAME_I);//
  4014. }
  4015. framelen=3;
  4016. setnum=0;
  4017. }
  4018. }
  4019. for(i=0;i<ParaIDNum;i++)
  4020. {
  4021. if(!tParaID[i].parId)
  4022. continue;
  4023. tmp_param= &tParaID[i];
  4024. switch(tmp_param->group_type)
  4025. {
  4026. case SETGROUP_TYPE_PARA:
  4027. // rt_printf("description:%s addr:%x value:%f\r\n",tEquipParaTable[tmp_param->setno].szName,tmp_param->parId,pparabuf[tmp_param->setno].ff);
  4028. iec_set2rmtset(&tmp_cst_val,tEquipParaTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,pparabuf[tmp_param->setno].ff);
  4029. break;
  4030. case SETGROUP_TYPE_PUB:
  4031. // rt_printf("description:%s addr:%x value:%f\r\n",tPubSetTable[tmp_param->setno].szName,tmp_param->parId,ppubbuf[tmp_param->setno].ff);
  4032. iec_set2rmtset(&tmp_cst_val,tPubSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,ppubbuf[tmp_param->setno].ff);
  4033. break;
  4034. case SETGROUP_TYPE_CSTSET:
  4035. // rt_printf("description:%s addr:%x value:%f\r\n",tCstSetTable[tmp_param->setno].szName,tmp_param->parId,pcstbuf[tmp_param->setno].ff);
  4036. iec_set2rmtset(&tmp_cst_val,tCstSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,pcstbuf[tmp_param->setno].ff);
  4037. break;
  4038. case SETGROUP_TYPE_BH:
  4039. // rt_printf("description:%s addr:%x value:%f\r\n",tSwSetTable[tmp_param->setno].szName,tmp_param->parId,psetbuf[tmp_param->setno].ff);
  4040. iec_set2rmtset(&tmp_cst_val,tSwSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,psetbuf[tmp_param->setno].ff);
  4041. break;
  4042. default:
  4043. continue;
  4044. }
  4045. tmp_cst_val.di = tmp_param->parId;
  4046. len=iec_add2frame(datbuf+framelen,&tmp_cst_val,b104);
  4047. framelen+=len;
  4048. setnum++;
  4049. if(i == ParaIDNum - 1)
  4050. {
  4051. over = 1;
  4052. }
  4053. if((framelen > 200) || over)
  4054. {
  4055. if(over) //最后的一帧,tag清零
  4056. {
  4057. datbuf[2]=0;
  4058. }
  4059. if(b104)
  4060. {
  4061. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,setnum,IEC_COT_ACTCON,202); //
  4062. }
  4063. else
  4064. {
  4065. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,setnum,IEC_COT_ACTCON,202,FRAME_I);//
  4066. }
  4067. framelen=3;
  4068. setnum=0;
  4069. }
  4070. }
  4071. }
  4072. else // 读定义点号的定值
  4073. {
  4074. BYTE *ps=&pdat[2]; //从点号开始
  4075. for(i=0;i<vsq;i++)
  4076. {
  4077. int j;
  4078. u16 di;
  4079. bool bfind=false;
  4080. if(tfixedset.set[i].di==0)
  4081. continue;
  4082. di=ps[0]+(ps[1]<<8);
  4083. for(j=0;j<tfixedset.num;j++) // 固定定值
  4084. {
  4085. if(tfixedset.set[j].di==di) // 点号对应
  4086. {
  4087. tmp_cst_val=tfixedset.set[j];
  4088. bfind=true;
  4089. break;
  4090. }
  4091. }
  4092. if(!bfind)
  4093. {
  4094. for(j=0;j<ParaIDNum;j++) // 修改定值
  4095. {
  4096. if(di != tParaID[j].parId)
  4097. continue;
  4098. tmp_param= &tParaID[j];
  4099. switch(tmp_param->group_type)
  4100. {
  4101. case SETGROUP_TYPE_PARA:
  4102. iec_set2rmtset(&tmp_cst_val,tEquipParaTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,pparabuf[tmp_param->setno].ff);
  4103. break;
  4104. case SETGROUP_TYPE_PUB:
  4105. iec_set2rmtset(&tmp_cst_val,tPubSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,ppubbuf[tmp_param->setno].ff);
  4106. break;
  4107. case SETGROUP_TYPE_CSTSET:
  4108. iec_set2rmtset(&tmp_cst_val,tCstSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,pcstbuf[tmp_param->setno].ff);
  4109. break;
  4110. case SETGROUP_TYPE_BH:
  4111. iec_set2rmtset(&tmp_cst_val,tSwSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,psetbuf[tmp_param->setno].ff);
  4112. break;
  4113. default:
  4114. continue;
  4115. }
  4116. bfind=true;
  4117. break;
  4118. }
  4119. }
  4120. ps+=2;
  4121. if(b104)ps++;
  4122. // 组报文
  4123. if(bfind)
  4124. {
  4125. tmp_cst_val.di=di;
  4126. len=iec_add2frame(datbuf+framelen,&tmp_cst_val,b104);
  4127. framelen+=len;
  4128. setnum++;
  4129. }
  4130. if(framelen>200||i==vsq-1)
  4131. {
  4132. if(i==vsq-1) //最后的一帧,tag清零
  4133. {
  4134. datbuf[2]=0;
  4135. }
  4136. if(b104)
  4137. {
  4138. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,setnum,IEC_COT_ACTCON,202); //
  4139. }
  4140. else
  4141. {
  4142. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,setnum,IEC_COT_ACTCON,202,FRAME_I);//
  4143. }
  4144. framelen=3;
  4145. setnum=0;
  4146. }
  4147. }
  4148. }
  4149. rt_free(psetbuf);
  4150. rt_free(pparabuf);
  4151. rt_free(pcstbuf);
  4152. rt_free(ppubbuf);
  4153. return 0;
  4154. }
  4155. int iec_get_equi_ip(char *ip,BYTE *p_out)
  4156. {
  4157. int i,j=0,k=0;
  4158. char str_ip[4]={'\0'};
  4159. for(i =0;i<=strlen(ip);i++)
  4160. {
  4161. if (ip[i]=='\0'||ip[i]=='.')
  4162. {
  4163. p_out[k] =atoi(str_ip);
  4164. if (p_out[k]<0||p_out[k]>255)
  4165. {
  4166. return -1;
  4167. }
  4168. k++;
  4169. j=0;
  4170. memset(str_ip,0,sizeof(str_ip));
  4171. continue;
  4172. }
  4173. str_ip[j]=ip[i];
  4174. j++;
  4175. }
  4176. return 0;
  4177. }
  4178. #ifdef YPARA_LINK
  4179. /*0不级联,返回级联号*/
  4180. int get_ypara_linkch_write(void *pt,BYTE *pdat,BYTE vsq,bool b104)
  4181. {
  4182. BYTE *ps; //从点号开始
  4183. u16 di = 0;
  4184. int index=0;
  4185. BYTE pdi; //参数特征标识符
  4186. u8 cmd=0,dport=0;
  4187. if(b104)
  4188. {
  4189. IEC104_DEF *pt104 = pt;
  4190. cmd = pt104->msg_ypara_send.ypara_msg.cmd;
  4191. dport = pt104->msg_ypara_send.dport;
  4192. }
  4193. else
  4194. {
  4195. IEC101_DEF *pt101 = pt;
  4196. cmd = pt101->msg_ypara_send.ypara_msg.cmd;
  4197. dport = pt101->msg_ypara_send.dport;
  4198. }
  4199. pdi = pdat[2];
  4200. ps=&pdat[3];
  4201. if((pdi&0xc0)==0) // 定值固化
  4202. {
  4203. if(cmd == YP_SEL) /*有下发级联预置*/
  4204. {
  4205. return dport;
  4206. }
  4207. else
  4208. {
  4209. return 0;
  4210. }
  4211. }
  4212. else if((pdi&0xc0)==0x40) /*撤销*/
  4213. {
  4214. if(cmd == YP_SEL) /*有下发级联预置*/
  4215. {
  4216. return dport;
  4217. }
  4218. else
  4219. {
  4220. return 0;
  4221. }
  4222. }
  4223. else // 参数预置
  4224. {
  4225. di=ps[0]+(ps[1]<<8);
  4226. index = get_stbl_index_s(di);
  4227. if(index < 0) return 0; /*点号不对*/
  4228. return tParaID[index].link_ch; /*级联号*/
  4229. }
  4230. }
  4231. int iec_setread_mananger(void *pt,BYTE *pdat,BYTE vsq,bool b104) // dat从定值区号开始
  4232. {
  4233. int len,i;
  4234. u16 section; // 定值区号
  4235. BYTE setnum=0;
  4236. BYTE framelen = 0;
  4237. BYTE datbuf[256] = {0};
  4238. BYTE over=0;
  4239. RMT_FIXED_TABLE tfixedset;
  4240. SET_PARAID *tmp_param;
  4241. SET_PARA_VAL *tmp_pval;
  4242. RMT_SET_VAL tmp_cst_val;
  4243. SET_VALUE *psetbuf,*pparabuf,*pcstbuf,*ppubbuf;
  4244. section=pdat[0]+(pdat[1]<<8); // 定值区号
  4245. if(section>=SEC_NUMBER) // 定值区不为零,否定应答返回
  4246. {
  4247. rt_printf("\r\n远方读定值,定值区超出范围,否定应答\r\n");
  4248. return -1;
  4249. }
  4250. ppubbuf = rt_malloc(PUB_SET_NUMBER*4);
  4251. if(!ppubbuf)
  4252. {
  4253. rt_printf("\r\n远方读定值,分配公共定值缓冲区错误!\r\n");
  4254. return -1;
  4255. }
  4256. psetbuf = rt_malloc(SW_SET_NUMBER*4);
  4257. if(!psetbuf)
  4258. {
  4259. rt_free(ppubbuf);
  4260. rt_printf("\r\n远方读定值,分配开关定值缓冲区错误!\r\n");
  4261. return -2;
  4262. }
  4263. pparabuf = rt_malloc(EQUIP_PARA_NUMBER*4);
  4264. if(!pparabuf)
  4265. {
  4266. rt_free(ppubbuf);
  4267. rt_free(psetbuf);
  4268. rt_printf("\r\n远方读定值,分配参数缓冲区错误!\r\n");
  4269. return -3;
  4270. }
  4271. pcstbuf = rt_malloc(CSTSET_NUMBER*4);
  4272. if(!pcstbuf)
  4273. {
  4274. rt_free(ppubbuf);
  4275. rt_free(psetbuf);
  4276. rt_free(pparabuf);
  4277. rt_printf("\r\n远方读定值,分配内部定值缓冲区错误!\r\n");
  4278. return -2;
  4279. }
  4280. if(!ReadPara((void*)ppubbuf,EEP_PUB_ADDR,PUB_SET_NUMBER,&tPubSetTable[0]))
  4281. {
  4282. GetDefPara((void*)ppubbuf,PUB_SET_NUMBER,&tPubSetTable[0]);
  4283. }
  4284. if(!ReadPara((void*)psetbuf,EEP_SET_ADDR+m_runsection*SETSIZE,SW_SET_NUMBER,&tSwSetTable[0]))
  4285. {
  4286. GetDefPara((void*)psetbuf,SW_SET_NUMBER,&tSwSetTable[0]);
  4287. }
  4288. if(!ReadPara((void*)pparabuf,EEP_EQUIP_PARA_ADDR,EQUIP_PARA_NUMBER,&tEquipParaTable[0]))
  4289. {
  4290. GetDefPara((void*)pparabuf,EQUIP_PARA_NUMBER,&tEquipParaTable[0]);
  4291. }
  4292. if(!ReadPara((void*)pcstbuf,EEP_CSTSET_ADDR,CSTSET_NUMBER,&tCstSetTable[0]))
  4293. {
  4294. GetDefPara((void*)pcstbuf,CSTSET_NUMBER,&tCstSetTable[0]);
  4295. }
  4296. iec_init_fixedset(pt,&tfixedset,b104); // 获取固定定值
  4297. datbuf[0]=(BYTE)section; // 区号
  4298. datbuf[1]=(BYTE)(section>>8);
  4299. datbuf[2]=0x01; // 参数特征标识,有后续
  4300. framelen=3;
  4301. setnum=0;
  4302. if(vsq==0) // 读全部定值
  4303. {
  4304. for(i=0;i<tfixedset.num;i++) // 固定定值
  4305. {
  4306. if(tfixedset.set[i].di==0)
  4307. continue;
  4308. tmp_cst_val=tfixedset.set[i];
  4309. len=iec_add2frame(datbuf+framelen,&tmp_cst_val,b104);
  4310. framelen+=len;
  4311. setnum++;
  4312. if(framelen>200)
  4313. {
  4314. if(b104)
  4315. {
  4316. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,setnum,IEC_COT_ACTCON,202); //
  4317. }
  4318. else
  4319. {
  4320. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,setnum,IEC_COT_ACTCON,202,FRAME_I);//
  4321. }
  4322. framelen=3;
  4323. setnum=0;
  4324. }
  4325. }
  4326. for(i=0;i<ParaIDNum;i++)
  4327. {
  4328. if(!tParaID[i].parId)
  4329. continue;
  4330. if(tParaID[i].link_ch == 0) /*本地定值*/
  4331. {
  4332. tmp_param= &tParaID[i];
  4333. switch(tmp_param->group_type)
  4334. {
  4335. case SETGROUP_TYPE_PARA:
  4336. iec_set2rmtset(&tmp_cst_val,tEquipParaTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,pparabuf[tmp_param->setno].ff);
  4337. break;
  4338. case SETGROUP_TYPE_PUB:
  4339. iec_set2rmtset(&tmp_cst_val,tPubSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,ppubbuf[tmp_param->setno].ff);
  4340. break;
  4341. case SETGROUP_TYPE_CSTSET:
  4342. iec_set2rmtset(&tmp_cst_val,tCstSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,pcstbuf[tmp_param->setno].ff);
  4343. break;
  4344. case SETGROUP_TYPE_BH:
  4345. iec_set2rmtset(&tmp_cst_val,tSwSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,psetbuf[tmp_param->setno].ff);
  4346. break;
  4347. default:
  4348. continue;
  4349. }
  4350. }
  4351. else
  4352. {
  4353. /*级联定值的值*/
  4354. tmp_param= &tParaID[i];
  4355. tmp_pval = &tPara_val[i];
  4356. tmp_cst_val.unit = tmp_pval->datatype;
  4357. tmp_cst_val.len = tmp_pval->len;
  4358. memcpy(tmp_cst_val.str,tmp_pval->str,tmp_pval->len);
  4359. }
  4360. /*组包*/
  4361. tmp_cst_val.di = tmp_param->parId;
  4362. len=iec_add2frame(datbuf+framelen,&tmp_cst_val,b104);
  4363. framelen+=len;
  4364. setnum++;
  4365. if(i == ParaIDNum - 1)
  4366. {
  4367. over = 1;
  4368. }
  4369. if((framelen > 200) || over)
  4370. {
  4371. if(over) //最后的一帧,tag清零
  4372. {
  4373. datbuf[2]=0;
  4374. }
  4375. if(b104)
  4376. {
  4377. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,setnum,IEC_COT_ACTCON,202); //
  4378. }
  4379. else
  4380. {
  4381. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,setnum,IEC_COT_ACTCON,202,FRAME_I);//
  4382. }
  4383. framelen=3;
  4384. setnum=0;
  4385. }
  4386. }
  4387. }
  4388. else // 读定义点号的定值
  4389. {
  4390. BYTE *ps=&pdat[2]; //从点号开始
  4391. for(i=0;i<vsq;i++)
  4392. {
  4393. int j;
  4394. u16 di;
  4395. bool bfind=false;
  4396. di=ps[0]+(ps[1]<<8);
  4397. for(j=0;j<tfixedset.num;j++) // 固定定值
  4398. {
  4399. if(tfixedset.set[j].di==di) // 点号对应
  4400. {
  4401. tmp_cst_val=tfixedset.set[j];
  4402. bfind=true;
  4403. break;
  4404. }
  4405. }
  4406. if(!bfind)
  4407. {
  4408. for(j=0;j<ParaIDNum;j++) // 修改定值
  4409. {
  4410. if(di != tParaID[j].parId)
  4411. continue;
  4412. if(tParaID[j].link_ch == 0) /*本地定值*/
  4413. {
  4414. tmp_param= &tParaID[j];
  4415. switch(tmp_param->group_type)
  4416. {
  4417. case SETGROUP_TYPE_PARA:
  4418. iec_set2rmtset(&tmp_cst_val,tEquipParaTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,pparabuf[tmp_param->setno].ff);
  4419. break;
  4420. case SETGROUP_TYPE_PUB:
  4421. iec_set2rmtset(&tmp_cst_val,tPubSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,ppubbuf[tmp_param->setno].ff);
  4422. break;
  4423. case SETGROUP_TYPE_CSTSET:
  4424. iec_set2rmtset(&tmp_cst_val,tCstSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,pcstbuf[tmp_param->setno].ff);
  4425. break;
  4426. case SETGROUP_TYPE_BH:
  4427. iec_set2rmtset(&tmp_cst_val,tSwSetTable[tmp_param->setno].data_type,tmp_param->group_type,tmp_param->setno,psetbuf[tmp_param->setno].ff);
  4428. break;
  4429. default:
  4430. continue;
  4431. }
  4432. }
  4433. else
  4434. {
  4435. /*级联定值的值*/
  4436. tmp_param= &tParaID[j];
  4437. tmp_pval = &tPara_val[j];
  4438. tmp_cst_val.unit = tmp_pval->datatype;
  4439. tmp_cst_val.len = tmp_pval->len;
  4440. memcpy(tmp_cst_val.str,tmp_pval->str,tmp_pval->len);
  4441. }
  4442. bfind=true;
  4443. break;
  4444. }
  4445. }
  4446. ps+=2;
  4447. if(b104)ps++;
  4448. // 组报文
  4449. if(bfind)
  4450. {
  4451. tmp_cst_val.di=di;
  4452. len=iec_add2frame(datbuf+framelen,&tmp_cst_val,b104);
  4453. framelen+=len;
  4454. setnum++;
  4455. }
  4456. if(framelen>200||i==vsq-1)
  4457. {
  4458. if(i==vsq-1) //最后的一帧,tag清零
  4459. {
  4460. datbuf[2]=0;
  4461. }
  4462. if(b104)
  4463. {
  4464. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,setnum,IEC_COT_ACTCON,202); //
  4465. }
  4466. else
  4467. {
  4468. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,setnum,IEC_COT_ACTCON,202,FRAME_I);//
  4469. }
  4470. framelen=3;
  4471. setnum=0;
  4472. }
  4473. }
  4474. }
  4475. rt_free(psetbuf);
  4476. rt_free(pparabuf);
  4477. rt_free(pcstbuf);
  4478. rt_free(ppubbuf);
  4479. return 0;
  4480. }
  4481. #endif
  4482. #ifdef YPARA_LINK_S
  4483. void set_para_flag(void)
  4484. {
  4485. int net;
  4486. IEC104_DEF *pt;
  4487. for(net=0; net<CFG_ETH_MAX_LOGIC; net++)
  4488. {
  4489. pt=&g_t104[net];
  4490. pt->para_updata_flag=1;
  4491. }
  4492. }
  4493. #endif
  4494. int iec_rmtset2set( RMT_SET_DATA *rsd,BYTE *pdat ,bool b104)
  4495. {
  4496. int i,len,index;
  4497. BYTE Tag,addoff=0;
  4498. WORD di;
  4499. bool bover=false;
  4500. di=pdat[0]+(pdat[1]<<8);
  4501. index=0;
  4502. for(i=0;i<RMT_SET_NUMBER;i++)
  4503. {
  4504. if(rsd->di[i]==di) // 预置数据中有对应的点号的定值,覆盖
  4505. {
  4506. index=i;
  4507. bover=true;
  4508. break;
  4509. }
  4510. }
  4511. if(!bover)
  4512. {
  4513. for(i=0;i<RMT_SET_NUMBER;i++)
  4514. {
  4515. if(rsd->di[i]==0) //结构中有点号为0的,可以使用
  4516. {
  4517. index=i;
  4518. break;
  4519. }
  4520. }
  4521. }
  4522. if(b104)addoff=1;
  4523. Tag=pdat[2+addoff];
  4524. len=pdat[3+addoff];
  4525. pdat+=(4+addoff);
  4526. rsd->di[index]=di;
  4527. switch(Tag)
  4528. {
  4529. case BOOL_R:
  4530. case TINY_R:
  4531. case UTINY_R:
  4532. rsd->val[index]=pdat[0];
  4533. break;
  4534. case UINT_R:
  4535. case INT_R:
  4536. case LONG_R:
  4537. case ULONG_R:
  4538. rsd->val[index]=(pdat[0]+(pdat[1]<<8)+(pdat[2]<<16)+(pdat[3]<<24));
  4539. break;
  4540. case SHORT_R:
  4541. case USHORT_R:
  4542. rsd->val[index]=(pdat[0]+(pdat[1]<<8));
  4543. break;
  4544. case FLOAT_R:
  4545. {
  4546. union{
  4547. float ff;
  4548. u8 tt[4];
  4549. }ff;
  4550. CopySwap(ff.tt,pdat,4,false);
  4551. rsd->val[index]=ff.ff;
  4552. break;
  4553. }
  4554. case STR_R:
  4555. {
  4556. memset(rsd->t_str[rmt_str_cnt].str,0,sizeof(rsd->t_str[rmt_str_cnt].str));
  4557. memcpy(rsd->t_str[rmt_str_cnt].str,pdat,len);
  4558. rsd->t_str[rmt_str_cnt].di = rsd->di[index];
  4559. rt_printf("%s:di=%04x,str=%s\r\n",__func__,rsd->t_str[rmt_str_cnt].di,rsd->t_str[rmt_str_cnt].str);
  4560. rmt_str_cnt++;
  4561. }
  4562. break;
  4563. default:
  4564. rsd->val[index]=0;
  4565. break;
  4566. }
  4567. return len;
  4568. }
  4569. int iec_set_section=0;
  4570. int iec_saveset( RMT_SET_DATA *rsd)
  4571. {
  4572. SET_VALUE *psetbuf,*pparabuf,*pcstbuf;
  4573. int i;
  4574. bool bset=false,bpara=false,bcst=false;
  4575. int ret=-1;
  4576. SET_PARAID *tmp_param;
  4577. if(!pRunSet->bTT_EDIT_YB) //远方修改未投入
  4578. {
  4579. for(i=0;i<RMT_SET_NUMBER;i++)
  4580. {
  4581. rsd->di[i]=0;
  4582. }
  4583. rsd->bpreset=false;
  4584. rt_printf("\r\n远方修改定值未投入!\r\n");
  4585. return -1;
  4586. }
  4587. psetbuf = rt_malloc(SET_NUMBER*4);
  4588. if(!psetbuf)
  4589. {
  4590. rt_printf("\r\n远方固化定值,分配定值缓冲区错误!\r\n");
  4591. return -2;
  4592. }
  4593. pparabuf = rt_malloc(EQUIP_PARA_NUMBER*4);
  4594. if(!pparabuf)
  4595. {
  4596. rt_free(psetbuf);
  4597. rt_printf("\r\n远方固化定值,分配参数缓冲区错误!\r\n");
  4598. return -3;
  4599. }
  4600. pcstbuf = rt_malloc(CSTSET_NUMBER*4);
  4601. if(!pcstbuf)
  4602. {
  4603. //rt_free(pcstbuf);
  4604. rt_free(psetbuf);
  4605. rt_free(pparabuf);
  4606. rt_printf("\r\n远方固化定值,分配内部定值缓冲区错误!\r\n");
  4607. return -4;
  4608. }
  4609. if(!ReadSet(iec_set_section,(void *)psetbuf)) // 读定值
  4610. {
  4611. GetDefSet((float*)psetbuf);
  4612. }
  4613. if(!ReadPara((void*)pparabuf,EEP_EQUIP_PARA_ADDR,EQUIP_PARA_NUMBER,&tEquipParaTable[0]))// 读参数
  4614. {
  4615. GetDefPara((void*)pparabuf,EQUIP_PARA_NUMBER,&tEquipParaTable[0]);
  4616. }
  4617. /*读取内部定值*/
  4618. if(!ReadPara((void*)pcstbuf,EEP_CSTSET_ADDR,CSTSET_NUMBER,&tCstSetTable[0]))
  4619. {
  4620. GetDefPara((void*)pcstbuf,CSTSET_NUMBER,&tCstSetTable[0]);
  4621. }
  4622. for(i=0;i<RMT_SET_NUMBER;i++)
  4623. {
  4624. int j;
  4625. WORD di;
  4626. bool bfind=false;
  4627. di=rsd->di[i];
  4628. if(di==0)continue;
  4629. for(j=0;j<ParaIDNum;j++)
  4630. {
  4631. int k;
  4632. if(di != tParaID[j].parId)
  4633. continue;
  4634. tmp_param = &tParaID[j];
  4635. switch(tmp_param->group_type)
  4636. {
  4637. case SETGROUP_TYPE_PARA:
  4638. if(tEquipParaTable[tmp_param->setno].wType == SETTYPE_IP)
  4639. {
  4640. union{
  4641. float ff;
  4642. u8 tt[4];
  4643. }ff;
  4644. for(k=0;k<rmt_str_cnt;k++)
  4645. {
  4646. if(di == rsd->t_str[k].di)
  4647. {
  4648. if(iec_get_equi_ip(rsd->t_str[k].str,ff.tt)==0)
  4649. {
  4650. pparabuf[tmp_param->setno].ff=ff.ff;
  4651. }
  4652. bpara=true;
  4653. break;
  4654. }
  4655. }
  4656. }
  4657. else
  4658. {
  4659. pparabuf[tmp_param->setno].ff=rsd->val[i];
  4660. }
  4661. bpara=true;
  4662. break;
  4663. case SETGROUP_TYPE_PUB:
  4664. psetbuf[tmp_param->setno].ff=rsd->val[i];
  4665. bset=true;
  4666. break;
  4667. case SETGROUP_TYPE_CSTSET:
  4668. pcstbuf[tmp_param->setno].ff=rsd->val[i];
  4669. bcst=true;
  4670. break;
  4671. case SETGROUP_TYPE_BH: // 第一个开关的定值
  4672. psetbuf[PUB_SET_NUMBER+tmp_param->setno].ff=rsd->val[i];
  4673. bset=true;
  4674. break;
  4675. default:break;
  4676. }
  4677. bfind=true;
  4678. }
  4679. rsd->di[i] = 0;
  4680. rsd->val[i] = 0;
  4681. }
  4682. if(bset)
  4683. {
  4684. if(SaveSet(iec_set_section,(void *)psetbuf))
  4685. {
  4686. soe_record_opt(EV_SET_OK,0);
  4687. if(iec_set_section != m_runsection) rt_printf("----iec_set_section=%d,m_runsection=%d\r\n",iec_set_section,m_runsection);
  4688. //if(iec_set_section==m_runsection)
  4689. {
  4690. MakeRunSet(false);
  4691. rt_err_clr(ERR_CODE_SET,0);
  4692. }
  4693. }
  4694. else
  4695. {
  4696. soe_record_opt(EV_SET_FAIL,0);
  4697. }
  4698. ret=0;
  4699. }
  4700. if(bpara)
  4701. {
  4702. if(SavePara((void *)pparabuf,EEP_EQUIP_PARA_ADDR,EQUIP_PARA_NUMBER,&tEquipParaTable[0]))
  4703. {
  4704. soe_record_opt(EV_EQUPARA_OK,0);
  4705. MakeRunPara(false, false);
  4706. rt_err_clr(ERR_CODE_EQU_PARA,0);
  4707. }
  4708. else
  4709. {
  4710. soe_record_opt(EV_EQUPARA_FAIL,0);
  4711. }
  4712. ret=0;
  4713. }
  4714. if(bcst)
  4715. {
  4716. if(SavePara((void *)pcstbuf,EEP_CSTSET_ADDR,CSTSET_NUMBER,&tCstSetTable[0]))
  4717. {
  4718. soe_record_opt(EV_CSTSET_OK,0);
  4719. MakeRunSet(false);
  4720. rt_err_clr(ERR_CODE_SET_IN,0);
  4721. }
  4722. else
  4723. {
  4724. soe_record_opt(EV_CSTSET_FAIL,0);
  4725. }
  4726. ret=0;
  4727. }
  4728. #ifdef YPARA_LINK_S
  4729. set_para_flag();
  4730. #endif
  4731. rt_free(psetbuf); // sunxi 20180719 added
  4732. rt_free(pparabuf); // sunxi 20180719 added
  4733. rt_free(pcstbuf); // sunxi 20180719 added
  4734. return ret;
  4735. }
  4736. int iec_setwrite(void *pt,BYTE *pdat,BYTE cot,BYTE vsq,bool b104) // dat从定值区号开始
  4737. {
  4738. WORD section; // 定值区号
  4739. BYTE pdi; //参数特征标识符
  4740. int ret=0;
  4741. RMT_SET_DATA *rsd;
  4742. if(b104)
  4743. {
  4744. IEC104_DEF *pt104=(IEC104_DEF *)pt;
  4745. rsd=&pt104->tr;
  4746. }
  4747. else
  4748. {
  4749. IEC101_DEF *pt101=(IEC101_DEF *)pt;
  4750. rsd=&pt101->tr;
  4751. }
  4752. section=pdat[0]+(pdat[1]<<8); // 定值区号
  4753. pdi=pdat[2];
  4754. if(section>=SEC_NUMBER)
  4755. {
  4756. rt_printf("\r\n远方修改定值,定值区越限=%d,否定应答\r\n",section);
  4757. return -1;
  4758. }
  4759. iec_set_section=section;
  4760. if(((pdi&0xc0)==0)&&cot==0x06) // 定值固化
  4761. {
  4762. ret=iec_saveset(rsd);
  4763. rsd->bpreset=false;
  4764. }
  4765. else if(vsq>0&&((pdi&0xc0)==0x80)&&cot==0x06) // 参数预置
  4766. {
  4767. int i;
  4768. rmt_str_cnt=0;
  4769. pdat+=3;
  4770. for(i=0;i<vsq;i++)
  4771. {
  4772. int len;
  4773. len=iec_rmtset2set(rsd,&pdat[0],b104);
  4774. pdat+=(4+len);
  4775. if(b104)pdat+=1;
  4776. }
  4777. rsd->bpreset=true;
  4778. rsd->us0_preset=ustimer_get_origin();
  4779. }
  4780. else if(((pdi&0xc0)==0x40)&&cot==0x09) // 取消预置
  4781. {
  4782. int i;
  4783. rmt_str_cnt=0;
  4784. for(i=0;i<RMT_SET_NUMBER;i++)
  4785. {
  4786. rsd->di[i]=0;
  4787. }
  4788. rsd->bpreset=false;
  4789. }
  4790. else
  4791. {
  4792. rt_printf("\r\n远方修改定值否定应答,vsq:%d 传送原因:0x%02x 特征表示符:0x%02x \r\n",vsq,cot,pdi);
  4793. ret= -2;
  4794. }
  4795. return ret;
  4796. }
  4797. void iec_printfixedset(void)
  4798. {
  4799. int i;
  4800. WORD index;
  4801. RMT_FIXED_TABLE tfixedset;
  4802. iec_init_fixedset(&g_t104[0],&tfixedset,true); // 获取固定定值
  4803. rt_printf("\r\n点号 名称 信息");
  4804. for(i=0;i<tfixedset.num;i++)
  4805. {
  4806. index=tfixedsettable[i].index;
  4807. if(index==FIXED_SET_CRC) // 软件版本号
  4808. {
  4809. rt_printf("\r\n%04x %-24s%04x",tfixedsettable[i].di,tfixedsettable[i].name,(tfixedset.set[i].str[0]+(tfixedset.set[i].str[1]<<8)));
  4810. }
  4811. else if(index==FIXED_SET_MAC1||index==FIXED_SET_MAC2) // 软件版本号
  4812. {
  4813. rt_printf("\r\n%04x %-24s%02x-%02x-%02x-%02x-%02x-%02x",tfixedsettable[i].di,tfixedsettable[i].name,
  4814. tfixedset.set[i].str[0],tfixedset.set[i].str[1],tfixedset.set[i].str[2],tfixedset.set[i].str[3],tfixedset.set[i].str[4],tfixedset.set[i].str[5]);
  4815. }
  4816. else
  4817. {
  4818. rt_printf("\r\n%04x %-24s%s",tfixedset.set[i].di,tfixedsettable[i].name,tfixedset.set[i].str);
  4819. }
  4820. }
  4821. }
  4822. /**************************************************************************
  4823. 函数名称:iec_Direct_Echo
  4824. 函数版本:1.00
  4825. 作者:
  4826. 创建日期:2007.9.1
  4827. 函数功能说明:101规约目录响应
  4828. 输出参数:pt101缓冲区地址,COT 传送原因
  4829. 返回值:
  4830. 更新信息:
  4831. 更新日志1:
  4832. 日期:
  4833. 修改者:
  4834. 修改内容:
  4835. 修改原因:
  4836. ***************************************************************************/
  4837. void iec_dir_echo(void *pt,char *buf,bool b104) // buf 从 操作标识下一字节开始
  4838. {
  4839. int i;
  4840. char *pdat;
  4841. char cf;// 操作及召唤标识
  4842. int id; // 目录ID
  4843. BYTE namelen;
  4844. char namestr[64];
  4845. int filenum;
  4846. struct dir_file_struct *pdir,*pdirfree;
  4847. BYTE framelen,filescnt;
  4848. char datbuf[256];
  4849. char *dir;
  4850. struct rtc_time_t ts,te;
  4851. T_FILE *ptfile;
  4852. if(b104) ptfile=&((IEC104_DEF *)pt)->tf;
  4853. else ptfile=&((IEC101_DEF *)pt)->tf;
  4854. pdat=buf;
  4855. id=pdat[0]+(pdat[1]<<8)+(pdat[2]<<16)+(pdat[3]<<24); // 目录ID
  4856. pdat+=4;
  4857. namelen=*pdat++; // 目录长度
  4858. dir=NULL;
  4859. strcpy(ptfile->dirpath,"");
  4860. if(namelen>0)
  4861. {
  4862. for(i=0;i<namelen;i++)
  4863. {
  4864. namestr[i]=*pdat++;
  4865. }
  4866. #ifdef FUN_PATH_TEST
  4867. if(strstr(namestr,"COMTRADE") != NULL) /*录波文件*/
  4868. {
  4869. strcpy(ptfile->dirpath,HF_WAVE_DIR);
  4870. }
  4871. else
  4872. {
  4873. sprintf(ptfile->dirpath,"/app/%s/",namestr);
  4874. }
  4875. #else
  4876. sprintf(ptfile->dirpath,"/app/%s/",namestr);
  4877. #endif
  4878. namestr[namelen]='\0'; // 目录名称字符串结束符
  4879. dir=namestr;
  4880. if(namestr[0]=='/')
  4881. {
  4882. sprintf(ptfile->dirpath,"/app%s/",namestr);//去掉'/'
  4883. }
  4884. else
  4885. {
  4886. sprintf(ptfile->dirpath,"/app/%s/",namestr);
  4887. }
  4888. }
  4889. cf=*pdat++; // 召唤标识
  4890. ts.ms=pdat[0]+(pdat[1]<<8); //查询起始时间
  4891. pdat+=2;
  4892. ts.min=*pdat++;
  4893. ts.hour=*pdat++;
  4894. ts.day=*pdat++;
  4895. ts.month=*pdat++;
  4896. ts.year=*pdat++;
  4897. ts.min&=0x3f;
  4898. ts.hour&=0x1f;
  4899. ts.month&=0x0f;
  4900. ts.day&=0x1f;
  4901. te.ms=pdat[0]+(pdat[1]<<8); //查询终止时间
  4902. pdat+=2;
  4903. te.min=*pdat++;
  4904. te.hour=*pdat++;
  4905. te.day=*pdat++;
  4906. te.month=*pdat++;
  4907. te.year=*pdat++;
  4908. te.min&=0x3f;
  4909. te.hour&=0x1f;
  4910. te.month&=0x0f;
  4911. te.day&=0x1f;
  4912. pdir=hf_get_dir_file_n(id,ptfile->dirpath,cf,&ts,&te,&filenum); // 根据信息体地址,判断读读哪个目录,获取目录信息
  4913. if(pdir==NULL)
  4914. {
  4915. filenum=0;
  4916. }
  4917. pdirfree=pdir;
  4918. // 组织报文
  4919. filescnt=0;
  4920. framelen=0;
  4921. pdat=datbuf;
  4922. pdat[framelen++]=0; //信息体地址
  4923. pdat[framelen++]=0;
  4924. if(b104)pdat[framelen++]=0;
  4925. pdat[framelen++]=2;// 附加数据包类型
  4926. pdat[framelen++]=2; // 读目录
  4927. pdat[framelen++]=(filenum>0)?0:1;// 结果描述符 :=1 失败
  4928. pdat[framelen++]=(BYTE)(id>>0); // 标识
  4929. pdat[framelen++]=(BYTE)(id>>8);
  4930. pdat[framelen++]=(BYTE)(id>>16);
  4931. pdat[framelen++]=(BYTE)(id>>24);
  4932. pdat[framelen++]=0; // 后续标识
  4933. pdat[framelen++]=filescnt; // 文件数量
  4934. if(filenum==0)
  4935. {
  4936. if(b104)
  4937. {
  4938. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,ptfile->vsq,IEC_COT_REQ,210); //
  4939. }
  4940. else
  4941. {
  4942. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,ptfile->vsq,IEC_COT_REQ,210,FRAME_I);//
  4943. }
  4944. return ;
  4945. }
  4946. datbuf[9+b104]=1; // 后续标识
  4947. pdat=&datbuf[framelen];
  4948. for(i=0;i<filenum;i++)
  4949. {
  4950. struct rtc_time_t rtc;
  4951. u8 week;
  4952. int len;
  4953. len=strlen(pdir->name);
  4954. *pdat++=len; //文件名长度
  4955. memcpy(pdat,pdir->name,len);
  4956. pdat+=len;
  4957. *pdat++=0; //文件属性
  4958. /*文件长度*/
  4959. *pdat++ = (BYTE)(pdir->file_size);
  4960. *pdat++ = (BYTE)(pdir->file_size>>8);
  4961. *pdat++ = (BYTE)(pdir->file_size>>16);
  4962. *pdat++ = (BYTE)(pdir->file_size>>24);
  4963. /*日历时钟*/
  4964. timespec_to_rtc(pdir->file_time,&rtc,1);
  4965. week = WEEK_DAY(pdir->file_time.tv_sec);
  4966. *pdat++ = (BYTE)(rtc.ms);
  4967. *pdat++ = (BYTE)(rtc.ms>>8);
  4968. *pdat++ = (BYTE)(rtc.min);
  4969. *pdat++ = (BYTE)(rtc.hour);
  4970. *pdat++ = (BYTE)((week<<5) |rtc.day);
  4971. *pdat++ = (BYTE)(rtc.month);
  4972. *pdat++ = (BYTE)(rtc.year);
  4973. framelen+=len+13;
  4974. filescnt++;
  4975. pdir++;
  4976. if(framelen>160||i==(filenum-1))
  4977. {
  4978. if(i==(filenum-1))
  4979. {
  4980. datbuf[9+b104]=0; // 后续标识
  4981. }
  4982. datbuf[10+b104]=filescnt; // 文件数量
  4983. if(b104)
  4984. {
  4985. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,ptfile->vsq,IEC_COT_REQ,210); //
  4986. ptfile=&((IEC104_DEF *)pt)->tf;
  4987. }
  4988. else
  4989. {
  4990. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,ptfile->vsq,IEC_COT_REQ,210,FRAME_I);//
  4991. ptfile=&((IEC101_DEF *)pt)->tf;
  4992. }
  4993. framelen=11+b104;
  4994. filescnt=0;
  4995. pdat=&datbuf[framelen];
  4996. }
  4997. }
  4998. rt_free(pdirfree);
  4999. }
  5000. void iec_readfile_echo(void *pt,char *buf,bool b104) // buf 从 操作标识下一字节开始
  5001. {
  5002. int i;
  5003. char *pdat,*pfilebuf;
  5004. BYTE namelen,framelen;
  5005. int filelenth;
  5006. //char namestr[64],tmpstr[64];
  5007. char namestr[256],tmpstr[256];//modify by sunxi: 20220629
  5008. char datbuf[256];
  5009. uint32_t id;
  5010. T_FILE *ptfile;
  5011. int mulen=0;
  5012. if(b104) ptfile=&((IEC104_DEF *)pt)->tf;
  5013. else ptfile=&((IEC101_DEF *)pt)->tf;
  5014. pdat=buf;
  5015. namelen=*pdat++; // 目录长度
  5016. if(namelen>256)//modify by sunxi: 20220629 这里的长度有可能会超过256!!!
  5017. {
  5018. namelen = 255;
  5019. rt_printf("iec_readfile_echo--------- namelen > 256 !!!!! error!!!\r\n");
  5020. }
  5021. strcpy(tmpstr,ptfile->dirpath);
  5022. //if(namelen>0)
  5023. {
  5024. for(i=0;i<namelen;i++)
  5025. {
  5026. namestr[i]=*pdat++;
  5027. if(namestr[i]=='/') // 文件名称带目录,记录目录结束位置
  5028. {
  5029. mulen=i;
  5030. }
  5031. }
  5032. }
  5033. namestr[namelen]='\0'; // 文件名称字符串结束符
  5034. if(mulen>0&&mulen<namelen) // 文件名带目录
  5035. {
  5036. mulen+=1; //将下发名称的目录信息去掉
  5037. }
  5038. #ifdef RCD_STRAN_S
  5039. if(strncmp(namestr,"BAY",3) == 0) /*录波文件*/
  5040. {
  5041. strcpy(tmpstr,HF_WAVE_DIR);
  5042. rt_printf_time("-------传输文件%s激活\r\n",namestr);
  5043. if(ptfile->bTransing||ptfile->bdatTraned) /*激活文件时有文件传输,释放*/
  5044. {
  5045. iec_freefile(ptfile);
  5046. }
  5047. }
  5048. #endif
  5049. #ifdef FUN_DIR_DEPTH
  5050. if(strncmp(namestr,"soe",strlen("soe")) == 0)
  5051. {
  5052. if(strstr(ptfile->dirpath,"HISTORY")) strcpy(tmpstr,HF_SOE_101_DIR);
  5053. }
  5054. else if(strncmp(namestr,"ulog",strlen("ulog")) == 0)
  5055. {
  5056. if(strstr(ptfile->dirpath,"HISTORY")) strcpy(tmpstr,HF_LOG_101_DIR);
  5057. }
  5058. else if(strncmp(namestr,"co",strlen("co")) == 0)
  5059. {
  5060. if(strstr(ptfile->dirpath,"HISTORY")) strcpy(tmpstr,HF_CO_101_DIR);
  5061. }
  5062. #endif
  5063. strcat(tmpstr,&namestr[mulen]);
  5064. if(strcmp(p_his_file[HS_FILE_SOE],&namestr[mulen])==0)
  5065. {
  5066. if(!del_history_file(HS_FILE_SOE))
  5067. {
  5068. create_event_rcd_file(HS_FILE_SOE);
  5069. }
  5070. //rt_printf("%s:tmpstr=%s,namestr=%s\r\n",__func__,tmpstr,&namestr[mulen]);
  5071. }
  5072. else if(strcmp(p_his_file[HS_FILE_CO],&namestr[mulen])==0)
  5073. {
  5074. if(!del_history_file(HS_FILE_CO))
  5075. {
  5076. create_event_rcd_file(HS_FILE_CO);
  5077. }
  5078. //rt_printf("%s:tmpstr=%s,namestr=%s\r\n",__func__,tmpstr,&namestr[mulen]);
  5079. }
  5080. pfilebuf=hf_get_file_inf_n(tmpstr,&id,&filelenth);
  5081. if(pfilebuf==NULL)
  5082. {
  5083. id=0;
  5084. filelenth=0;
  5085. }
  5086. // 组织报文
  5087. pdat=datbuf;
  5088. framelen=0;
  5089. pdat[framelen++]=0; //信息体地址
  5090. pdat[framelen++]=0;
  5091. if(b104)pdat[framelen++]=0;
  5092. pdat[framelen++]=2;// 附加数据包类型
  5093. pdat[framelen++]=4; // 读文件激活
  5094. pdat[framelen++]=(filelenth>0)?0:1;// 结果描述符 :=1 失败
  5095. pdat[framelen++]=namelen; //文件名长度
  5096. for(i=0;i<namelen;i++)
  5097. {
  5098. pdat[framelen++]=namestr[i];
  5099. }
  5100. /*文件标识*/
  5101. pdat[framelen++] = (BYTE)(id);
  5102. pdat[framelen++] = (BYTE)(id>>8);
  5103. pdat[framelen++] = (BYTE)(id>>16);
  5104. pdat[framelen++] = (BYTE)(id>>24);
  5105. /*文件长度*/
  5106. pdat[framelen++] = (BYTE)(filelenth);
  5107. pdat[framelen++] = (BYTE)(filelenth>>8);
  5108. pdat[framelen++] = (BYTE)(filelenth>>16);
  5109. pdat[framelen++] = (BYTE)(filelenth>>24);
  5110. if(b104)
  5111. {
  5112. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,ptfile->vsq,IEC_COT_ACTCON,210); //
  5113. }
  5114. else
  5115. {
  5116. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,ptfile->vsq,IEC_COT_ACTCON,210,FRAME_I);//
  5117. }
  5118. //设置文件传输标志,在各规约处理中发送
  5119. if(filelenth>0)
  5120. {
  5121. ptfile->pfilebuf=pfilebuf;
  5122. ptfile->bTransing=true;
  5123. ptfile->offset=0;
  5124. ptfile->id=id;
  5125. ptfile->filelenth=filelenth;
  5126. ptfile->us0_file_trans=ustimer_get_origin();
  5127. }
  5128. }
  5129. void iec_readfile_send(void *pt,bool b104) //
  5130. {
  5131. int i;
  5132. char *pdat;
  5133. BYTE framelen;
  5134. char datbuf[256];
  5135. T_FILE *ptfile;
  5136. bool beof=false;
  5137. BYTE checksum=0;
  5138. int datalen=FILE_SEND_FRAME_LENTH;
  5139. if(b104)
  5140. {
  5141. ptfile=&((IEC104_DEF *)pt)->tf;
  5142. }
  5143. else
  5144. {
  5145. ptfile=&((IEC101_DEF *)pt)->tf;
  5146. }
  5147. if(!ptfile->bTransing||ptfile->offset>=ptfile->filelenth)return ;
  5148. if(ptfile->offset+FILE_SEND_FRAME_LENTH>=ptfile->filelenth)
  5149. {
  5150. datalen=ptfile->filelenth-ptfile->offset; //最后一帧传输的字节数
  5151. beof=true;
  5152. }
  5153. // 组织报文
  5154. pdat=datbuf; \
  5155. framelen=0;
  5156. pdat[framelen++]=0; //信息体地址
  5157. pdat[framelen++]=0;
  5158. if(b104)pdat[framelen++]=0;
  5159. pdat[framelen++]=2;// 附加数据包类型
  5160. pdat[framelen++]=5; //读文件数据响应
  5161. /*文件标识*/
  5162. pdat[framelen++] = (BYTE)(ptfile->id);
  5163. pdat[framelen++] = (BYTE)(ptfile->id>>8);
  5164. pdat[framelen++] = (BYTE)(ptfile->id>>16);
  5165. pdat[framelen++] = (BYTE)(ptfile->id>>24);
  5166. /*文件偏移*/
  5167. pdat[framelen++] = (BYTE)(ptfile->offset);
  5168. pdat[framelen++] = (BYTE)(ptfile->offset>>8);
  5169. pdat[framelen++] = (BYTE)(ptfile->offset>>16);
  5170. pdat[framelen++] = (BYTE)(ptfile->offset>>24);
  5171. pdat[framelen++] = beof?0:1; // 后续标志 0:无后续 1:有后续
  5172. for(i=0;i<datalen;i++)
  5173. {
  5174. BYTE dd;
  5175. dd=ptfile->pfilebuf[ptfile->offset+i];
  5176. pdat[framelen++] = dd;
  5177. checksum+=dd;
  5178. }
  5179. pdat[framelen++] = checksum; // 校验和
  5180. if(b104)
  5181. {
  5182. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,ptfile->vsq,IEC_COT_REQ,210); //
  5183. if(!beof)
  5184. {
  5185. ptfile->offset+=FILE_SEND_FRAME_LENTH;
  5186. ptfile->bTransing=true; // 104规约 继续传输
  5187. }
  5188. }
  5189. else
  5190. {
  5191. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,ptfile->vsq,IEC_COT_REQ,210,FRAME_I);//
  5192. if(pRunSet->bTT_101Transsure)
  5193. {
  5194. ptfile->bTransing=false; // 101规约 bTransing 在收到文件接收确认帧后置位,启动发送
  5195. }
  5196. else
  5197. {
  5198. if(!beof)
  5199. {
  5200. ptfile->offset+=FILE_SEND_FRAME_LENTH;
  5201. ptfile->bTransing=true; // 继续传输
  5202. }
  5203. }
  5204. }
  5205. if(beof) // 文件传输完毕
  5206. {
  5207. #ifdef RCD_STRAN_S
  5208. newrcd_queque *rcd;
  5209. if(!b104)
  5210. {
  5211. rcd=&((IEC101_DEF *)pt)->rcd_qt;
  5212. del_data_queue(&rcd->queue);
  5213. log_str_time(LOG_OPERATE,"文件传输结束",0,1);
  5214. }
  5215. else
  5216. {
  5217. rcd=&((IEC104_DEF *)pt)->rcd_qt;
  5218. del_data_queue(&rcd->queue);
  5219. rt_printf("---------文件传输结束,清缓存\r\n");
  5220. log_str_time(LOG_OPERATE,"文件传输结束",0,1);
  5221. }
  5222. #endif
  5223. ptfile->bTransing=false;
  5224. ptfile->bdatTraned=false;
  5225. iec_freefile(ptfile);
  5226. }
  5227. else
  5228. {
  5229. ptfile->bdatTraned=true;
  5230. ptfile->us0_file_trans=ustimer_get_origin();
  5231. }
  5232. }
  5233. void iec_readfile_sure(void *pt,u8 *buf,bool b104) //buf 从 操作标识下一字节开始
  5234. {
  5235. T_FILE *ptfile;
  5236. BYTE *pdat;
  5237. DWORD id,offset;
  5238. pdat=buf;
  5239. id=pdat[0]|(pdat[1]<<8)|(pdat[2]<<16)|(pdat[3]<<24); // 目录ID
  5240. pdat+=4;
  5241. offset=pdat[0]+(pdat[1]<<8)+(pdat[2]<<16)+(pdat[3]<<24); // 偏移
  5242. if(b104)
  5243. {
  5244. ptfile=&((IEC104_DEF *)pt)->tf;
  5245. }
  5246. else
  5247. {
  5248. ptfile=&((IEC101_DEF *)pt)->tf;
  5249. }
  5250. if(ptfile->id!=id||
  5251. (offset+FILE_SEND_FRAME_LENTH>=ptfile->filelenth))
  5252. {
  5253. return; // 文件已传输完毕
  5254. }
  5255. ptfile->offset=offset+FILE_SEND_FRAME_LENTH; // 将偏移增加,继续传输
  5256. ptfile->bTransing=true;
  5257. ptfile->bdatTraned=false;
  5258. }
  5259. void iec_freefile(T_FILE *ptfile) // dat 从信息对象开始
  5260. {
  5261. if(ptfile->pfilebuf==NULL)return;
  5262. rt_free(ptfile->pfilebuf);
  5263. ptfile->pfilebuf=NULL;
  5264. rt_printf("\r\n文件节传输完毕,释放内存,文件ID:%d 长度:%d\r\n",ptfile->id,ptfile->filelenth);
  5265. }
  5266. void iec_writefile_echo(void *pt,BYTE *buf,bool b104) // buf 从 操作标识下一字节开始
  5267. {
  5268. int i;
  5269. BYTE *pdat;
  5270. BYTE namelen,framelen,ret;
  5271. char namestr[64];
  5272. char datbuf[256];
  5273. T_FILE *ptfile;
  5274. if(b104) ptfile=&((IEC104_DEF *)pt)->tf;
  5275. else ptfile=&((IEC101_DEF *)pt)->tf;
  5276. pdat=buf;
  5277. namelen=*pdat++; // 长度
  5278. if(namelen>=64)namelen=63; // 文件名不能超过64字节
  5279. if(namelen>0)
  5280. {
  5281. for(i=0;i<namelen;i++)
  5282. {
  5283. namestr[i]=*pdat++;
  5284. }
  5285. }
  5286. namestr[namelen]='\0'; // 文件名称字符串结束符
  5287. strcpy(ptfile->wfilename,"/tmp/");
  5288. strcat(ptfile->wfilename,namestr);
  5289. //if(ptfile->bUpdateStart) // 启动程序更新,备份升级文件名称
  5290. {
  5291. strcpy(ptfile->updatefilename,namestr);
  5292. }
  5293. ptfile->wId=pdat[0]|(pdat[1]<<8)|(pdat[2]<<16)|(pdat[3]<<24);
  5294. pdat+=4;
  5295. ptfile->wfilelenth=pdat[0]|(pdat[1]<<8)|(pdat[2]<<16)|(pdat[3]<<24);
  5296. rt_printf("\r\n%s wlenth=%d",ptfile->wfilename,ptfile->wfilelenth);
  5297. if(strstr(KO_FILE_NAME,namestr)==NULL)
  5298. {
  5299. ret=2;
  5300. }
  5301. else if(ptfile->wfilelenth>=2048*1024) // 文件大于2M
  5302. {
  5303. ret=3;
  5304. }
  5305. else
  5306. {
  5307. ret=0;
  5308. }
  5309. // 组织报文
  5310. pdat=datbuf; \
  5311. framelen=0;
  5312. pdat[framelen++]=0; //信息体地址
  5313. pdat[framelen++]=0;
  5314. if(b104)pdat[framelen++]=0;
  5315. pdat[framelen++]=2;// 附加数据包类型
  5316. pdat[framelen++]=8; // 写文件激活
  5317. pdat[framelen++]=ret;// 结果描述符 :=2 未知文件名
  5318. pdat[framelen++]=namelen; //文件名长度
  5319. for(i=0;i<namelen;i++)
  5320. {
  5321. pdat[framelen++]=namestr[i];
  5322. }
  5323. /*文件标识*/
  5324. pdat[framelen++] = (BYTE)(ptfile->wId);
  5325. pdat[framelen++] = (BYTE)(ptfile->wId>>8);
  5326. pdat[framelen++] = (BYTE)(ptfile->wId>>16);
  5327. pdat[framelen++] = (BYTE)(ptfile->wId>>24);
  5328. /*文件长度*/
  5329. pdat[framelen++] = (BYTE)(ptfile->wfilelenth);
  5330. pdat[framelen++] = (BYTE)(ptfile->wfilelenth>>8);
  5331. pdat[framelen++] = (BYTE)(ptfile->wfilelenth>>16);
  5332. pdat[framelen++] = (BYTE)(ptfile->wfilelenth>>24);
  5333. if(b104)
  5334. {
  5335. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,ptfile->vsq,IEC_COT_ACTCON,210); //
  5336. }
  5337. else
  5338. {
  5339. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,ptfile->vsq,IEC_COT_ACTCON,210,FRAME_I);//
  5340. }
  5341. if(ret==0) // 创建写文件接收缓冲区
  5342. {
  5343. ptfile->writebuf= rt_malloc(ptfile->wfilelenth); //
  5344. if(!ptfile->writebuf)
  5345. {
  5346. rt_printf("%s:rt_malloc eror\r\n", __func__);
  5347. return ;
  5348. }
  5349. ptfile->us0_file_write=ustimer_get_origin();
  5350. ptfile->bdatwriting=true;
  5351. }
  5352. }
  5353. void iec_writefile_sure(void *pt,BYTE *buf,bool b104,u8 datlen) // buf 从 操作标识下一字节开始 datlen为含操作标识的报文长度,即文档中关于文件相关报文的长度
  5354. {
  5355. int i;
  5356. BYTE *pdat;
  5357. BYTE framelen,ret;
  5358. u32 id,offset;
  5359. char datbuf[256];
  5360. T_FILE *ptfile;
  5361. bool beof=false;
  5362. if(b104) ptfile=&((IEC104_DEF *)pt)->tf;
  5363. else ptfile=&((IEC101_DEF *)pt)->tf;
  5364. pdat=buf;
  5365. id=pdat[0]|(pdat[1]<<8)|(pdat[2]<<16)|(pdat[3]<<24);
  5366. pdat+=4;
  5367. offset=pdat[0]|(pdat[1]<<8)|(pdat[2]<<16)|(pdat[3]<<24);
  5368. pdat+=4;
  5369. pdat+=1; //后续标识
  5370. datlen-=11;
  5371. ret=0;
  5372. for(i=0;i<datlen;i++)
  5373. {
  5374. if((offset+i)<ptfile->wfilelenth)
  5375. {
  5376. if(ptfile->writebuf) ptfile->writebuf[offset+i]=*pdat++;
  5377. }
  5378. }
  5379. if((offset+datlen)>=ptfile->wfilelenth)
  5380. {
  5381. beof=true;
  5382. }
  5383. ptfile->us0_file_write=ustimer_get_origin();
  5384. if(id!=ptfile->wId) // id不一致
  5385. {
  5386. ret=4;
  5387. beof=true;
  5388. rt_printf("写文件: %s ID不一致! id=%d,wId=%d\r\n",ptfile->wfilename,id,ptfile->wId);
  5389. }
  5390. if(ptfile->bUpdateCancel)
  5391. {
  5392. ptfile->bUpdateCancel=false;
  5393. beof=true;
  5394. ret=1;
  5395. }
  5396. if(beof) //文件结束,写缓冲到文件中
  5397. {
  5398. loff_t pos;
  5399. int retlen;
  5400. //创建文件
  5401. struct file * handle;
  5402. if(ret==0)
  5403. {
  5404. handle = rt_file_open(ptfile->wfilename,O_CREAT|O_RDWR|O_TRUNC,0);
  5405. if(IS_ERR(handle))
  5406. {
  5407. rt_printf("\r\n写文件: %s 创建失败!\r\n",ptfile->wfilename);
  5408. ret=1;
  5409. }
  5410. else
  5411. {
  5412. pos=0;
  5413. retlen=rt_file_write(handle,ptfile->writebuf,ptfile->wfilelenth,&pos);
  5414. rt_printf("\r\n写文件: %s 返回%d filelenth=%d\r\n",ptfile->wfilename,retlen,ptfile->wfilelenth);
  5415. if(retlen!=ptfile->wfilelenth)// 写入文件长度不对应
  5416. {
  5417. ret=3;
  5418. }
  5419. rt_file_close(handle,0);
  5420. }
  5421. }
  5422. ptfile->bdatwriting=false;
  5423. rt_free(ptfile->writebuf);
  5424. ptfile->writebuf=NULL;
  5425. }
  5426. if(beof||(!b104&&pRunSet->bTT_101Transsure )) // 101规约需发确认帧,或规约文件最后一帧
  5427. {
  5428. // 组织报文
  5429. pdat=datbuf;
  5430. framelen=0;
  5431. pdat[framelen++]=0; //信息体地址
  5432. pdat[framelen++]=0;
  5433. if(b104)pdat[framelen++]=0;
  5434. pdat[framelen++]=2;// 附加数据包类型
  5435. pdat[framelen++]=10; // 写文件激活
  5436. /*文件标识*/
  5437. pdat[framelen++] = (BYTE)(ptfile->wId);
  5438. pdat[framelen++] = (BYTE)(ptfile->wId>>8);
  5439. pdat[framelen++] = (BYTE)(ptfile->wId>>16);
  5440. pdat[framelen++] = (BYTE)(ptfile->wId>>24);
  5441. /*文件长度*/
  5442. pdat[framelen++] = (BYTE)(offset);
  5443. pdat[framelen++] = (BYTE)(offset>>8);
  5444. pdat[framelen++] = (BYTE)(offset>>16);
  5445. pdat[framelen++] = (BYTE)(offset>>24);
  5446. pdat[framelen++]=ret;// 结果描述符 :=2 未知文件名
  5447. if(b104)
  5448. {
  5449. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,ptfile->vsq,IEC_COT_REQ,210); //
  5450. }
  5451. else
  5452. {
  5453. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,ptfile->vsq,IEC_COT_REQ,210,FRAME_I);//
  5454. }
  5455. }
  5456. #ifdef SEC_BOX_CODE
  5457. if(b104&&!beof)
  5458. {
  5459. ptfile->recvwrnum+=2;
  5460. if(ptfile->recvwrnum>=tRunPara.w104K)
  5461. {
  5462. IEC104_Echo_US((IEC104_DEF *)pt,FRAME_S);
  5463. ptfile->recvwrnum=0;
  5464. }
  5465. }
  5466. #endif
  5467. }
  5468. int iec_file_app(void *pt,BYTE *buf,BYTE cot,BYTE framelen,bool b104) // dat 从信息体地址后开始 framelen 从信息体地址后 附件数据类型包计算
  5469. {
  5470. int ret=0;
  5471. if(buf[0]!=2)// 附加数据类型包 ==2 文件传输
  5472. {
  5473. return -1;
  5474. }
  5475. // 判断传送原因和操作标识是否一致,不一致返回错误
  5476. if(!(((buf[1]==3||buf[1]==7)&&(cot==IEC_COT_ACT))
  5477. ||((buf[1]==1||buf[1]==6||buf[1==9])&&(cot==IEC_COT_REQ))))
  5478. {
  5479. //return -2; // 为方便调试,先屏蔽此处理
  5480. }
  5481. switch(buf[1])
  5482. {
  5483. case 1: // 读目录
  5484. {
  5485. #ifdef GD_AREA_ECZD_JIEYANG_2022
  5486. if(!del_history_file(HS_FILE_SOE))
  5487. {
  5488. create_event_rcd_file(HS_FILE_SOE);
  5489. }
  5490. if(!del_history_file(HS_FILE_CO))
  5491. {
  5492. create_event_rcd_file(HS_FILE_CO);
  5493. }
  5494. #endif
  5495. iec_dir_echo(pt,&buf[2],b104); // 报文从 操作标识下一字节开始
  5496. break;
  5497. }
  5498. case 3: //读文件激活
  5499. {
  5500. iec_readfile_echo(pt,&buf[2],b104);
  5501. break;
  5502. }
  5503. case 6: //读文件数据响应确认
  5504. {
  5505. iec_readfile_sure(pt,&buf[2],b104);
  5506. break;
  5507. }
  5508. case 7: //写文件激活
  5509. {
  5510. iec_writefile_echo(pt,&buf[2],b104);
  5511. break;
  5512. }
  5513. case 9: //写文件传输确认
  5514. {
  5515. iec_writefile_sure(pt,&buf[2],b104,framelen);
  5516. break;
  5517. }
  5518. default:ret=-1;
  5519. }
  5520. return ret;
  5521. }
  5522. /*************软件升级报文*****************************/
  5523. int iec_pro_update(void *pt,BYTE cot,BYTE ctype,bool b104)
  5524. {
  5525. BYTE framelen,rcot;
  5526. char *pdat, datbuf[32];
  5527. T_FILE *ptfile;
  5528. bool s_e;
  5529. #ifdef SEC_BOX_CODE
  5530. char md5inf[32];
  5531. int md5lenth=0;
  5532. #endif
  5533. if(b104) ptfile=&((IEC104_DEF *)pt)->tf;
  5534. else ptfile=&((IEC101_DEF *)pt)->tf;
  5535. s_e=(ctype&0x80)?1:0;
  5536. if(cot==IEC_COT_ACT&&s_e) // 升级启动命令
  5537. {
  5538. rcot=IEC_COT_ACTCON;
  5539. ptfile->bUpdateStart=true;
  5540. strcpy(ptfile->updatefilename,"");
  5541. rt_printf("\r\n升级启动");
  5542. g_sec_md5_ret=-1;
  5543. // 组织报文
  5544. pdat=datbuf;
  5545. framelen=0;
  5546. pdat[framelen++]=0; //信息体地址
  5547. pdat[framelen++]=0;
  5548. if(b104)pdat[framelen++]=0;
  5549. pdat[framelen++]=ctype;
  5550. if(b104)
  5551. {
  5552. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,0,rcot,211); //
  5553. }
  5554. else
  5555. {
  5556. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,0,rcot,211,FRAME_I);//
  5557. }
  5558. }
  5559. else if(cot==6&&!s_e) // 升级结束命令
  5560. {
  5561. char file_name[64];
  5562. char log_buf[128]={0};
  5563. rcot=IEC_COT_ACTCON;
  5564. ptfile->bUpdateStart=false;
  5565. // 判断文件真实性,并启动升级处理
  5566. // 组织报文
  5567. pdat=datbuf;
  5568. framelen=0;
  5569. pdat[framelen++]=0; //信息体地址
  5570. pdat[framelen++]=0;
  5571. if(b104)pdat[framelen++]=0;
  5572. pdat[framelen++]=ctype;
  5573. if(b104)
  5574. {
  5575. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,0,rcot,211); //
  5576. }
  5577. else
  5578. {
  5579. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,0,rcot,211,FRAME_I);//
  5580. }
  5581. #ifdef SEC_BOX_CODE
  5582. md5lenth=sec_md5_get_inf(md5inf);
  5583. #endif
  5584. sprintf(file_name,"%s%s","/app/",ptfile->updatefilename);
  5585. rt_printf_time("\r\n程序升级:->%s\r\n",file_name);
  5586. if(strcmp(file_name,KO_FILE_NAME)==0) // 文件名相同
  5587. { int index;
  5588. char from_file_name[64];
  5589. char to_file_name[64];
  5590. char old_file_name[64];
  5591. index=file_check(ptfile->updatefilename,ATTR_DOWN);
  5592. if(index>=0)
  5593. {
  5594. int ret=file_check_info(index) ;
  5595. sprintf(from_file_name,"%s%s","/tmp/",g_file_item[index].file_name);
  5596. if (ret == 0&&(((g_sec_md5_ret==0)&&pRunSet->bTT_ESAM)||!pRunSet->bTT_ESAM))
  5597. {
  5598. watchdog_feed_mainloop_50s();
  5599. sprintf(to_file_name,"%s%s",g_file_item[index].file_dir,g_file_item[index].file_name);
  5600. //sprintf(from_file_name,"%s%s","/tmp/",g_file_item[index].file_name);
  5601. sprintf(old_file_name,"%s%s",to_file_name,"_1");
  5602. rt_file_mv(to_file_name,old_file_name);
  5603. rt_file_mv(from_file_name,to_file_name);
  5604. sprintf(file_name,"%s%s%s","文件更新",g_file_item[index].file_dir,g_file_item[index].file_name);
  5605. log_str_time(LOG_OPERATE,file_name,0,1);
  5606. sprintf(log_buf,"%s updated",g_file_item[index].file_name);
  5607. load_hs_log_rcd(TYPE_SW_UPDATE,true,NULL,log_buf,1);
  5608. rt_printf_time("\r\n%s 程序升级完毕,等待复位\r\n",file_name);
  5609. ptfile->bUpdateReset=true;
  5610. ptfile->us0_updatereset=ustimer_get_origin();
  5611. }
  5612. else
  5613. {
  5614. rt_printf("\r\n%s 升级失败! ret=%d g_sec_md5_ret=%d\r\n",from_file_name,ret,g_sec_md5_ret);
  5615. rt_file_del(from_file_name);
  5616. }
  5617. }
  5618. }
  5619. if(pRunSet->bTT_ESAM&&(g_sec_md5_ret>0)) // md5 检查错误应答 安全信息1f报文
  5620. {
  5621. if(b104)
  5622. {
  5623. ((IEC104_DEF *)pt)->bEchosecmd5=true;
  5624. }
  5625. else
  5626. {
  5627. ((IEC101_DEF *)pt)->bEchosecmd5=true;
  5628. }
  5629. }
  5630. }
  5631. else if(cot==IEC_COT_DEACT&&!s_e) // 升级撤销
  5632. {
  5633. rcot=IEC_COT_DEACTCON;
  5634. ptfile->bUpdateCancel=true;
  5635. ptfile->bUpdateStart=false;
  5636. strcpy(ptfile->updatefilename,"");
  5637. // 组织报文
  5638. pdat=datbuf;
  5639. framelen=0;
  5640. pdat[framelen++]=0; //信息体地址
  5641. pdat[framelen++]=0;
  5642. if(b104)pdat[framelen++]=0;
  5643. pdat[framelen++]=ctype;
  5644. if(b104)
  5645. {
  5646. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,0,rcot,211); //
  5647. }
  5648. else
  5649. {
  5650. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,0,rcot,211,FRAME_I);//
  5651. }
  5652. }
  5653. else
  5654. {
  5655. rcot=IEC_COT_UN_COT;
  5656. // 组织报文
  5657. pdat=datbuf;
  5658. framelen=0;
  5659. pdat[framelen++]=0; //信息体地址
  5660. pdat[framelen++]=0;
  5661. if(b104)pdat[framelen++]=0;
  5662. pdat[framelen++]=ctype;
  5663. if(b104)
  5664. {
  5665. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,0,rcot,211); //
  5666. }
  5667. else
  5668. {
  5669. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,0,rcot,211,FRAME_I);//
  5670. }
  5671. }
  5672. #if 0
  5673. // 组织报文
  5674. pdat=datbuf;
  5675. framelen=0;
  5676. pdat[framelen++]=0; //信息体地址
  5677. pdat[framelen++]=0;
  5678. if(b104)pdat[framelen++]=0;
  5679. pdat[framelen++]=ctype;
  5680. if(b104)
  5681. {
  5682. IEC104_AddFrame((IEC104_DEF *)pt, datbuf,framelen,0,rcot,211); //
  5683. }
  5684. else
  5685. {
  5686. IEC101_Asdu_Add((IEC101_DEF *)pt,datbuf,framelen,0,rcot,211,FRAME_I);//
  5687. }
  5688. #endif
  5689. #ifdef SEC_BOX_CODE
  5690. //扩展报文,回复md5信息
  5691. if(cot==6&&!s_e&&!pRunSet->bTT_ESAM) // 升级结束命令
  5692. {
  5693. rcot=63;
  5694. if(b104)
  5695. {
  5696. IEC104_AddFrame((IEC104_DEF *)pt, md5inf,md5lenth,0,rcot,211); //
  5697. }
  5698. else
  5699. {
  5700. IEC101_Asdu_Add((IEC101_DEF *)pt,md5inf,md5lenth,0,rcot,211,FRAME_I);//
  5701. }
  5702. }
  5703. #endif
  5704. return 0;
  5705. }
  5706. /*******************下面是故障指示板通信处理过程********************/
  5707. static void FaultComm_Recv_Rst(IEC101_DEF *pt)
  5708. {
  5709. pt->nTypeCounter=0;
  5710. pt->nRecvLenth=0;
  5711. pt->nRecvCounter=0;
  5712. }
  5713. static void FaultComm_Send(IEC101_DEF *pt)
  5714. {
  5715. RS_Send_Enable(pt->chnl);
  5716. g_tRsComm[pt->chnl].nSendCnt=1; // 发送缓冲区的第一个字节为发送长度
  5717. g_tRsComm[pt->chnl].bSend=true;
  5718. }
  5719. void FaultComm_Init(IEC101_DEF *pt,BYTE chnl)
  5720. {
  5721. // 接收帧间隔为重发间隔时间的一半
  5722. g_tRsComm[chnl].us_recv_timeout = pRunSet->dT101Resend*USTIMER_SEC/2;
  5723. memset(pt,0,sizeof(IEC101_DEF));
  5724. pt->type=IEC_FAULT_CHN;
  5725. pt->chnl=chnl;
  5726. pt->us0_msend=ustimer_get_origin();
  5727. }
  5728. void FaultComm_Applay(IEC101_DEF *pt)
  5729. {
  5730. BYTE *pd;
  5731. u32 slot;
  5732. // 检查状态板是否配置
  5733. slot = equ_get_slot_by_type(BOARD_TYPE_RS_STATUS);
  5734. if(slot == 0)
  5735. {
  5736. return;
  5737. }
  5738. // 接收数据处理
  5739. if(pt->bData)
  5740. {
  5741. pt->bData=false;
  5742. pd=&pt->recvbuf[4];// 68 L L 68 zone addr data0 data1 sum 16
  5743. if(pd[1]==0x55) //是状态板地址
  5744. {
  5745. if(pd[2]) //有复归按键产生
  5746. {
  5747. ResetHzLed(0);
  5748. SignalReset(0,true); //复归按键处理
  5749. rt_printf("状态指示板复归信号产生。\r\n");
  5750. }
  5751. pt->msend_retries=0;
  5752. pt->bMSend=false;
  5753. if(rt_err_clr(ERR_CODE_SB_COMM,slot) == 2)
  5754. {
  5755. rt_printf("状态板通讯恢复\r\n");
  5756. soe_record_ev(EV_BOARDCOMM, 0, 0,0,0 );
  5757. }
  5758. }
  5759. }
  5760. // 1秒主动更新一次
  5761. if(ustimer_get_duration(pt->us0_msend) > USTIMER_SEC)
  5762. {
  5763. DWORD led=0,ledsw=0,pos,i;
  5764. // 通讯状态监测
  5765. if(pt->bMSend)
  5766. {
  5767. if(++pt->msend_retries>2) //重发三次
  5768. {
  5769. if(rt_err_set(ERR_CODE_SB_COMM,slot) == 2)
  5770. {
  5771. rt_printf("状态板通讯异常\r\n");
  5772. soe_record_ev(EV_BOARDCOMM, 1, 0,0,0 );
  5773. }
  5774. }
  5775. }
  5776. for(i=0;i<g_sw_num;i++)
  5777. {
  5778. // 合位灯
  5779. pos = i*4+0;
  5780. if(g_led_stu[g_led_slot[slot].sn[pos].sw][g_led_slot[slot].sn[pos].no])
  5781. {
  5782. ledsw |= 1<<(i*2+0);
  5783. }
  5784. // 跳位灯
  5785. pos = i*4+1;
  5786. if(g_led_stu[g_led_slot[slot].sn[pos].sw][g_led_slot[slot].sn[pos].no])
  5787. {
  5788. ledsw |= 1<<(i*2+1);
  5789. }
  5790. // 过流灯
  5791. pos = i*4+2;
  5792. if(g_led_stu[g_led_slot[slot].sn[pos].sw][g_led_slot[slot].sn[pos].no])
  5793. {
  5794. led |= 1<<(i*2+0);
  5795. }
  5796. // 接地灯
  5797. pos = i*4+3;
  5798. if(g_led_stu[g_led_slot[slot].sn[pos].sw][g_led_slot[slot].sn[pos].no])
  5799. {
  5800. led |= 1<<(i*2+1);
  5801. }
  5802. }
  5803. //rt_printf("ledsw=%08x,led=%08x.\r\n",ledsw,led);
  5804. pd=g_tRsComm[pt->chnl].sendbuf;
  5805. pd[0]=16; //报文长度
  5806. pd[1]=0x68; //报文头
  5807. pd[2]=10;
  5808. pd[3]=10;
  5809. pd[4]=0x68;
  5810. pd[5]=0; //控制域
  5811. pd[6]=0x55; //链路地址
  5812. pd[7]=(ledsw>>0)&0xff;
  5813. pd[8]=(ledsw>>8)&0xff;
  5814. pd[9]=(ledsw>>16)&0xff;
  5815. pd[10]=(ledsw>>24)&0xff;
  5816. pd[11]=(led>>0)&0xff;
  5817. pd[12]=(led>>8)&0xff;
  5818. pd[13]=(led>>16)&0xff;
  5819. pd[14]=(led>>24)&0xff;
  5820. pd[15]=pd[5]+pd[6]+pd[7]+pd[8]+pd[9]+pd[10]+pd[11]+pd[12]+pd[13]+pd[14];
  5821. pd[16]=0x16;
  5822. FaultComm_Send(pt);
  5823. pt->bMSend=true;
  5824. pt->us0_msend=ustimer_get_origin();
  5825. }
  5826. }
  5827. void FaultComm_Recv(IEC101_DEF *pt,BYTE byRevData)
  5828. {
  5829. pt->recvbuf[pt->nRecvCounter++]=byRevData;
  5830. switch(pt->nTypeCounter)
  5831. {
  5832. case 0:
  5833. if(byRevData==0x68)
  5834. {
  5835. pt->nTypeCounter=1;
  5836. }
  5837. else
  5838. {
  5839. FaultComm_Recv_Rst(pt);
  5840. }
  5841. break;
  5842. case 1:
  5843. pt->nTypeCounter=2;
  5844. pt->nRecvLenth=byRevData; //长帧长度
  5845. if(pt->nRecvLenth>IEC101_RECVBUF_MAX)
  5846. {
  5847. FaultComm_Recv_Rst(pt);
  5848. }
  5849. break;
  5850. case 2:
  5851. if(pt->nRecvLenth==byRevData)
  5852. {
  5853. pt->nTypeCounter=3;
  5854. }
  5855. else
  5856. {
  5857. FaultComm_Recv_Rst(pt);
  5858. }
  5859. break;
  5860. case 3:
  5861. if(byRevData==0x68)
  5862. {
  5863. pt->nTypeCounter=4;
  5864. }
  5865. else
  5866. {
  5867. FaultComm_Recv_Rst(pt);
  5868. }
  5869. break;
  5870. case 4: //控制域
  5871. pt->sum=byRevData;
  5872. pt->nTypeCounter=5;
  5873. break;
  5874. case 5: //目标地址
  5875. pt->sum+=byRevData;
  5876. pt->nRecvCnt=0;
  5877. pt->nTypeCounter=6;
  5878. break;
  5879. case 6: //有效数据 //有效数据
  5880. if(++pt->nRecvCnt>=pt->nRecvLenth-2)
  5881. {
  5882. pt->nTypeCounter=7;
  5883. }
  5884. pt->sum+=byRevData;
  5885. break;
  5886. case 7: //校验和
  5887. if(pt->sum==byRevData)
  5888. {
  5889. pt->nTypeCounter=8;
  5890. }
  5891. else
  5892. {
  5893. FaultComm_Recv_Rst(pt);
  5894. }
  5895. break;
  5896. case 8: //结束符
  5897. FaultComm_Recv_Rst(pt);
  5898. if(byRevData==0x16) //校验结束字符,报文接收完毕,
  5899. {
  5900. pt->bData=true;
  5901. return ;
  5902. }
  5903. break;
  5904. default:
  5905. FaultComm_Recv_Rst(pt);
  5906. break;
  5907. } //类型校验结束
  5908. }