IECComm.c 143 KB

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