我用c#编写了一个连接PLC S7-200的程序。我使用 dll Nmodbus。
我的目标:从PLC在线读取数据并将数据写入PLC。我使用计时器从 PLC 在线读取数据。
问题:当我多次写入数据时,我的程序挂起。
Button1_click:写入数据
Button5_click:更新显示数据
public partial class Form1 : Form
{
System.Timers.Timer aTimer = new System.Timers.Timer(5000);
SerialPort sp = new SerialPort("COM10");
int count = 1;
void Update()
{
byte slaveId = 1;
ushort startAddress = 40001;
ushort numRegisters = 80;
// read five registers
IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(sp);
ushort[] registers = master.ReadHoldingRegisters(1, 0, 80);
for (int i = 0; i < numRegisters; i++)
{
string temp = "Register[" + (startAddress + i) + "]=" + registers[i];
if (i == 36)
label10.Text = registers[i].ToString();
listBox1.Items.Add(temp);
}
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
button5_Click(source, e);
label11.Text = sp.IsOpen.ToString();
// Update();
}
public Form1()
{
InitializeComponent();
//if (sp.IsOpen)
//{
// sp.Close();
//}
//Update();
sp.BaudRate = int.Parse(txtbaudrate.Text);
sp.DataBits = 8;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.Open();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 1000;
aTimer.Enabled = true;
}
ushort i = 1;
int j = 1;
bool flag = false;
private void button1_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
try
{
flag = true;
aTimer.Stop();
j++;
label11.Text = sp.IsOpen.ToString();
sp.Close();
sp.Open();
sp.BaudRate = int.Parse(txtbaudrate.Text);
sp.DataBits = 8;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
ushort startAddress = 40036;
ushort numRegisters = 80;
byte slaveId = 1;
ushort[] registers;
if (j % 2 != 0)
{
registers = new ushort[] { 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 };
}
else
{
registers = new ushort[] { 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500 };
}
// read five registers
IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(sp);
//ushort[] registers = master.ReadHoldingRegisters(1, 0, 5);
master.WriteMultipleRegisters(1, 36, registers);
//for (int i = 0; i < numRegisters; i++)
//{
// string temp = "Register[" + (startAddress + i) + "]=" + registers[i];
// listBox1.Items.Add(temp);
//}
sp.Close();
sp.Open();
aTimer.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
sp.Close();
}
}
}
private void button5_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
Update();
}