using Hik.Api; using S7.Net; using System.Threading.Channels; using System.Windows.Forms; namespace xicheji { public partial class MainForm : Form { private Plc? plc = null; private HikApi? hikApi = null; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { try { } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } } private void button1_Click(object sender, EventArgs e) { try { ConnectCamera(); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } } private void button2_Click(object sender, EventArgs e) { if (plc == null || !plc.IsConnected) { Log("连接成功!"); return; } } private void button3_Click(object sender, EventArgs e) { plc?.Close(); } private void button4_Click(object sender, EventArgs e) { try { } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace); } } private void nowTimer_Tick(object sender, EventArgs e) { try { var now = DateTime.Now; lblTime.Text = $"{now.Year}年 {now.Month}月 {now.Day}日 {now.Hour}:{now.Minute}:{now.Second}"; } catch { } } private void receivePLCTimer_Tick(object sender, EventArgs e) { try { if (plc == null || !plc.IsConnected) { receivePLCTimer.Enabled = false; return; } } catch { } } private void uploadTimer_Tick(object sender, EventArgs e) { } private void btnSetting_Click(object sender, EventArgs e) { try { var settingForm = new SettingForm(); if (settingForm.ShowDialog() == DialogResult.OK) { ConnectPLC(); ConnectCamera(); receivePLCTimer.Enabled = true; uploadTimer.Enabled = true; } } catch { } } private void ConnectPLC() { plc = new Plc(CpuType.S7200, "127.0.0.1", 1502, 0, 1); plc.Open(); if (plc == null || !plc.IsConnected) { MessageBox.Show("PLC连接失败!"); return; } } private void ConnectCamera() { hikApi = HikApi.Login("192.168.1.64", 8000, "admin", "password") as HikApi; if (hikApi == null || !hikApi.Connected) { MessageBox.Show("摄像头连接失败!"); return; } hikApi.PlaybackService.StartPlayBack(hikApi.DefaultIpChannel, pictureBox1.Handle); } private void Log(params object[] msgs) { //richTextBox1.AppendText(string.Join(" ", msgs)); //richTextBox1.AppendText("\n\n"); //richTextBox1.ScrollToCaret(); Console.WriteLine(string.Join(" ", msgs)); } } }