SweepCode.cs 6.5 KB

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