IECComm.c 122 KB

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