Util.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Text.RegularExpressions;
  6. using System.Collections;
  7. using System.Diagnostics;
  8. using System.Data;
  9. using System.IO;
  10. using Microsoft.Win32;
  11. namespace FPER
  12. {
  13. public class Util
  14. {
  15. /****************************************************************
  16. * String --> Int, 공백이나 NULL 인경우. 기본값으로 리턴한다.
  17. ****************************************************************/
  18. public static int StrToInt(object txt, int val)
  19. {
  20. int ret_val = val;
  21. try
  22. {
  23. if (txt == null) return val;
  24. if (txt.ToString().Trim() == "") return val;
  25. ret_val = int.Parse(txt.ToString());
  26. }
  27. catch (Exception ex)
  28. {
  29. Util.UErrorMessage(ex, 0, 0);
  30. }
  31. finally
  32. {
  33. }
  34. return ret_val;
  35. }
  36. /****************************************************************
  37. * 공백이나 NULL 인경우. 기본값으로 리턴한다.
  38. ****************************************************************/
  39. public static string NullToStr(object txt)
  40. {
  41. string ret_val = "";
  42. try
  43. {
  44. if (txt == null) return ret_val;
  45. if (txt.ToString().Trim() == "") return ret_val;
  46. ret_val = txt.ToString().Trim();
  47. }
  48. catch (Exception ex)
  49. {
  50. Util.UErrorMessage(ex, 0, 0);
  51. }
  52. finally
  53. {
  54. }
  55. return ret_val;
  56. }
  57. /*******************************************
  58. * control 숫자형 입력값 검사.
  59. *******************************************/
  60. public static Boolean ChkOnlyLiteralTextBox(TextBox txt, String txtboxName)
  61. {
  62. try
  63. {
  64. String input_value = txt.Text;
  65. if (!onlyLiteral(input_value))
  66. {
  67. txt.Text = "";
  68. txt.Focus();
  69. throw new Exception(String.Format("[{0}] 입력형식이 올바르지 않습니다. 숫자를 입력하여 주십시요.", txtboxName));
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. Util.UErrorMessage(ex, 0, 0);
  75. }
  76. return true;
  77. }
  78. /*******************************************
  79. * TextBox 필수 입력 검사.
  80. *******************************************/
  81. public static Boolean ChkTxtBox(TextBox txt, String txtboxName)
  82. {
  83. try
  84. {
  85. if (txt.Text.Trim() == "")
  86. {
  87. txt.Focus();
  88. throw new Exception(String.Format("[{0}] 입력값은 필수입니다.", txtboxName));
  89. }
  90. return true;
  91. }
  92. catch (Exception ex)
  93. {
  94. Util.UErrorMessage(ex, 0, 0);
  95. throw ex;
  96. }
  97. }
  98. /*******************************************
  99. * MaskedTextBox 필수 입력 검사.
  100. *******************************************/
  101. public static Boolean ChkTxtBox(MaskedTextBox txt, String txtboxName)
  102. {
  103. try
  104. {
  105. if (txt.Text.Trim() == "")
  106. {
  107. txt.Focus();
  108. throw new Exception(String.Format("[{0}] 입력값은 필수입니다.", txtboxName));
  109. }
  110. return true;
  111. }
  112. catch (Exception ex)
  113. {
  114. Util.UErrorMessage(ex, 0, 0);
  115. throw ex;
  116. }
  117. }
  118. /*******************************************
  119. * ComboBox 필수 입력 검사.
  120. *******************************************/
  121. public static Boolean ChkComboBox(ComboBox cbo, String cboName)
  122. {
  123. try
  124. {
  125. if (cbo.SelectedValue == null)
  126. {
  127. throw new Exception(String.Format("[{0}] 값을 선택하여 주십시요.", cboName));
  128. }
  129. return true;
  130. }
  131. catch (Exception ex)
  132. {
  133. Util.UErrorMessage(ex, 0, 0);
  134. throw ex;
  135. }
  136. }
  137. /**************************************************************************************
  138. * GroupBox 컨트롤 상태 변경하기
  139. * 수정,입력모드
  140. * 입력불가모드
  141. **************************************************************************************/
  142. public static void GroupUIChange(Control gb, UIMode mode)
  143. {
  144. try
  145. {
  146. /** 화면 입력,수정 모드 **/
  147. if (mode == UIMode.input)
  148. {
  149. foreach (Control currentControl in gb.Controls)
  150. {
  151. if (currentControl is TextBox)
  152. {
  153. TextBox txt = (TextBox)currentControl;
  154. //txt.Enabled = true;
  155. txt.ReadOnly = false;
  156. }
  157. else if (currentControl is MaskedTextBox)
  158. {
  159. MaskedTextBox msktxt = (MaskedTextBox)currentControl;
  160. //msktxt.Enabled = true;
  161. msktxt.ReadOnly = false;
  162. }
  163. else if (currentControl is ComboBox)
  164. {
  165. ComboBox cbo = (ComboBox)currentControl;
  166. cbo.Enabled = true;
  167. }
  168. else if (currentControl is CheckBox)
  169. {
  170. CheckBox chk = (CheckBox)currentControl;
  171. chk.Enabled = true;
  172. }
  173. else if (currentControl is Button)
  174. {
  175. Button btn = (Button)currentControl;
  176. btn.Visible = true;
  177. }
  178. else if (currentControl is TabControl)
  179. {
  180. TabControl tc = (TabControl)currentControl;
  181. GroupUIChange(tc, mode);
  182. }
  183. else if (currentControl is TabPage)
  184. {
  185. TabPage pg = (TabPage)currentControl;
  186. GroupUIChange(pg, mode);
  187. }
  188. }
  189. }
  190. /** 화면 입력,수정 모드 여기까지**/
  191. /** 화면 VIEW 모드 **/
  192. else
  193. {
  194. foreach (Control currentControl in gb.Controls)
  195. {
  196. if (currentControl is TextBox)
  197. {
  198. TextBox txt = (TextBox)currentControl;
  199. //txt.Enabled = false;
  200. txt.ReadOnly = true;
  201. }
  202. else if (currentControl is MaskedTextBox)
  203. {
  204. MaskedTextBox msktxt = (MaskedTextBox)currentControl;
  205. //msktxt.Enabled = false;
  206. msktxt.ReadOnly = true;
  207. }
  208. else if (currentControl is ComboBox)
  209. {
  210. ComboBox cbo = (ComboBox)currentControl;
  211. cbo.Enabled = false;
  212. cbo.ForeColor = System.Drawing.Color.Black;
  213. }
  214. else if (currentControl is CheckBox)
  215. {
  216. CheckBox btn = (CheckBox)currentControl;
  217. btn.Enabled = false;
  218. btn.ForeColor = System.Drawing.Color.Black;
  219. }
  220. else if (currentControl is Button)
  221. {
  222. Button btn = (Button)currentControl;
  223. btn.Visible = false;
  224. }
  225. else if (currentControl is TabControl)
  226. {
  227. TabControl tc = (TabControl)currentControl;
  228. GroupUIChange(tc, mode);
  229. }
  230. else if (currentControl is TabPage)
  231. {
  232. TabPage pg = (TabPage)currentControl;
  233. GroupUIChange(pg, mode);
  234. }
  235. }
  236. }
  237. /** 화면 VIEW 모드 여기까지 **/
  238. }
  239. catch (Exception ex)
  240. {
  241. Util.UErrorMessage(ex, 0, 0);
  242. }
  243. }
  244. /**************************************************************************************
  245. * GroupBox 컨트롤 상태 변경하기
  246. * 수정,입력모드
  247. * 입력불가모드
  248. **************************************************************************************/
  249. public static void GroupControlChange(Control gb, UIMode mode)
  250. {
  251. try
  252. {
  253. /** 화면 입력,수정 모드 **/
  254. if (mode == UIMode.input)
  255. {
  256. foreach (Control currentControl in gb.Controls)
  257. {
  258. if (currentControl is TextBox)
  259. {
  260. TextBox txt = (TextBox)currentControl;
  261. //txt.Enabled = true;
  262. txt.ReadOnly = false;
  263. }
  264. else if (currentControl is MaskedTextBox)
  265. {
  266. MaskedTextBox msktxt = (MaskedTextBox)currentControl;
  267. //msktxt.Enabled = true;
  268. msktxt.ReadOnly = false;
  269. }
  270. else if (currentControl is ComboBox)
  271. {
  272. ComboBox cbo = (ComboBox)currentControl;
  273. cbo.Enabled = true;
  274. }
  275. else if (currentControl is CheckBox)
  276. {
  277. CheckBox chk = (CheckBox)currentControl;
  278. chk.Enabled = true;
  279. }
  280. else if (currentControl is Button)
  281. {
  282. Button btn = (Button)currentControl;
  283. btn.Enabled = true;
  284. }
  285. else if (currentControl is TabControl)
  286. {
  287. TabControl tc = (TabControl)currentControl;
  288. GroupControlChange(tc, mode);
  289. }
  290. else if (currentControl is TabPage)
  291. {
  292. TabPage pg = (TabPage)currentControl;
  293. GroupControlChange(pg, mode);
  294. }
  295. }
  296. }
  297. /** 화면 입력,수정 모드 여기까지**/
  298. /** 화면 VIEW 모드 **/
  299. else
  300. {
  301. foreach (Control currentControl in gb.Controls)
  302. {
  303. if (currentControl is TextBox)
  304. {
  305. TextBox txt = (TextBox)currentControl;
  306. //txt.Enabled = false;
  307. txt.ReadOnly = true;
  308. }
  309. else if (currentControl is MaskedTextBox)
  310. {
  311. MaskedTextBox msktxt = (MaskedTextBox)currentControl;
  312. //msktxt.Enabled = false;
  313. msktxt.ReadOnly = true;
  314. }
  315. else if (currentControl is ComboBox)
  316. {
  317. ComboBox cbo = (ComboBox)currentControl;
  318. cbo.Enabled = false;
  319. cbo.ForeColor = System.Drawing.Color.Black;
  320. }
  321. else if (currentControl is CheckBox)
  322. {
  323. CheckBox btn = (CheckBox)currentControl;
  324. btn.Enabled = false;
  325. btn.ForeColor = System.Drawing.Color.Black;
  326. }
  327. else if (currentControl is Button)
  328. {
  329. Button btn = (Button)currentControl;
  330. btn.Enabled = false;
  331. }
  332. else if (currentControl is TabControl)
  333. {
  334. TabControl tc = (TabControl)currentControl;
  335. GroupControlChange(tc, mode);
  336. }
  337. else if (currentControl is TabPage)
  338. {
  339. TabPage pg = (TabPage)currentControl;
  340. GroupControlChange(pg, mode);
  341. }
  342. }
  343. }
  344. /** 화면 VIEW 모드 여기까지 **/
  345. }
  346. catch (Exception ex)
  347. {
  348. Util.UErrorMessage(ex, 0, 0);
  349. }
  350. }
  351. /*******************************************
  352. * 우편번호조합 검사.
  353. *******************************************/
  354. public static Boolean chkZipCode(String intxt)
  355. {
  356. String ZipRegex = "[0-9]{3}-[0-9]{3}";
  357. if (Regex.IsMatch(intxt, ZipRegex))
  358. {
  359. return true;
  360. }
  361. else
  362. {
  363. return false;
  364. }
  365. }
  366. /*******************************************
  367. * 숫자조합 검사.
  368. *******************************************/
  369. public static Boolean onlyLiteral(String intxt)
  370. {
  371. String patttern = "^[0-9]+$"; //정수:0-9숫자 실수:^[+-]?\d+(\.\d+)?$ 또는 ^[+-]?\d*\.?\d*$
  372. if (Regex.IsMatch(intxt, patttern))
  373. {
  374. return true;
  375. }
  376. else
  377. {
  378. return false;
  379. }
  380. }
  381. /*******************************************
  382. * 숫자영문조합 검사.
  383. *******************************************/
  384. public static Boolean onlyLiteralnAlpa(String intxt)
  385. {
  386. String patttern = "\\w+"; //[a-zA-Z0-9_] 영문숫자_
  387. if (Regex.IsMatch(intxt, patttern))
  388. {
  389. return true;
  390. }
  391. else
  392. {
  393. return false;
  394. }
  395. }
  396. /*******************************************
  397. * 사용함/사용안함 ArrayList 생성
  398. *******************************************/
  399. public static ArrayList useFlagArray()
  400. {
  401. ArrayList ary = new ArrayList();
  402. ary.Add(new cboitem("Y", "사용함"));
  403. ary.Add(new cboitem("N", "사용안함"));
  404. return ary;
  405. }
  406. /*******************************************
  407. * ComInfo 응답 에러체크
  408. *******************************************/
  409. public static bool ComInfoErrProcess(CmdInfo cmd, bool connected)
  410. {
  411. bool CompleteOK = true;
  412. try
  413. {
  414. if (cmd != null)
  415. {
  416. if (cmd.ErrResponse)
  417. {
  418. if (connected)
  419. {
  420. RCVData_NACK nack = (RCVData_NACK)cmd.ErrResponseData;
  421. }
  422. CompleteOK = false;
  423. }
  424. else if (cmd.ResponseData == null)
  425. {
  426. CompleteOK = false;
  427. }
  428. }
  429. }
  430. catch (Exception ex)
  431. {
  432. Util.UErrorMessage(ex, 0, 0);
  433. CompleteOK = false;
  434. }
  435. return CompleteOK;
  436. }
  437. // cyim 2015.7.23 NACK 처리 부분 오류
  438. // NACK 즉, 응답을 하지 않는 경우에 한해서 처리를 해야되는데도 불구하고, 다른 명령에 대한 응답이 들어오면
  439. // 무조건 예외처리를 해버리는 것이 문제가 된다. 제대로 처리하려면 NACK 외 어떤 명령이 왔는지 파라미터로 전달하여 오버로딩 처리해야된다
  440. public static bool ComInfoErrProcess(CmdInfo cmd, bool connected, string CmdType)
  441. {
  442. bool CompleteOK = true;
  443. try
  444. {
  445. if (cmd != null)
  446. {
  447. if (cmd.ErrResponseData.GetType().Name == CmdType)
  448. {
  449. ;// CmdType 이 제대로 맞았다면 이는 오류가 아니다
  450. }
  451. else if (cmd.ErrResponse)
  452. {
  453. if (connected)
  454. {
  455. RCVData_NACK nack = (RCVData_NACK)cmd.ErrResponseData;
  456. }
  457. CompleteOK = false;
  458. }
  459. else if (cmd.ResponseData == null)
  460. {
  461. CompleteOK = false;
  462. }
  463. }
  464. }
  465. catch (Exception ex)
  466. {
  467. Util.UErrorMessage(ex, 0, 0);
  468. CompleteOK = false;
  469. }
  470. return CompleteOK;
  471. }
  472. /*******************************************
  473. * COMPORT ArrayList 생성
  474. *******************************************/
  475. public static ArrayList ArrayComport()
  476. {
  477. ////레지스트리에서 수신기ID정보 읽어오기 -- >변경 데몬과 동일한 PC가 아닐수 있으므로..
  478. //RegistryKey rk = Registry.LocalMachine.OpenSubKey("HARDWARE\\DEVICEMAP\\SERIALCOMM", false);
  479. //if (rk != null)
  480. //{
  481. // String[] keys = rk.GetValueNames();
  482. // for (int i = 0; i < keys.Length; i++)
  483. // {
  484. // String value = rk.GetValue(keys[i]).ToString();
  485. // int no = int.Parse(value.Replace("COM", ""));
  486. // if (no > 9)
  487. // comport.Add(new cboitem(value, value));
  488. // else
  489. // comport.Add(new cboitem(value, value));
  490. // }
  491. //}
  492. ArrayList comport = new ArrayList();
  493. // cyim 2013.7.10 디자인개선작업 : 수신기설정 - 통신설정 COM1~COM8 로 범위를 축소 : 20 -> 8
  494. for (int i = 1; i <= 8; i++)
  495. {
  496. String value = "COM" + i.ToString();
  497. comport.Add(new cboitem(value, value));
  498. }
  499. return comport;
  500. }
  501. /*******************************************
  502. * 속도 ArrayList 생성
  503. *******************************************/
  504. public static ArrayList ArraySpeed()
  505. {
  506. ArrayList speed = new ArrayList();
  507. //speed.Add(new cboitem("110","110"));
  508. //speed.Add(new cboitem("300","300"));
  509. //speed.Add(new cboitem("600","600"));
  510. //speed.Add(new cboitem("1200","1200"));
  511. speed.Add(new cboitem("2400", "2400"));
  512. speed.Add(new cboitem("4800", "4800"));
  513. speed.Add(new cboitem("9600", "9600"));
  514. speed.Add(new cboitem("14400", "14400"));
  515. speed.Add(new cboitem("19200", "19200"));
  516. speed.Add(new cboitem("28800", "28800"));
  517. speed.Add(new cboitem("38400", "38400"));
  518. speed.Add(new cboitem("57600", "57600"));
  519. speed.Add(new cboitem("115200", "115200")); //기본
  520. //speed.Add(new cboitem("128000", "128000"));
  521. //speed.Add(new cboitem("256000", "256000"));
  522. return speed;
  523. }
  524. /*******************************************
  525. * DATABIT ArrayList 생성
  526. *******************************************/
  527. public static ArrayList ArrayDatabit()
  528. {
  529. ArrayList databit = new ArrayList();
  530. //databit.Add(new cboitem("4", "4"));
  531. //databit.Add(new cboitem("5", "5"));
  532. //databit.Add(new cboitem("6", "6"));
  533. databit.Add(new cboitem("7", "7")); //기본
  534. databit.Add(new cboitem("8", "8"));
  535. return databit;
  536. }
  537. /*******************************************
  538. * PARITY ArrayList 생성
  539. *******************************************/
  540. public static ArrayList ArrayParity()
  541. {
  542. ArrayList parity = new ArrayList();
  543. parity.Add(new cboitem("E", "E 짝수"));
  544. //parity.Add(new cboitem("M", "M 표시"));
  545. parity.Add(new cboitem("N", "N 없음")); //기본
  546. parity.Add(new cboitem("O", "O 홀수"));
  547. //parity.Add(new cboitem("S", "S 공간"));
  548. return parity;
  549. }
  550. /*******************************************
  551. * STOPBIT ArrayList 생성
  552. *******************************************/
  553. public static ArrayList ArrayStopbit()
  554. {
  555. ArrayList stopbit = new ArrayList();
  556. stopbit.Add(new cboitem("1", "1")); //기본
  557. //stopbit.Add(new cboitem("2", "1.5"));
  558. stopbit.Add(new cboitem("2", "2"));
  559. return stopbit;
  560. }
  561. /*******************************************
  562. * Baud Rate ArrayList 생성
  563. *******************************************/
  564. public static ArrayList baudRateArray()
  565. {
  566. ArrayList ary = new ArrayList();
  567. ary.Add(new cboitem("0", "2400"));
  568. ary.Add(new cboitem("1", "4800"));
  569. ary.Add(new cboitem("2", "9600"));
  570. ary.Add(new cboitem("3", "19200"));
  571. ary.Add(new cboitem("4", "38400"));
  572. ary.Add(new cboitem("5", "57600"));
  573. ary.Add(new cboitem("6", "76800"));
  574. ary.Add(new cboitem("7", "115200"));
  575. return ary;
  576. }
  577. /*******************************************
  578. * Baud Code --> Baud Rate 리턴
  579. *******************************************/
  580. public static String baudCodeToRate(String code)
  581. {
  582. String baud = null;
  583. switch (code)
  584. {
  585. case "0": baud = "2400"; break;
  586. case "1": baud = "4800"; break;
  587. case "2": baud = "9600"; break;
  588. case "3": baud = "19200"; break;
  589. case "4": baud = "38400"; break;
  590. case "5": baud = "57600"; break;
  591. case "6": baud = "76800"; break;
  592. case "7": baud = "115200"; break;
  593. }
  594. return baud;
  595. }
  596. /*******************************************
  597. * Baud Code --> Baud Rate 리턴
  598. *******************************************/
  599. public static String baudRateToCode(String rate)
  600. {
  601. String code = null;
  602. switch (rate)
  603. {
  604. case "2400": code = "0"; break;
  605. case "4800": code = "1"; break;
  606. case "9600": code = "2"; break;
  607. case "19200": code = "3"; break;
  608. case "38400": code = "4"; break;
  609. case "57600": code = "5"; break;
  610. case "76800": code = "6"; break;
  611. case "115200": code = "7"; break;
  612. }
  613. return code;
  614. }
  615. /*******************************************
  616. * 콤보박스에 ArrayList 셋팅
  617. *******************************************/
  618. public static void ComboSetting(ComboBox co, ArrayList ary, String defaultvalue)
  619. {
  620. try
  621. {
  622. if (ary == null) { co.DataSource = null; return; }
  623. if (co == null) return;
  624. //co.Items.Clear();
  625. co.DataSource = ary;
  626. co.DisplayMember = "text";
  627. co.ValueMember = "value";
  628. if (defaultvalue != null) co.SelectedValue = defaultvalue;
  629. //if (defaultvalue == null)
  630. // co.SelectedIndex = -1;
  631. //else
  632. // co.SelectedValue = defaultvalue;
  633. }
  634. catch (Exception ex)
  635. {
  636. Util.UErrorMessage(ex, 0, 0);
  637. //Debug.WriteLine(ex.Message);
  638. }
  639. }
  640. /*******************************************
  641. * 컨트롤 이름으로 검색
  642. *******************************************/
  643. public static Control FineControl(Control parent, String ContronlName)
  644. {
  645. //Control con = null;
  646. //foreach (Control currentControl in parent.Controls)
  647. //{
  648. // String cName = currentControl.Name; //btnRepeater1
  649. // if (cName.Equals(ContronlName))
  650. // {
  651. // con = currentControl;
  652. // }
  653. //}
  654. Control con = null;
  655. if (parent == null) return null;
  656. Control[] controls = parent.Controls.Find(ContronlName, true);
  657. if (controls.Length > 0)
  658. {
  659. con = controls[0];
  660. }
  661. return con;
  662. }
  663. /*******************************************
  664. * chars -> String으로 변환
  665. *******************************************/
  666. public static String CharToStr(char[] chars)
  667. {
  668. String s = "";
  669. foreach (Char c in chars)
  670. {
  671. s += string.Format("{0}", c);
  672. }
  673. return s;
  674. }
  675. /*******************************************
  676. * bytes -> String으로 변환
  677. *******************************************/
  678. public static String ByteToStr(byte[] bytes, int start_idx, int length_idx)
  679. {
  680. ASCIIEncoding ascii = new ASCIIEncoding();
  681. //회로 I/O타입
  682. int charCount = ascii.GetCharCount(bytes, start_idx, length_idx);
  683. Char[] iochar = new Char[charCount];
  684. int charsDecodedCount = ascii.GetChars(bytes, start_idx, length_idx, iochar, 0);
  685. return CharToStr(iochar);
  686. }
  687. /*******************************************
  688. * byte -> hex 스트링으로 변환
  689. *******************************************/
  690. public static String ToHex(byte[] bin_data)
  691. {
  692. string result = "";
  693. foreach (byte ch in bin_data)
  694. {
  695. result += string.Format("{0:x2} ", ch); // 2자리의 16진수로 출력, [참고] 링크 읽어볼 것
  696. }
  697. return result;
  698. }
  699. /*******************************************
  700. * 스트링을 byte 배열로 변환
  701. *******************************************/
  702. public static byte[] ToBytes(String arg)
  703. {
  704. string arg2 = NullToStr(arg);
  705. System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  706. return encoding.GetBytes(arg2);
  707. }
  708. /*******************************************
  709. * byte + byte 배열로 변환
  710. *******************************************/
  711. public static byte[] ByteArrayCopyAdd(byte[] srcByte, byte[] addByte, int addLen)
  712. {
  713. int totalLen = srcByte.Length + addLen;
  714. byte[] totalByte = new byte[totalLen];
  715. try
  716. {
  717. Array.Copy(srcByte, 0, totalByte, 0, srcByte.Length);
  718. Array.Copy(addByte, 0, totalByte, srcByte.Length, addLen);
  719. }
  720. catch (Exception ex)
  721. {
  722. Util.UErrorMessage(ex, 0, 0);
  723. throw ex;
  724. //Debug.WriteLine(ex.Message);
  725. }
  726. return totalByte;
  727. }
  728. /*******************************************
  729. * Y,N String --> Bool로 변환
  730. *******************************************/
  731. public static bool StringToBool(String flg)
  732. {
  733. return flg == "Y" ? true : false;
  734. }
  735. /*******************************************
  736. * 2010-1-1 이후 현재까지 간격
  737. *******************************************/
  738. public static double milisec()
  739. {
  740. DateTime stdDate = new DateTime(2010, 1, 1, 0, 0, 0);
  741. TimeSpan ts = DateTime.Now - stdDate;
  742. double intRtn = ts.TotalMilliseconds;
  743. return intRtn;
  744. }
  745. /*******************************************
  746. * 하위,상위 숫자로 float 볼트리턴
  747. *******************************************/
  748. public static float ToValtage(int vol_dn, int vol_up)
  749. {
  750. float voltage24 = 0;
  751. //24V (상위*256 + 하위) / 100
  752. voltage24 = (vol_up * 256) + vol_dn;
  753. voltage24 = voltage24 / 100;
  754. return voltage24;
  755. }
  756. /*******************************************
  757. * TextBox 입력을 대문자로 변경해줌
  758. *******************************************/
  759. public static void ToUpper(TextBox obj)
  760. {
  761. try
  762. {
  763. int caretPosition;
  764. caretPosition = obj.SelectionStart;
  765. obj.Text = obj.Text.ToUpper();
  766. obj.Select(caretPosition, 0);
  767. }
  768. catch (Exception ex)
  769. {
  770. Util.UErrorMessage(ex, 0, 0);
  771. }
  772. }
  773. public static void LogWrite(string logstr)
  774. {
  775. try
  776. {
  777. //Util.UDebugMessage("LogWrite " + logstr, 0, 0);
  778. }
  779. catch (Exception ex)
  780. {
  781. Util.UErrorMessage(ex, 0, 0);
  782. }
  783. // try
  784. // {
  785. //
  786. // string fileName = string.Format("FTER_{0:yyyyMMdd}.txt", DateTime.Now);
  787. // string dirPath = string.Format("{0}\\Logs",Application.StartupPath);
  788. //
  789. // if (!System.IO.Directory.Exists(dirPath)) System.IO.Directory.CreateDirectory(dirPath);
  790. //
  791. // string fullFileName = string.Format("{0}\\{1}", dirPath, fileName);
  792. //
  793. // FileStream fs = new FileStream(fullFileName, FileMode.Append, FileAccess.Write, FileShare.Read);
  794. // StreamWriter sw = new StreamWriter(fs);
  795. // sw.WriteLine(string.Format("{0:yyyy-MM-dd HH:mm:ss} : {1}", DateTime.Now, logstr)); //내용쓰고
  796. // sw.Flush();
  797. // sw.Close();
  798. //
  799. // }
  800. // catch (Exception ex)
  801. // {
  802. // Util.UErrorMessage(ex,0,0);
  803. // //Debug.WriteLine(ex.StackTrace);
  804. // }
  805. }
  806. public static void LogBineryWrite(byte[] logstr, int offset)
  807. {
  808. try
  809. {
  810. string fileName = string.Format("FTER_PACKET_{0:yyyyMMdd}.dat", DateTime.Now);
  811. string dirPath = string.Format("{0}\\Logs", Application.StartupPath);
  812. if (!System.IO.Directory.Exists(dirPath)) System.IO.Directory.CreateDirectory(dirPath);
  813. byte[] ReadBuffer = new byte[offset];
  814. Array.Copy(logstr, ReadBuffer, offset);
  815. string fullFileName = string.Format("{0}\\{1}", dirPath, fileName);
  816. FileStream fs = new FileStream(fullFileName, FileMode.Append, FileAccess.Write);
  817. BinaryWriter bw = new BinaryWriter(fs);
  818. bw.Write(ReadBuffer);
  819. bw.Close();
  820. fs.Close();
  821. }
  822. catch (Exception ex)
  823. {
  824. Util.UErrorMessage(ex, 0, 0);
  825. // Debug.WriteLine(ex.StackTrace);
  826. }
  827. }
  828. //회로 표현 메세지
  829. public static string device_message(string inout_type, int receiver_id, int comm_id, int board_id, int loop_no, int repeater_id, int device_id)
  830. {
  831. string message = "";
  832. try
  833. {
  834. switch (inout_type)
  835. {
  836. case "I":
  837. case "O":
  838. {
  839. DacUIProcess dacUIProcess = new DacUIProcess(receiver_id); // cyim 2015.7.30 데이타베이스 접속 루틴 변경
  840. MskDeviceIDString device_str = new MskDeviceIDString(comm_id, board_id, loop_no, repeater_id, device_id, inout_type);
  841. string device_name = "";
  842. string position_name = "";
  843. string device_type_name = "";
  844. DataTable dt = dacUIProcess.Device_Select(receiver_id, comm_id, board_id, loop_no, repeater_id, device_id, inout_type);
  845. if (dt.Rows.Count > 0)
  846. {
  847. DataRow dr = dt.Rows[0];
  848. device_name = Util.NullToStr(dr["DEVICE_NAME"]);
  849. position_name = Util.NullToStr(dr["POSITION_NAME"]);
  850. device_type_name = Util.NullToStr(dr["DEVICE_TYPE_NAME"]);
  851. }
  852. string inoutName = inout_type.Equals(code_InOutType.Output) ? "출력" : "입력";
  853. if (comm_id == 1 || comm_id == 3)
  854. {
  855. //message = string.Format("[{0}]-[{1}]-[{2}]-[{3}]"
  856. // , device_str.MskId, device_name, position_name, device_type_name);
  857. //message = string.Format("{3}\r\n{2} {1}\r\n[{0}]"
  858. // , device_str.MskId, device_name, position_name, device_type_name);
  859. message = string.Format("{0} {1} [{2}]", position_name, device_name, device_str.MskId);
  860. }
  861. else if (comm_id == 4)
  862. {
  863. message = string.Format("키패드[{0}]", device_name);
  864. }
  865. break;
  866. }
  867. case "D":
  868. {
  869. if (comm_id == 1)
  870. message = string.Format("통신보드[{0}] LOOP번호[{1}] 중계기번호[{2}] 회로번호[{3}]"
  871. , board_id, loop_no, repeater_id, device_id);
  872. else if (comm_id == 3)
  873. message = string.Format("I/O보드[{0}] 회로번호[{1}]", board_id, device_id);
  874. else if (comm_id == 4)
  875. message = string.Format("키패드번호[{0}] ", device_id);
  876. break;
  877. }
  878. case "R":
  879. {
  880. if (comm_id == 1)
  881. message = string.Format("통신보드[{0}] LOOP번호[{1}] 중계기번호[{2}]"
  882. , board_id, loop_no, repeater_id);
  883. break;
  884. }
  885. case "L":
  886. {
  887. if (comm_id == 1) message = string.Format("통신보드[{0}] LOOP번호[{1}] ", board_id, loop_no);
  888. else if (comm_id == 2) message = string.Format("BACKLOOP 통신보드[{0}] LOOP번호[{1}] ", board_id, loop_no);
  889. else if (comm_id == 3) message = "IO보드";
  890. else if (comm_id == 4) message = "키패드";
  891. break;
  892. }
  893. case "B":
  894. {
  895. if (comm_id == 1)
  896. message = string.Format("통신보드[{0}] ", board_id);
  897. else if (comm_id == 3)
  898. message = string.Format("I/O보드[{0}] ", board_id);
  899. else if (comm_id == 4)
  900. message = string.Format("키패드[{0}] ", board_id);
  901. break;
  902. }
  903. case "C":
  904. case "A":
  905. {
  906. if (comm_id == 1)
  907. message = "Front";
  908. else if (comm_id == 2)
  909. message = "Back";
  910. else if (comm_id == 3)
  911. message = "I/O보드";
  912. else if (comm_id == 4)
  913. message = "키패드";
  914. else
  915. message = "수신기";
  916. break;
  917. }
  918. }
  919. }
  920. catch (Exception ex)
  921. {
  922. message = "";//2010.11.11,k.s.d, db access bug fix.
  923. Util.UErrorMessage(ex, 0, 0);
  924. }
  925. return message;
  926. }
  927. // cyim 2015.7.29 수신반 루틴을 위해 레지스트리값 이용은 최소화 : 프로그램 시작시에 이미 읽어들임
  928. //public static string FirebirdPath()
  929. //{
  930. // String filePath = "";
  931. // try
  932. // {
  933. // //레지스트리에서 DB접속정보 읽어오기
  934. // RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\I_FPER_COMM_DAEMON", false);
  935. // if (rk != null)
  936. // {
  937. // String DATABASE_NAME = rk.GetValue("DATABASE_NAME").ToString();
  938. // int pos = DATABASE_NAME.IndexOf(":");
  939. // if (pos != -1)
  940. // {
  941. // filePath = DATABASE_NAME.Substring(pos + 1);
  942. // }
  943. // }
  944. // }
  945. // catch (Exception ex)
  946. // {
  947. // Util.UErrorMessage(ex, 0, 0);
  948. // }
  949. // return filePath;
  950. //}
  951. public static DateTime getBackUpLastDateTime()
  952. {
  953. DateTime backup_dt = DateTime.Parse("1999-01-01");
  954. try
  955. {
  956. //레지스트리에서 DB접속정보 읽어오기
  957. RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\I_FPER_COMM_DAEMON", false);
  958. if (rk != null)
  959. {
  960. String backup_dt_str = rk.GetValue("BACKUP_DATETIME").ToString();
  961. if (backup_dt_str.Length == 19)
  962. {
  963. backup_dt = DateTime.Parse(backup_dt_str);
  964. }
  965. }
  966. }
  967. catch (Exception ex)
  968. {
  969. Util.UErrorMessage(ex, 0, 0);
  970. }
  971. return backup_dt;
  972. }
  973. public static void setBackUpLastDateTime(DateTime backup_dt)
  974. {
  975. try
  976. {
  977. //레지스트리에서 DB접속정보 읽어오기
  978. RegistryKey rk = Registry.LocalMachine.CreateSubKey("SOFTWARE").CreateSubKey("I_FPER_COMM_DAEMON");
  979. if (rk != null)
  980. {
  981. rk.SetValue("BACKUP_DATETIME", string.Format("{0:yyyy-MM-dd HH:mm:ss}", backup_dt));
  982. }
  983. }
  984. catch (Exception ex)
  985. {
  986. Util.UErrorMessage(ex, 0, 0);
  987. }
  988. }
  989. //2010.11.08, k.s.d , Debug message Function
  990. //static int prt_dbg_level = -1;// dbg message level
  991. public static void UDebugMessage(string msg, int dbg_category, int dbg_level)
  992. {// dbg message
  993. //if(dbg_level <= prt_dbg_level) {
  994. // Debug.WriteLine("[FPER]: "+msg);
  995. //}
  996. ;
  997. }
  998. public static void UDebugMessage(string msg, int dbg_category, int dbg_level, StackTrace st)
  999. {// error message
  1000. //if(dbg_level <= prt_dbg_level) {
  1001. // Debug.WriteLine(string.Format("[FPER][{0}]: {1} //+ {2} -//",dbg_category,msg,st.ToString()));
  1002. //}
  1003. ;
  1004. }
  1005. public static void UErrorMessage(Exception ex, int dbg_category, int dbg_level)
  1006. { // error message with stack
  1007. //if(dbg_level <= prt_dbg_level) {
  1008. // Debug.WriteLine(string.Format("[FPER]:[ERR]: {0}, //+ {1} -// ", ex.Message, ex.StackTrace));
  1009. //}
  1010. ;
  1011. }
  1012. }
  1013. }