IECComm.c 143 KB

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