IECComm.c 143 KB

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