SweepCode.cs 6.0 KB

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