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