我对 rs232 和 Eurotherme 2208e 有问题,这是没有问题的代码。
public Window8()
{
InitializeComponent();
DispatcherTimer myDispatcherTimer = new DispatcherTimer();
myDispatcherTimer.Interval = new TimeSpan(0, 0, 1); // 100 Milliseconds
myDispatcherTimer.Tick += myDispatcherTimer_Tick;
myDispatcherTimer.Start();
}
void myDispatcherTimer_Tick(object sender, EventArgs e)
{
textBlock1.Text = DateTime.Now.ToString("HH:mm:ss");
}
public void port_DataReceived(object o, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
time_out.Stop();
msg_recu += port.ReadExisting();
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new recevoir_del(recevoir), oven_index);
}catch(UnauthorizedAccessException ex)
{
MessageBox.Show(ex.Message,"",MessageBoxButton.OK,MessageBoxImage.Error);
}
}
public delegate void ask_mesure_del(four oven);
public void ask_mesure(four new_etuve)
{
try{
msg_recu = "";
if (new_etuve.regulateur=="EUROTHERM") {
port.StopBits = StopBits.One;
demande_pv_E[1] = demande_pv_E[2] = (byte)new_etuve.gid.ToString().ElementAt(0);
demande_pv_E[3] = demande_pv_E[4] = (byte)new_etuve.uid.ToString().ElementAt(0);
port.Write(demande_pv_E, 0, demande_pv_E.Length);
}
time_out.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "askmesure", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
这是调用指令:
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new ask_mesure_del(ask_mesure), Oven_Class.list_Oven.ElementAt(oven_index));
当我将此块添加到函数 ask_mesure 时:
public delegate void ask_mesure_del(four oven);
public void ask_mesure(four new_etuve)
{
try{
msg_recu = "";
if (new_etuve.regulateur=="EUROTHERM") {
port.StopBits = StopBits.One;
demande_pv_E[1] = demande_pv_E[2] = (byte)new_etuve.gid.ToString().ElementAt(0);
demande_pv_E[3] = demande_pv_E[4] = (byte)new_etuve.uid.ToString().ElementAt(0);
port.Write(demande_pv_E, 0, demande_pv_E.Length);
}
if(flago == 1){
port.StopBits = StopBits.One;
ecriture_sl_h_E[1] = ecriture_sl_h_E[2] = (byte)new_etuve.gid.ToString().ElementAt(0);
ecriture_sl_h_E[3] = ecriture_sl_h_E[4] = (byte)new_etuve.uid.ToString().ElementAt(0);
for (int i = 0; i < new_etuve.consigne.ToString().Length; i++)
{
ecriture_sl_h_E[10 - i] = (byte)new_etuve.consigne.ToString().ElementAt(new_etuve.consigne.ToString().Length - 1 - i);
}
ecriture_sl_h_E[ecriture_sl_h_E.Length - 1] = bcc(ecriture_sl_h_E);
port.Write(ecriture_sl_h_E, 0, ecriture_sl_h_E.Length);
}
time_out.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "askmesure", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
端口通信 2 分钟,连接失败 3 分钟后连接返回