IECComm.c 143 KB

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