SweepCode.cs 6.4 KB

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