FrmStandAloneMetering.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. using com.hnshituo.core.webapp.vo;
  2. using Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.IO;
  8. using System.IO.Ports;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. namespace FrmStandAloneMetering
  13. {
  14. /// <summary>
  15. /// 仪表状态
  16. /// </summary>
  17. public enum MeterStatus
  18. {
  19. /// <summary>
  20. /// Stable 稳定
  21. /// </summary>
  22. Stable,
  23. /// <summary>
  24. /// 不稳定
  25. /// </summary>
  26. UnStable,
  27. /// <summary>
  28. /// 空磅
  29. /// </summary>
  30. Null,
  31. ///// <summary>
  32. ///// 未连接
  33. ///// </summary>
  34. UnConnect,
  35. ///// <summary>
  36. ///// 已连接
  37. ///// </summary>
  38. Connect
  39. }
  40. public partial class FrmStandAloneMetering : Form
  41. {
  42. private string strComPara; //仪表参数
  43. private int messageLength; //报文长度
  44. private string strSeparate;//分割符
  45. private int startPosition; //开始取值位置
  46. private int dataLength; //数据长度
  47. private int sleepTime; //采样频率
  48. private int stableTime; //稳定时间
  49. private int stableCount; //稳定次数
  50. private int stableDiff; //稳定次数
  51. private double tmpWeight; //临时重量,用于判断是否稳定状态
  52. private int weightStabCount; //波动次数
  53. private SerialPort serialPort1; //数据采集串口
  54. private bool blThreadFlag;//数据采集线程开关
  55. private StringBuilder weightLog = new StringBuilder(); //重量日志的文件
  56. private MeterWorkCarActFirAlonService alonService = new MeterWorkCarActFirAlonService();
  57. public FrmStandAloneMetering()
  58. {
  59. InitializeComponent();
  60. }
  61. private void FrmStandAloneMetering_Load(object sender, EventArgs e)
  62. {
  63. strComPara = ConfigurationManager.AppSettings["ComPara"].ToString().Trim(); //仪表参数COM口
  64. messageLength = Convert.ToInt32(ConfigurationManager.AppSettings["MessageLength"].ToString().Trim()); //报文长度
  65. strSeparate = ConfigurationManager.AppSettings["Separate"].ToString().Trim();//分割符
  66. startPosition = Convert.ToInt32(ConfigurationManager.AppSettings["StartPosition"].ToString().Trim());//开始取值位置
  67. dataLength = Convert.ToInt32(ConfigurationManager.AppSettings["DataLength"].ToString().Trim());// 数据长度
  68. sleepTime = Convert.ToInt32(ConfigurationManager.AppSettings["SleepTime"].ToString().Trim());//采样频率
  69. stableTime = Convert.ToInt32(ConfigurationManager.AppSettings["StableTime"].ToString().Trim());//稳定时间
  70. stableDiff = Convert.ToInt32(ConfigurationManager.AppSettings["StableDiff"].ToString().Trim());//稳定重量
  71. stableCount = stableTime * 1000 / sleepTime;//稳定次数
  72. //界面COM赋值
  73. if (!String.IsNullOrEmpty(strComPara) && strComPara.Contains(","))
  74. {
  75. string[] strParams = strComPara.Split(new char[] { ',' });
  76. cbChooseCom.Text = strParams[0];
  77. }
  78. //界面稳定时间赋值
  79. cbStableSchedule.Text = stableTime.ToString();
  80. tmpWeight = 0;//临时重量
  81. weightStabCount = 0; //波动次数
  82. }
  83. #region 用户控件的事件
  84. /// <summary>
  85. /// 开启数据采集
  86. /// </summary>
  87. /// <param name="sender"></param>
  88. /// <param name="e"></param>
  89. private void btnCollection_Click(object sender, EventArgs e)
  90. {
  91. if (blThreadFlag)
  92. {
  93. MessageBox.Show("采集程序已经运行,禁止重新进行采集!");
  94. return;
  95. }
  96. blThreadFlag = true;
  97. System.Threading.Thread DataCollectThread = new System.Threading.Thread(new System.Threading.ThreadStart(DataCollect));
  98. DataCollectThread.Start();
  99. }
  100. /// <summary>
  101. /// 上传本地的计量数据
  102. /// </summary>
  103. /// <param name="sender"></param>
  104. /// <param name="e"></param>
  105. private void btnUploadData_Click(object sender, EventArgs e)
  106. {
  107. uploadData();
  108. }
  109. /// <summary>
  110. /// 下载服务器的基础数据
  111. /// </summary>
  112. /// <param name="sender"></param>
  113. /// <param name="e"></param>
  114. private void btnDownloadData_Click(object sender, EventArgs e)
  115. {
  116. // 获取物料信息
  117. MeterBaseMatterInfoService meterBaseMatterInfo = new MeterBaseMatterInfoService();//物料服务
  118. MeterBaseMatterInfo MatterInfo1 = new MeterBaseMatterInfo();
  119. MatterInfo1.validFlag = "1";
  120. MatterInfo1.pageNum = 1;
  121. MatterInfo1.pageSize = 9999;
  122. RESTfulResult<List<MeterBaseMatterInfo>> rmx = meterBaseMatterInfo.doQueryListLike(MatterInfo1);
  123. // 删除CSV文件
  124. Utils.FileUtil.DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "baseData\\MeterBaseMatterInfo.csv");
  125. // 重新保存CSV文件
  126. saveMatterInfo(rmx.Data);
  127. }
  128. /// <summary>
  129. /// 数据查询
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. private void btQuery_Click(object sender, EventArgs e)
  134. {
  135. doQuery();
  136. }
  137. /// <summary>
  138. /// 毛重保存
  139. /// </summary>
  140. /// <param name="sender"></param>
  141. /// <param name="e"></param>
  142. private void btSaveGross_Click(object sender, EventArgs e)
  143. {
  144. saveData("0"); //毛重保存(0=毛重;1=常规皮重)
  145. doQuery();
  146. }
  147. /// <summary>
  148. /// 皮重保存
  149. /// </summary>
  150. /// <param name="sender"></param>
  151. /// <param name="e"></param>
  152. private void btSaveTare_Click(object sender, EventArgs e)
  153. {
  154. saveData("1"); //毛重保存(0=毛重;1=常规皮重)
  155. doQuery();
  156. }
  157. /// <summary>
  158. /// 界面关闭
  159. /// </summary>
  160. /// <param name="sender"></param>
  161. /// <param name="e"></param>
  162. private void FrmStandAloneMetering_FormClosing(object sender, FormClosingEventArgs e)
  163. {
  164. blThreadFlag = false;
  165. System.Threading.Thread.Sleep(sleepTime * 2);
  166. if (serialPort1 != null)
  167. {
  168. serialPort1.Close();
  169. }
  170. Application.Exit();
  171. }
  172. /// <summary>
  173. /// 稳定时间选择事件
  174. /// </summary>
  175. /// <param name="sender"></param>
  176. /// <param name="e"></param>
  177. private void cbStableSchedule_TextChanged(object sender, EventArgs e)
  178. {
  179. stableTime = Convert.ToInt32(cbStableSchedule.Text.Trim());//稳定时间
  180. stableCount = stableTime * 1000 / sleepTime;//稳定次数
  181. }
  182. /// <summary>
  183. /// COM口下拉事件
  184. /// </summary>
  185. /// <param name="sender"></param>
  186. /// <param name="e"></param>
  187. private void cbChooseCom_TextChanged(object sender, EventArgs e)
  188. {
  189. if (blThreadFlag)
  190. {
  191. MessageBox.Show("采集程序已经运行,禁止重新调整COM口!");
  192. return;
  193. }
  194. }
  195. #endregion 用户控件的事件
  196. #region 数据采集
  197. /// <summary>
  198. /// 数据采集线程
  199. /// </summary>
  200. private void DataCollect()
  201. {
  202. if (!blThreadFlag) return;
  203. string[] strParams = strComPara.Split(new char[] { ',' });
  204. Parity parity = Parity.None;
  205. if (strParams[2].ToUpper() == "ODD")
  206. {
  207. parity = Parity.Odd;
  208. }
  209. if (strParams[2].ToUpper() == "EVEN")
  210. {
  211. parity = Parity.Even;
  212. }
  213. StopBits stopBits = StopBits.None;
  214. if (strParams[4] == "1")
  215. {
  216. stopBits = StopBits.One;
  217. }
  218. if (strParams[4] == "1.5")
  219. {
  220. stopBits = StopBits.OnePointFive;
  221. }
  222. if (strParams[4] == "2")
  223. {
  224. stopBits = StopBits.Two;
  225. }
  226. serialPort1 = new SerialPort(strParams[0], Int32.Parse(strParams[1]),
  227. parity, Int32.Parse(strParams[3]), stopBits);
  228. while (!serialPort1.IsOpen)
  229. {
  230. try
  231. {
  232. serialPort1.Open();
  233. }
  234. catch (Exception err)
  235. {
  236. WriteCatchLog(err.ToString());
  237. }
  238. System.Threading.Thread.Sleep(500);
  239. }
  240. StringBuilder stringBuilder = new StringBuilder();
  241. string strtmp = "";
  242. while (blThreadFlag)
  243. {
  244. try
  245. {
  246. System.Threading.Thread.Sleep(sleepTime);
  247. if (serialPort1.BytesToRead > 0)
  248. {
  249. strtmp = serialPort1.ReadExisting();
  250. stringBuilder.Append(strtmp);
  251. //"US,GS,+0073.34 t\r\nUS,GS,+0073.96 t\r\nUS,GS,+0071.36 t\r\nUS,GS,+0070.40 t\r\nUS,GS,+0073.06 t\r\nUS,GS,+0073.16 t\r\nUS,GS,+0071.72 t\r\nUS,GS,+0073.30 t\r\nUS,GS,+0074.16 t\r\n"
  252. //0252.50 kg
  253. //20 20 0D 0A 7F 30 32 35 32 2E 35 30 20 6B 67
  254. //03 04 0D 0A FF 30 30 30 30 31 35 20 6B 67
  255. //char strSeparate = (char)0x6B;
  256. if (stringBuilder.ToString().LastIndexOf(strSeparate) >= 0)
  257. {
  258. if (stringBuilder.ToString().LastIndexOf(strSeparate) < messageLength)//不完整报文,抛掉
  259. {
  260. stringBuilder.Remove(0, stringBuilder.ToString().LastIndexOf(strSeparate) + strSeparate.Length);
  261. //stringBuilder.Remove(0, stringBuilder.ToString().LastIndexOf(strSeparate) + strSeparate.Length);
  262. }
  263. int index = stringBuilder.ToString().LastIndexOf(strSeparate);
  264. if ((index - messageLength) < 0)
  265. continue;
  266. string strData = stringBuilder.ToString().Substring(index - messageLength, messageLength + strSeparate.Length);
  267. // WriteCatchLog("strData " + strData);
  268. string weight = strData.Substring(startPosition, dataLength);
  269. if ("T".Equals(strSeparate.ToUpper()))
  270. {
  271. //单位为T是做特殊处理,界面显示的是T
  272. weight = (Convert.ToDouble(weight) * 1000) + "";
  273. }
  274. //WriteCatchLog("weight " + weight);
  275. stringBuilder.Remove(0, stringBuilder.Length);
  276. ucStorageWeightT1.setWgt(Math.Round(Convert.ToDouble(weight), 3));
  277. DoShowWeightStatus(Convert.ToDouble(weight));
  278. }
  279. }
  280. }
  281. catch (Exception err)
  282. {
  283. MessageBox.Show("串口打开异常!异常原因" + err.ToString());
  284. WriteCatchLog("DataCollect1" + err.ToString());
  285. }
  286. }
  287. }
  288. /// <summary>
  289. /// 重量稳定判断
  290. /// </summary>
  291. /// <param name="_weight"></param>
  292. public void DoShowWeightStatus(double _weight)
  293. {
  294. if (Math.Abs(_weight) < 50)
  295. {
  296. //不稳定状态;红灯
  297. ucStorageWeightT1.setStable(false);
  298. return;
  299. }
  300. //波动在范围内时,稳定次数+1,临时重量不变;波动范围超出时,稳定次数清零,更新临时重量,
  301. int inDiff = Math.Abs(Convert.ToInt32(tmpWeight - _weight));
  302. if (inDiff < stableDiff)
  303. {
  304. weightStabCount += 1;
  305. }
  306. else
  307. {
  308. weightStabCount = 0;
  309. }
  310. //WriteCatchLog("_tmpWeight: " + _tmpWeight + " _weight: " + _weight + " inDiff: "+inDiff+ " StabCount : " + weightStabCount);
  311. tmpWeight = Convert.ToSingle(_weight);
  312. //重量30kg内波动超过10次认为重量稳定
  313. if (weightStabCount > stableCount)
  314. {
  315. //稳定状态;绿灯
  316. ucStorageWeightT1.setStable(true);
  317. weightStabCount = stableCount;
  318. }
  319. else
  320. {
  321. ucStorageWeightT1.setStable(false);
  322. }
  323. }
  324. #endregion 数据采集
  325. /// <summary>
  326. /// 重量保存:0=毛重;1=常规皮重
  327. /// </summary>
  328. /// <param name="strWeightType"></param>
  329. private void saveData(string strWeightType)
  330. {
  331. MeterWorkCarActualFirst actualFirst = new MeterWorkCarActualFirst(); //一次计量实体
  332. actualFirst.actualFirstNo = "CAR" + DateTime.Now.ToString("yyyyMMddHHmmssfff"); //主键
  333. actualFirst.carNo = tbCarNo.Text.Trim();//车号
  334. actualFirst.baseSpotName = cbBaseSpot.Text.Trim();//计量点
  335. actualFirst.meterTypeName = cbMeterTypeName.Text.Trim();//业务类型
  336. actualFirst.contractNo = cbContractNo.Text.Trim();//合同号
  337. actualFirst.batchNo = tbBatchNo.Text.Trim();//批次号
  338. actualFirst.heatNo = tbHeatNo.Text.Trim();//炉号
  339. actualFirst.shipmentNum = Convert.ToInt32(string.IsNullOrEmpty(tbShipmentNum.Text.Trim()) ? "0" : tbShipmentNum.Text.Trim());//包数/数量
  340. actualFirst.matterName = cbMatterName.Text.Trim();//物资名称
  341. actualFirst.receivingUintName = cbReceivingUintName.Text.Trim();//收货单位
  342. actualFirst.forwardingUnitName = cbForwardingUnitName.Text.Trim();//发货单位
  343. actualFirst.customerSupplierName = cbCustomerSupplierName.Text.Trim();//供应商
  344. actualFirst.loadPointName = cbLoadPointName.Text.Trim();//卸货地点
  345. actualFirst.memo = tbMemo.Text.Trim();//备注
  346. actualFirst.weightType = strWeightType;//重量类型
  347. actualFirst.meterWeight = ucStorageWeightT1.getWgt() * 1000;
  348. //DataTable csvDataTable = OpenCSV(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv");
  349. if (System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd")) == false
  350. || System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv") == false)
  351. {
  352. StringBuilder actualFirstLogHead = new StringBuilder(); //一次计量数据保存文件
  353. actualFirstLogHead.Append("createTime,");//若第一行默认为当前时间则第一行多个逗号
  354. actualFirstLogHead.Append("actualFirstNo,");
  355. actualFirstLogHead.Append("carNo,");
  356. actualFirstLogHead.Append("baseSpotName,");
  357. actualFirstLogHead.Append("meterTypeName,");
  358. actualFirstLogHead.Append("contractNo,");
  359. actualFirstLogHead.Append("batchNo,");
  360. actualFirstLogHead.Append("heatNo,");
  361. actualFirstLogHead.Append("shipmentNum,");
  362. actualFirstLogHead.Append("matterName,");
  363. actualFirstLogHead.Append("receivingUintName,");
  364. actualFirstLogHead.Append("forwardingUnitName,");
  365. actualFirstLogHead.Append("customerSupplierName,");
  366. actualFirstLogHead.Append("loadPointName,");
  367. actualFirstLogHead.Append("memo,");
  368. actualFirstLogHead.Append("weightType,"); //最后一行不要逗号
  369. actualFirstLogHead.Append("meterWeight");
  370. logCsv.WriteDataLog("actualFirst", actualFirstLogHead.ToString());
  371. }
  372. StringBuilder actualFirstLog = new StringBuilder(); //一次计量数据保存文件
  373. actualFirstLog.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ",");
  374. actualFirstLog.Append(actualFirst.actualFirstNo.ToString() + ",");
  375. actualFirstLog.Append(actualFirst.carNo.ToString() + ",");
  376. actualFirstLog.Append(actualFirst.baseSpotName.ToString() + ",");
  377. actualFirstLog.Append(actualFirst.meterTypeName.ToString() + ",");
  378. actualFirstLog.Append(actualFirst.contractNo.ToString() + ",");
  379. actualFirstLog.Append(actualFirst.batchNo.ToString() + ",");
  380. actualFirstLog.Append(actualFirst.heatNo.ToString() + ",");
  381. actualFirstLog.Append(actualFirst.shipmentNum.ToString() + ",");
  382. actualFirstLog.Append(actualFirst.matterName.ToString() + ",");
  383. actualFirstLog.Append(actualFirst.receivingUintName.ToString() + ",");
  384. actualFirstLog.Append(actualFirst.forwardingUnitName.ToString() + ",");
  385. actualFirstLog.Append(actualFirst.customerSupplierName.ToString() + ",");
  386. actualFirstLog.Append(actualFirst.loadPointName.ToString() + ",");
  387. actualFirstLog.Append(actualFirst.memo.ToString() + ",");
  388. actualFirstLog.Append(actualFirst.weightType.ToString() + ",");
  389. actualFirstLog.Append(actualFirst.meterWeight.ToString());
  390. logCsv.WriteDataLog("actualFirst", actualFirstLog.ToString());
  391. }
  392. /// <summary>
  393. /// 上传数据
  394. /// </summary>
  395. private void uploadData()
  396. {
  397. openFileDialogUpData.Filter = "csv文件|*.csv";//只允许csv文件;*.csv;*.xlsx
  398. openFileDialogUpData.Title = "选择要导入的计量数据文件"; //弹出框头部显示
  399. openFileDialogUpData.AddExtension = true; //自动增加后缀
  400. openFileDialogUpData.AutoUpgradeEnabled = true; //是否随系统自动升级弹出窗口样式
  401. openFileDialogUpData.InitialDirectory = Application.StartupPath + "\\alonData\\" + DateTime.Now.ToString("yyyy-MM-dd");//默认打开当前目录
  402. //openFileDialog1.Multiselect = true;//该值确定是否可以选择多个文件
  403. if (openFileDialogUpData.ShowDialog() == DialogResult.OK)
  404. {
  405. DataTable csvDataTable = OpenCSV(openFileDialogUpData.FileName);
  406. int rowCount = csvDataTable.Rows.Count;
  407. if (csvDataTable != null && csvDataTable.Rows.Count > 0)
  408. {
  409. List<MeterWorkCarActFirAlon> lp = csvDataTable.TableToDataList<MeterWorkCarActFirAlon>();
  410. DialogResult dr = MessageBox.Show("共 " + lp.Count + " 条计量数据,是否继续上传?", "提示", MessageBoxButtons.OKCancel);
  411. if (dr != DialogResult.OK)
  412. {
  413. return;
  414. }
  415. RESTfulResult<string> rES = alonService.batchInsertAlonData(lp);
  416. if (rES.Succeed)
  417. {
  418. MessageBox.Show("数据上传成功!");
  419. }
  420. else
  421. {
  422. MessageBox.Show("数据上传失败:" + rES.Message);
  423. }
  424. }
  425. };
  426. }
  427. public static DataTable OpenCSV(string filePath)
  428. {
  429. Encoding encoding = Encoding.UTF8; //Common.GetType(filePath); //Encoding.ASCII;//
  430. DataTable dt = new DataTable();
  431. FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  432. //StreamReader sr = new StreamReader(fs, Encoding.UTF8);
  433. StreamReader sr = new StreamReader(fs, encoding);
  434. //string fileContent = sr.ReadToEnd();
  435. //encoding = sr.CurrentEncoding;
  436. //记录每次读取的一行记录
  437. string strLine = "";
  438. //记录每行记录中的各字段内容
  439. string[] aryLine = null;
  440. string[] tableHead = null;
  441. //标示列数
  442. int columnCount = 0;
  443. //标示是否是读取的第一行
  444. bool IsFirst = true;
  445. //逐行读取CSV中的数据
  446. while ((strLine = sr.ReadLine()) != null)
  447. {
  448. //strLine = Common.ConvertStringUTF8(strLine, encoding);
  449. //strLine = Common.ConvertStringUTF8(strLine);
  450. if (IsFirst == true)
  451. {
  452. tableHead = strLine.Split(',');
  453. IsFirst = false;
  454. columnCount = tableHead.Length;
  455. //创建列
  456. for (int i = 0; i < columnCount; i++)
  457. {
  458. DataColumn dc = new DataColumn(tableHead[i]);
  459. //DataColumn dc = new DataColumn(i.ToString());
  460. dt.Columns.Add(dc);
  461. switch (i) { }
  462. }
  463. }
  464. else
  465. {
  466. aryLine = strLine.Split(',');
  467. DataRow dr = dt.NewRow();
  468. for (int j = 0; j < columnCount; j++)
  469. {
  470. dr[j] = aryLine[j];
  471. }
  472. dt.Rows.Add(dr);
  473. }
  474. }
  475. //if (aryLine != null && aryLine.Length > 0)
  476. //{
  477. // dt.DefaultView.Sort = tableHead[0] + " " + "asc";
  478. //}
  479. sr.Close();
  480. fs.Close();
  481. return dt;
  482. }
  483. /// <summary>
  484. /// 数据查询
  485. /// </summary>
  486. private void doQuery()
  487. {
  488. if (System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd")) == true
  489. && System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv") == true)
  490. {
  491. DataTable csvDataTable = OpenCSV(AppDomain.CurrentDomain.BaseDirectory + "alonData\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\actualFirst.csv");
  492. int rowCount = csvDataTable.Rows.Count;
  493. if (csvDataTable != null && csvDataTable.Rows.Count > 0)
  494. {
  495. List<MeterWorkCarActFirAlon> lp = csvDataTable.TableToDataList<MeterWorkCarActFirAlon>();
  496. rowCount = lp.Count;
  497. //毛重数据查询
  498. DataTable dtGross = csvDataTable.Clone();
  499. List<MeterWorkCarActFirAlon> lpGross = lp.Where(s => s.weightType == "0").ToList();
  500. if (lpGross != null && lpGross.Count > 0)
  501. {
  502. dtGross = lpGross.ListToDataTable<MeterWorkCarActFirAlon>();
  503. }
  504. ClsControlPack.CopyDataToDatatable(ref dtGross, ref this.dtCarActFirstGross, true);
  505. foreach (var item in this.ultraGridGross.Rows)
  506. {
  507. item.Cells["weightType"].Value = GetWeightType(item.Cells["weightType"].Value.ToString());
  508. }
  509. ClsControlPack.RefreshAndAutoSize(ultraGridGross);
  510. //皮重数据查询
  511. DataTable dtTare = csvDataTable.Clone();
  512. List<MeterWorkCarActFirAlon> lpTare = lp.Where(s => s.weightType == "1").ToList();
  513. if (lpTare != null && lpTare.Count > 0)
  514. {
  515. dtTare = lpTare.ListToDataTable<MeterWorkCarActFirAlon>();
  516. }
  517. ClsControlPack.CopyDataToDatatable(ref dtTare, ref this.dtCarActFirstTare, true);
  518. foreach (var item in this.ultraGridTare.Rows)
  519. {
  520. item.Cells["weightType"].Value = GetWeightType(item.Cells["weightType"].Value.ToString());
  521. }
  522. ClsControlPack.RefreshAndAutoSize(ultraGridTare);
  523. }
  524. }
  525. else
  526. {
  527. MessageBox.Show("未查询到当天的数据文件!");
  528. }
  529. }
  530. /// <summary>
  531. /// 重量类型
  532. /// </summary>
  533. /// <param name="strCode"></param>
  534. /// <returns></returns>
  535. public static string GetWeightType(string strCode)
  536. {
  537. string weightType = ""; //重量类型(0:毛重;1:皮重)
  538. switch (strCode)
  539. {
  540. case "0": weightType = "毛重"; break;
  541. case "1": weightType = "皮重"; break;
  542. default:
  543. break;
  544. }
  545. return weightType;
  546. }
  547. /// <summary>
  548. /// 记录错误日志
  549. /// </summary>
  550. /// <param name="str"></param>
  551. private void WriteCatchLog(string str)
  552. {
  553. try
  554. {
  555. string m_szRunPath = System.Environment.CurrentDirectory.ToString();
  556. if (!(System.IO.Directory.Exists(m_szRunPath + "\\log")))
  557. {
  558. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  559. }
  560. string strDate = System.DateTime.Now.ToString("yyyyMMddhh");
  561. System.IO.TextWriter tw = new System.IO.StreamWriter(m_szRunPath + "\\log\\catch.log", true);
  562. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + str);
  563. tw.Close();
  564. }
  565. catch
  566. {
  567. }
  568. }
  569. private void saveMatterInfo(List<MeterBaseMatterInfo> list)
  570. {
  571. StringBuilder materInfoHead = new StringBuilder(); // 物料头
  572. materInfoHead.Append("matterNo,");//若第一行默认为当前时间则第一行多个逗号
  573. materInfoHead.Append("matterName");
  574. logCsv.WriteBaseData("MeterBaseMatterInfo", materInfoHead.ToString());
  575. }
  576. }
  577. }