IECComm.c 160 KB

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