SweepCode.cs 6.1 KB

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