IECComm.c 145 KB

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