403 lines
16 KiB
C#
403 lines
16 KiB
C#
using Lskj.Business.Impl;
|
|
using Lskj.Control;
|
|
using Lskj.Core;
|
|
using Lskj.Util;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Data.SqlClient;
|
|
using System.Drawing;
|
|
using System.IO.Ports;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
using static Lskj.PubGetChipInfo.DllBaseClass;
|
|
|
|
namespace Lskj.PubGetChipInfo
|
|
{
|
|
public partial class FrmMain : BaseForm
|
|
{
|
|
public DynamicGetChipInfoModel GetChipInfoModel;
|
|
public SerialPort oneSerialPort = new SerialPort();
|
|
public Thread m_thread = null;
|
|
public string m_strCom = "";
|
|
public int m_nBaud = 0;
|
|
public bool m_bQuit = false;
|
|
public FrmMain()
|
|
{
|
|
InitializeComponent();
|
|
this.Load += OnFrmMainLoad;
|
|
this.btn_Read.Click += OnBtnReadClick;
|
|
this.btn_Ok.Click += OnBtnOkClick;
|
|
}
|
|
/// <summary>
|
|
/// 加载数据
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnFrmMainLoad(object sender, EventArgs e)
|
|
{
|
|
if (GetChipInfoModel.ChipType.Equals("1"))
|
|
{
|
|
lb_info.Text = "PRM序列号:";
|
|
}
|
|
else if (GetChipInfoModel.ChipType.Equals("2"))
|
|
{
|
|
lb_info.Text = "授权芯片号:";
|
|
}
|
|
string[] arryPort = SerialPort.GetPortNames();
|
|
foreach (string item in arryPort)
|
|
{
|
|
com_PortNum.Properties.Items.Add(item);
|
|
}
|
|
//设置串口
|
|
string portNum = IniHelper.Read(string.Format("PubGetChipInfo_PortNum_{0}", this.GetChipInfoModel.ModuleCode));
|
|
this.com_PortNum.Text = portNum;
|
|
this.com_PortNum.SelectedIndexChanged += OnPortNumSelectedIndexChanged;
|
|
//设置波特率
|
|
string bps = IniHelper.Read(string.Format("PubGetChipInfo_Bps_{0}", this.GetChipInfoModel.ModuleCode));
|
|
this.com_Bps.Text = bps;
|
|
this.com_Bps.SelectedIndexChanged += OnBpsSelectedIndexChanged;
|
|
}
|
|
/// <summary>
|
|
/// 读取按钮点击
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnBtnReadClick(object sender, EventArgs e)
|
|
{
|
|
if (GetChipInfoModel.ChipType.Equals("1"))
|
|
{
|
|
m_strCom = com_PortNum.Text;
|
|
int.TryParse(com_Bps.Text, out m_nBaud);
|
|
if (m_strCom.Length <= 0 || m_nBaud <= 0)
|
|
{
|
|
MessageUtil.Show("串口参数不全,请检查后重试!", MessageBoxButtons.OK);
|
|
return;
|
|
}
|
|
if (m_thread != null && m_thread.IsAlive == true)
|
|
{
|
|
MessageUtil.Show("请倒计时结束后再重新读取!", MessageBoxButtons.OK);
|
|
}
|
|
else
|
|
{
|
|
txtEdit_info.Text = "";
|
|
m_bQuit = false;
|
|
m_thread = new Thread(DoPRMAction);
|
|
m_thread.Start();
|
|
}
|
|
}
|
|
else if (GetChipInfoModel.ChipType.Equals("2"))
|
|
{
|
|
m_strCom = com_PortNum.Text;
|
|
int.TryParse(com_Bps.Text, out m_nBaud);
|
|
if (m_strCom.Length <= 0 || m_nBaud <= 0)
|
|
{
|
|
MessageUtil.Show("串口参数不全,请检查后重试!", MessageBoxButtons.OK);
|
|
return;
|
|
}
|
|
if (m_thread != null && m_bQuit == false)
|
|
{
|
|
MessageUtil.Show("请倒计时结束后再重新读取!", MessageBoxButtons.OK);
|
|
}
|
|
else
|
|
{
|
|
txtEdit_info.Text = "";
|
|
m_bQuit = false;
|
|
m_thread = new Thread(Do2H1Action);
|
|
m_thread.Start();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 确认按钮点击
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnBtnOkClick(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(txtEdit_info.Text))
|
|
{
|
|
MessageUtil.Show("未获取到结果,无法保存");
|
|
return;
|
|
}
|
|
if (!string.IsNullOrEmpty(GetChipInfoModel.AfterSql))
|
|
{
|
|
DataRow sourceRow = GetChipInfoModel.MaintabFocusedRow; ;
|
|
string afterSql = GetChipInfoModel.AfterSql.TrimStart('!');
|
|
string pattern = @"(\w+)\((.*?)\)";
|
|
Regex regex = new Regex(pattern);
|
|
Match match = regex.Match(afterSql);
|
|
if (match.Success)
|
|
{
|
|
string functionName = match.Groups[1].Value;
|
|
string parameters = match.Groups[2].Value;
|
|
if (!string.IsNullOrEmpty(functionName))
|
|
{
|
|
DataTable sqlParametersTab = BaseImpl.GetDataTableResult($"SELECT PARAMETER_NAME,PARAMETER_MODE,CHARACTER_MAXIMUM_LENGTH,DATA_TYPE FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_NAME = '{functionName}' ORDER BY ORDINAL_POSITION;");
|
|
|
|
using (SqlCommand command = new SqlCommand(functionName, (SqlConnection)SqlHelper._connection))
|
|
{
|
|
string[] parametersArray = parameters.Split(',');
|
|
foreach (DataRow parameterRow in sqlParametersTab.Rows)
|
|
{
|
|
string parameterName = parameterRow["PARAMETER_NAME"] + "";
|
|
string parameterMode = parameterRow["PARAMETER_MODE"] + "";
|
|
int.TryParse(parameterRow["CHARACTER_MAXIMUM_LENGTH"] + "", out int dataLength);
|
|
string dataType = parameterRow["DATA_TYPE"] + "";
|
|
SqlParameter sqlParameter = null;
|
|
if (parameterMode.Equals("int"))
|
|
{
|
|
sqlParameter = new SqlParameter(parameterName, SqlDbType.Int);
|
|
}
|
|
else
|
|
{
|
|
sqlParameter = new SqlParameter(parameterName, SqlDbType.VarChar, dataLength);
|
|
}
|
|
if (parameterMode.Equals("IN"))
|
|
{
|
|
sqlParameter.Direction = ParameterDirection.Input;
|
|
}
|
|
else
|
|
{
|
|
sqlParameter.Direction = ParameterDirection.Output;
|
|
}
|
|
command.Parameters.Add(sqlParameter);
|
|
}
|
|
for (int i = 0; i < command.Parameters.Count; i++)
|
|
{
|
|
SqlParameter sqlParameter = command.Parameters[i];
|
|
if (parametersArray.Length > i)
|
|
{
|
|
string commandParameter = parametersArray[i];
|
|
string field = commandParameter.Replace("@", "").Replace("{", "").Replace("}", "");
|
|
string value = sourceRow.Table.Columns.Contains(field) ? sourceRow[field] + "" : commandParameter.Replace("@", "");
|
|
value = value.Replace("{result}", txtEdit_info.Text);
|
|
value = value.Replace("@", "").Replace("{", "").Replace("}", "");
|
|
sqlParameter.Value = value;
|
|
}
|
|
}
|
|
SqlParameter returnValue = new SqlParameter("@return", SqlDbType.Int, 4);
|
|
returnValue.Direction = ParameterDirection.ReturnValue;
|
|
command.Parameters.Add(returnValue);
|
|
command.CommandType = CommandType.StoredProcedure;
|
|
command.ExecuteNonQuery();
|
|
if ((returnValue.Value + "").Equals("1"))
|
|
{
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
else
|
|
{
|
|
SqlParameter outSqlParameter = command.Parameters.Cast<SqlParameter>().Where(n => n.Direction == ParameterDirection.Output).FirstOrDefault();
|
|
if (outSqlParameter != null)
|
|
{
|
|
string outMessage = outSqlParameter.Value + "";
|
|
if (!string.IsNullOrEmpty(outMessage))
|
|
{
|
|
MessageUtil.Show(outSqlParameter.Value + "");
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|
|
else
|
|
{
|
|
MessageUtil.Show("保存逻辑配置错误");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MessageUtil.Show("保存逻辑配置错误");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageUtil.Show(ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 读取PRM芯片号
|
|
/// </summary>
|
|
public void DoPRMAction()
|
|
{
|
|
try
|
|
{
|
|
oneSerialPort.ReadTimeout = 200;
|
|
oneSerialPort.PortName = m_strCom;
|
|
oneSerialPort.BaudRate = m_nBaud;
|
|
if (oneSerialPort.IsOpen == false)
|
|
{
|
|
oneSerialPort.Open();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageUtil.Show(ex.Message);
|
|
return;
|
|
}
|
|
long lCurTick = DateTime.Now.Ticks;
|
|
int nCount = 10;
|
|
SetCountDownText(nCount.ToString());
|
|
while (!m_bQuit)
|
|
{
|
|
try
|
|
{
|
|
if (nCount <= 0)
|
|
{
|
|
SetCountDownText("读取PRM序列号(&E)");
|
|
break;
|
|
}
|
|
if ((DateTime.Now.Ticks - lCurTick) > 1.0e7)
|
|
{
|
|
nCount--;
|
|
SetCountDownText(nCount.ToString());
|
|
lCurTick = DateTime.Now.Ticks;
|
|
}
|
|
string message = oneSerialPort.ReadLine();
|
|
if (message.Contains("$PGDGI")
|
|
&& message.Contains("PRM")
|
|
&& message.Contains("SN"))
|
|
{
|
|
int nStart = message.IndexOf(':');
|
|
int nEnd = message.IndexOf('*');
|
|
|
|
SetInfoTxtEditText(message.Substring(nStart + 1, nEnd - nStart - 1));
|
|
SetCountDownText("读取PRM序列号(&E)");
|
|
break;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
oneSerialPort.Close();
|
|
m_bQuit = true;
|
|
}
|
|
/// <summary>
|
|
/// 读取授权芯片号
|
|
/// </summary>
|
|
public void Do2H1Action()
|
|
{
|
|
try
|
|
{
|
|
oneSerialPort.ReadTimeout = 200;
|
|
oneSerialPort.PortName = m_strCom;
|
|
oneSerialPort.BaudRate = m_nBaud;
|
|
if (oneSerialPort.IsOpen == false)
|
|
{
|
|
oneSerialPort.Open();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageUtil.Show(ex.Message);
|
|
return;
|
|
}
|
|
oneSerialPort.Write("$CCTST,888888*00\r\n");
|
|
Thread.Sleep(100);
|
|
long lCurTick = DateTime.Now.Ticks;
|
|
int nCount = 10;
|
|
SetCountDownText(nCount.ToString());
|
|
while (!m_bQuit)
|
|
{
|
|
try
|
|
{
|
|
if (nCount <= 0)
|
|
{
|
|
SetCountDownText("读取授权芯片号(&E)");
|
|
break;
|
|
}
|
|
if ((DateTime.Now.Ticks - lCurTick) > 1.0e7)
|
|
{
|
|
nCount--;
|
|
SetCountDownText(nCount.ToString());
|
|
lCurTick = DateTime.Now.Ticks;
|
|
oneSerialPort.Write("$CCRMO,ICN,2,0*00\r\n");
|
|
}
|
|
string message = oneSerialPort.ReadLine();
|
|
if (message.Contains("$BDICN")
|
|
&& message.Contains("*"))
|
|
{
|
|
int nStart = message.IndexOf(',');
|
|
int nEnd = message.IndexOf('*');
|
|
if (nEnd - nStart > 0)
|
|
{
|
|
SetInfoTxtEditText(message.Substring(nStart + 1, nEnd - nStart - 1));
|
|
SetCountDownText("读取授权芯片号(&E)");
|
|
m_bQuit = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
oneSerialPort.Close();
|
|
m_bQuit = true;
|
|
}
|
|
/// <summary>
|
|
/// 设置倒计时文本值
|
|
/// </summary>
|
|
/// <param name="text"></param>
|
|
private void SetCountDownText(string text)
|
|
{
|
|
if (btn_Read.InvokeRequired == true)
|
|
{
|
|
btn_Read.Invoke(new Action(() =>
|
|
{
|
|
btn_Read.Text = text;
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
btn_Read.Text = text;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 设置芯片文本值
|
|
/// </summary>
|
|
/// <param name="text"></param>
|
|
private void SetInfoTxtEditText(string text)
|
|
{
|
|
if (txtEdit_info.InvokeRequired)
|
|
{
|
|
txtEdit_info.Invoke(new Action(() =>
|
|
{
|
|
txtEdit_info.Text = text;
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
txtEdit_info.Text = text;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 串口改变时
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnPortNumSelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
IniHelper.Write(string.Format("PubGetChipInfo_PortNum_{0}", this.GetChipInfoModel.ModuleCode), com_PortNum.Text);
|
|
}
|
|
/// <summary>
|
|
/// 波特率改变时
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnBpsSelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
IniHelper.Write(string.Format("PubGetChipInfo_Bps_{0}", this.GetChipInfoModel.ModuleCode), com_Bps.Text);
|
|
}
|
|
}
|
|
}
|