123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- 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));
- }
- }
- }
|