我开发了一个应用程序,在该应用程序中,我每 5 秒轮询一次串行端口(发送请求并获得响应)。一切都很好,我彻底测试了它。
但是当我引入线程时,我收到有关物理内存的蓝屏错误或线程中附加的以下内容,我只是将 GUI 更新任务和轮询任务分开。
谁能建议我该怎么做才能删除相同的内容,只是卡住了,不知道该怎么做。
每次运行应用程序时都会发生这种情况,我使用的是 profic 驱动程序串行到 USB 驱动程序。
主要应用代码
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
PollingAllMeters();
}
public void PollingAllMeters()
{
int isdbError = 0;
PollingUtility.MeterClass mData = new PollingUtility.MeterClass();
for (int index = 0; index < m_slaveID.Length; index++)
{
try
{
mData = poll.MeterOne(m_slaveID[index]);
Thread processData = new Thread(() => this.updateData(data));
processData.Start();
}
catch { MessageBox.Show("Inside polling box");
}
}
private void updateData(MeterValues data)
{
mainFormRef.updateData(data);
}
实际请求和响应发生的 DLL 代码
enter code here
public MeterClass MeterOne(byte meterID)
{
MeterClass data1 = new MeterClass();
//Voltage
mb.SendFc3(meterID, startAdd, register, ref value_meter); //modbus call is made here for sending and receiving response at serial port
if (mb.modbusStatus == "Read successful")
{
//do some calculation
}
else
{
// error handling code to create log file
if (mb.modbusStatus == "Error in read event")
{
isPollError = 21;
}
else if (mb.modbusStatus == "CRC error")
{
isPollError = 22;
}
else if (mb.modbusStatus == "Serial port not open")
{
isPollError = 23;
}
}
startAdd = 360;
register = 2;
value_meter = new byte[register * 2];
meterData = new byte[register * 2];
//Max demand
mb.SendFc3(meterID, startAdd, register, ref value_meter);
if (mb.modbusStatus == "Read successful")
{
//do some calculation
}
else
{
// error handling code to create log file
if (mb.modbusStatus == "Error in read event")
{
isPollError = 21;
}
else if (mb.modbusStatus == "CRC error")
{
isPollError = 22;
}
else if (mb.modbusStatus == "Serial port not open")
{
isPollError = 23;
}
}
startAdd = 496;
register = 16;
value_meter = new byte[register * 2];
meterData = new byte[register * 2];
mb.SendFc3(meterID, startAdd, register, ref value_meter);
if (mb.modbusStatus == "Read successful")
{
//do some calcualtion
}
else
{
// error handling code to create log file
if (mb.modbusStatus == "Error in read event")
{
isPollError = 21;
}
else if (mb.modbusStatus == "CRC error")
{
isPollError = 22;
}
else if (mb.modbusStatus == "Serial port not open")
{
isPollError = 23;
}
}
data1.date = DateTime.Now;
data1.Status = isPollError;
return data1; // return result to the main program
}