SweepCode.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Common;
  9. namespace MeterPlugInLibrary
  10. {
  11. public class SweepCode
  12. {
  13. private string strCode = "";//扫码信息
  14. private bool isError = false;//扫码信息
  15. public string StrCode
  16. {
  17. get { return strCode; }
  18. set { strCode = value; }
  19. }
  20. private string strState = "";//状态 0重量稳定 1重量不稳定 2空磅
  21. public string StrState
  22. {
  23. get { return strState; }
  24. set { strState = value; }
  25. }
  26. private object obj = new object();
  27. private Thread mThread = null;
  28. public void StartThreadSweep()
  29. {
  30. mThread = new Thread(new ThreadStart(ThreadSweepInfo));
  31. mThread.Start();
  32. }
  33. /// <summary>
  34. /// 扫码信息读取
  35. /// </summary>
  36. /// <returns></returns>
  37. private void ThreadSweepInfo()
  38. {
  39. try
  40. {
  41. while (true)
  42. {
  43. lock (obj)
  44. {
  45. string sCode = "";
  46. Thread.Sleep(500);
  47. //WriteLog("开始扫码线程:重量状态为:" + strState);
  48. if (strState.Equals("0"))
  49. {
  50. sCode = GetSweepCodeInfo(15, 11, "9600,N,8,1");//扫码信息;九江用COM7
  51. WriteLog("扫码返回:" + sCode);
  52. if (!string.IsNullOrEmpty(sCode) && !isError)
  53. {
  54. strCode = sCode;
  55. string lb = "进入扫码";
  56. WriteLog(lb + ",读到的二维码扫码信息:" + strCode);
  57. }
  58. else
  59. {
  60. strCode = "";
  61. }
  62. //Thread.Sleep(20 * 1000);//15秒执行一次
  63. }
  64. else if (strState.Equals("2"))
  65. {
  66. strCode = "";
  67. }
  68. }
  69. }
  70. }
  71. catch (Exception exp)
  72. {
  73. strCode = "";
  74. WriteLog("扫码信息读取异常!" + exp.Message);
  75. }
  76. }
  77. /// <summary>
  78. /// 关闭线程
  79. /// </summary>
  80. /// <returns></returns>
  81. public bool CloseThread()
  82. {
  83. try
  84. {
  85. if (mThread != null)
  86. {
  87. mThread.Abort();
  88. Close();
  89. }
  90. return true;
  91. }
  92. catch
  93. {
  94. return false;
  95. }
  96. }
  97. /// <summary>
  98. /// 二维码/条形码 信息获取
  99. /// </summary>
  100. /// <param name="intSleep">扫码枪多长时间关闭</param>
  101. /// <param name="iPort">端口</param>
  102. /// <param name="strComParam">波特率</param>
  103. /// <returns></returns>
  104. public string GetSweepCodeInfo(int intSleep, int iPort, string strComParam)
  105. {
  106. try
  107. {
  108. string strSweepCode = "";
  109. int i = Scanning.SetDeviceType(3);//设备类型
  110. if (i != 0)
  111. {
  112. isError = true;
  113. WriteLog("设备类型异常");
  114. return "设备类型异常";
  115. }
  116. i = Scanning.OpenDevice(iPort, strComParam);//打开扫描枪
  117. if (i != 0)
  118. {
  119. isError = true;
  120. WriteLog("打开扫描枪异常");
  121. return "打开扫描枪异常";//打开扫描枪异常
  122. }
  123. byte[] szData = new byte[256];
  124. i = Scanning.ReadData(ref szData[0], intSleep);//同步读条码信息
  125. string strGet = System.Text.Encoding.Default.GetString(szData, 0, szData.Length); //将字节数组转换为字符串
  126. if (!string.IsNullOrEmpty(strGet))
  127. {
  128. byte[] buffer = Encoding.UTF8.GetBytes(strGet);
  129. strGet = Encoding.GetEncoding("utf-8").GetString(buffer);
  130. //WriteLog("二维码信息(转换中文操作):" + strGet);
  131. }
  132. strSweepCode = strGet.Trim().Substring(0, 120).Replace("\r", "").Replace("\n", "").Replace(" ", "").Replace("\0", "");//得到二维码编号
  133. //strSweepCode = strSweepCode.Split('|')[0];
  134. if (!string.IsNullOrEmpty(strSweepCode))
  135. WriteLog("二维码编号:【" + strSweepCode + "】");
  136. i = Scanning.CloseDevice();//关闭扫描枪
  137. return strSweepCode;
  138. }
  139. catch (Exception exp)
  140. {
  141. isError = true;
  142. WriteLog("扫码异常!" + exp.Message);
  143. return "";
  144. }
  145. }
  146. /// <summary>
  147. /// 关闭扫描枪
  148. /// </summary>
  149. public void Close()
  150. {
  151. WriteLog("扫码关闭!");
  152. Scanning.CloseDevice();//关闭扫描枪
  153. }
  154. private void WriteLog(string str)
  155. {
  156. string m_szRunPath;
  157. m_szRunPath = System.Environment.CurrentDirectory;
  158. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  159. {
  160. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  161. }
  162. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  163. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  164. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  165. {
  166. Directory.CreateDirectory(strPathFile);
  167. }
  168. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\扫码设备_" + strDate + ".log", true);
  169. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  170. tw.WriteLine(str);
  171. tw.WriteLine("\r\n");
  172. tw.Close();
  173. }
  174. }
  175. }