|
|
@@ -110,9 +110,19 @@ namespace FileWatcher
|
|
|
double lastqty = 0;
|
|
|
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
|
{
|
|
|
- int len = serialPort1.BytesToRead;
|
|
|
- Byte[] readBuffer = new Byte[len];
|
|
|
- serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
|
|
|
+ int len;
|
|
|
+ Byte[] readBuffer;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ len = serialPort1.BytesToRead;
|
|
|
+ readBuffer = new Byte[len];
|
|
|
+ serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ //停止监控关闭串口的瞬间,后台读取可能抛异常,忽略本次数据
|
|
|
+ return;
|
|
|
+ }
|
|
|
string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
|
|
|
string error = "";
|
|
|
if (data != "")
|
|
|
@@ -211,6 +221,12 @@ namespace FileWatcher
|
|
|
WindowState = FormWindowState.Minimized;
|
|
|
e.Cancel = true;
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //退出程序前释放串口,避免端口被占用
|
|
|
+ Timer.Stop();
|
|
|
+ ReleaseSerial();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private void AutoStart_CheckedChanged(object sender, EventArgs e)
|
|
|
@@ -234,7 +250,7 @@ namespace FileWatcher
|
|
|
" from devsmtlocation left join (select DSL_LINECODE alllincode,Dsl_Location allLocation, " +
|
|
|
" sum(dsl_madeqty)allmadeqty from devsmtlocation group by DSL_LINECODE,Dsl_Location)A on " +
|
|
|
" alllincode=devsmtlocation.dsl_linecode and allLocation=dsl_location left join barcode " +
|
|
|
- " on dsl_barcode = bar_code where dsl_status=0 and dsl_usable=1 and dsl_linecode='" + li_code.Text + "' and DSL_GETQTY*0.1>dsl_remainqty)" +
|
|
|
+ " on dsl_barcode = bar_code where dsl_status=0 and dsl_usable=1 and dsl_linecode='" + li_code.Text + "' and DSL_GETQTY*0.1>(select sum(dsl_remainqty)from devsmtlocation T where T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null))" +
|
|
|
" ");
|
|
|
System.Data.DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
|
|
|
for (int i = 0; i < dt.Rows.Count; i++)
|
|
|
@@ -407,6 +423,8 @@ namespace FileWatcher
|
|
|
StopWatch.Enabled = true;
|
|
|
try
|
|
|
{
|
|
|
+ //重新打开前先释放旧的串口资源(PLC管道/计数器串口),避免端口被占用
|
|
|
+ ReleaseSerial();
|
|
|
if (CountType == "PLC")
|
|
|
{
|
|
|
plc = new HslCommunication.Profinet.Panasonic.PanasonicMewtocol();
|
|
|
@@ -478,10 +496,43 @@ namespace FileWatcher
|
|
|
StartWatch.Enabled = true;
|
|
|
ma_code.Enabled = true;
|
|
|
StopWatch.Enabled = false;
|
|
|
- serialPort1.Close();
|
|
|
+ //先停定时器再释放串口,避免停止后仍在访问
|
|
|
+ Timer.Stop();
|
|
|
+ ReleaseSerial();
|
|
|
OperateResult.AppendText("停止执行监控\n");
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 释放串口资源:计数器模式串口 + PLC通信管道内的串口,防止端口被占用
|
|
|
+ /// </summary>
|
|
|
+ private void ReleaseSerial()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (serialPort1.IsOpen)
|
|
|
+ {
|
|
|
+ serialPort1.Close();
|
|
|
+ OperateResult.AppendText("计数器串口已关闭\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ OperateResult.AppendText(">>关闭计数器串口失败:" + ex.Message + "\n");
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (plc != null)
|
|
|
+ {
|
|
|
+ plc.Close();
|
|
|
+ OperateResult.AppendText("PLC通信已关闭\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ OperateResult.AppendText(">>关闭PLC通信失败:" + ex.Message + "\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>停机指令是否已下发(写入Stop后置false,点“启动”后复位,避免重复写、重复弹窗)</summary>
|
|
|
bool StopData = true;
|
|
|
|
|
|
@@ -770,7 +821,7 @@ namespace FileWatcher
|
|
|
|
|
|
private void pr_pcbacount_ValueChanged(object sender, EventArgs e)
|
|
|
{
|
|
|
- dh.ExecuteSql("update product set pr_pcbacount='"+pr_pcbacount.Value+"' where pr_code=(select ma_prodcode from make where ma_code='"+ma_code.Text+"')", "update");
|
|
|
+ dh.ExecuteSql("update product set pr_pcbacount='" + pr_pcbacount.Value + "' where pr_code=(select ma_prodcode from make where ma_code='" + ma_code.Text + "')", "update");
|
|
|
}
|
|
|
}
|
|
|
}
|