IECComm.c 143 KB

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