IECComm.c 140 KB

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