|
本帖最后由 JXCine 于 2021-4-13 11:20 编辑
C#做了一个界面,在自己电脑上运行的好好的,.exe文件夹拷贝到另一个电脑上运行时,就会时不时的出现如下问题,请问这是啥原因呢,附上部分代码,急~~~
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
using System.IO.Ports;
namespace WindowsForms7kwCali
{
public partial class MainForm : Form
{
public static SerialPort serialPortPlc;
public static SerialPort serialPortIT9120;
static Mutex Mut; // 线程锁
//创建定时器
System.Timers.Timer MainTimer = new System.Timers.Timer(500)
{
AutoReset = true,
Enabled = false
};
public MainForm()
{
InitializeComponent();
// 1、初始化窗体大小相关的内容
this.Resize += new EventHandler(MyResize); // 窗体调整大小时引发事件
WidthOrigin = this.Width; // 获取窗体的宽度
HeightOrigin = this.Height; // 获取窗体的高度
SetTag(this);
// 2、搜索当前存在的串口,并添加到“串口号”中
SearchPorts();
// 3、创建线程锁
Mut = new Mutex();
// 4、接收数据线程
Control.CheckForIllegalCrossThreadCalls = false;
Thread CanReceiveThread = new Thread(Thread_Receive);
CanReceiveThread.Start();
// 5、校准线程
Thread CaliThread = new Thread(Thread_Calibration);
CaliThread.Start();
// 6、显示线程
Thread ShowThread = new Thread(Thread_Textshow);
ShowThread.Start();
// 7、定时器
MainTimer.Elapsed += new System.Timers.ElapsedEventHandler(MainTimerUp);
//MainTimer.Enabled = true;
//MainTimer.Start();
}
private void Thread_Calibration()
{
float Verr = 0;
float Ierr = 0;
float VBATerr = 0;
while (true)
{
Thread.Sleep(1);
switch (CaliFsm)
{
case 0x00: //开启充电机输入电压(Y0)
{
Plc_Input_Control(1);
InputVolOnFlag = 1;
break;
}
case 0x01: //开启独立运行模式、继电器闭合
{
Charger_Mode_Control();
Thread.Sleep(1000);
ChargerMode_And_RelayState_Read();
Thread.Sleep(500);
break;
}
case 0x02: //开启负载1
{
serialPortPlc.Write(setY1, 0, 15); //关闭Y1开启Y2Y0
Load1OnFlag = 1;
break;
}
case 0x03: //设置运行参数1:电压=420V 电流=10A ,功率输出开
{
Charger_Voltage_And_Current_Control(420, 10);
Thread.Sleep(500);
Charger_Output_Control(1);
Thread.Sleep(500);
Charger_Voltage_And_Current_Read();
Thread.Sleep(500);
Charger_Output_State_Read();
Thread.Sleep(5000);
break;
}
case 0x04: //读取功率表检测到的电压
{
PMeterStateRead(0); //读取电压1
Thread.Sleep(500);
break;
}
case 0x05: //读取功率表检测到的电流
{
//if (TempShowFlag == 0)
{
PMeterStateRead(1); //读取电流1
Thread.Sleep(500);
}
break;
}
case 0x06: //读取充电机检测到的电压电流
{
Read_Charger_Voltage_And_Current();
Thread.Sleep(500);
break;
}
case 0x07: //关闭充电机输出
{
Charger_Output_Control(0);
Thread.Sleep(300);
Charger_Output_State_Read(); //充电机输出状态读取
Thread.Sleep(300);
break;
}
case 0x08: //切换到负载2
{
serialPortPlc.Write(setY2, 0, 15); //关闭Y2开启Y1Y0
Load2OnFlag = 1;
break;
}
case 0x09: //设置运行参数2:电压=300V 电流=22A ,功率输出开
{
Charger_Voltage_And_Current_Control(300, 22);
Thread.Sleep(500);
Charger_Output_Control(1);
Thread.Sleep(500);
Charger_Voltage_And_Current_Read();
Thread.Sleep(500);
Charger_Output_State_Read();
Thread.Sleep(2000);
break;
}
case 0x0A: //读取功率表电压2
{
PMeterStateRead(0);
Thread.Sleep(500);
break;
}
case 0x0B: //读取功率表电流2
{
PMeterStateRead(1); //读取电流2
Thread.Sleep(500);
break;
}
case 0x0C: //读取充电机检测到的电压电流
{
Read_Charger_Voltage_And_Current();
Thread.Sleep(500);
break;
}
case 0x0D: //计算校准参数
{
Cali_V_Calculate();
Cali_I_Calculate();
Cali_Vbat_Calculate();
Thread.Sleep(2000);
CaliProgressShowFlag = 0x09; //显示校准参数计算完毕
break;
}
case 0x0E: //保存校准参数到RAM
{
CalibrationParamVandI_To_RAM();
Thread.Sleep(500);
CalibrationParamVbat_To_RAM();
Thread.Sleep(500);
CaliProgressShowFlag = 0x0A; //显示校准参数保存到RAM完成
CaliFsm = 0x0F;
break;
}
case 0x0F: //读取功率表电压值3:用于核验
{
PMeterStateRead(0); //电压
Thread.Sleep(500);
break;
}
case 0x10: //读取功率表电流值3:用于核验
{
PMeterStateRead(1); //读取电流3
Thread.Sleep(500);
break;
}
case 0x11: //读取充电机检测到的电压电流
{
Read_Charger_Voltage_And_Current();
Thread.Sleep(500);
break;
}
case 0x12:
{
Verr = System.Math.Abs(recvPMeterV3 - OutputVoltage3Value);
Ierr = System.Math.Abs(recvPMeterI3 - OutputCurrent3Value);
VBATerr = System.Math.Abs(recvPMeterV3 - Battery3Voltage);
if (
(Verr <= 30)
&& (VBATerr <= 30)
&& (Ierr <= 3)
)
{
CaliFsm = 0x13;
}
else //校准失败
{
CaliProgressShowFlag = 0x0C; //显示校准失败
}
break;
}
case 0x13://检验写入ROM是否成功
{
//保存校准参数到ROM
CalibrationParam_To_ROM();
Thread.Sleep(3000);
//提示校准完成
if (WriteToROMSuccess)
{
CaliProgressShowFlag = 0x0D; //显示校准成功
}
else
{
CaliProgressShowFlag = 0x0E; //显示保存ROM失败
}
break;
}
}
}
}
|
|