MainForm.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Hik.Api;
  2. using S7.Net;
  3. using System.Threading.Channels;
  4. using System.Windows.Forms;
  5. namespace xicheji
  6. {
  7. public partial class MainForm : Form
  8. {
  9. private Plc? plc = null;
  10. private HikApi? hikApi = null;
  11. public MainForm()
  12. {
  13. InitializeComponent();
  14. }
  15. private void MainForm_Load(object sender, EventArgs e)
  16. {
  17. try
  18. {
  19. }
  20. catch (Exception ex)
  21. {
  22. MessageBox.Show(ex.Message + ex.StackTrace);
  23. }
  24. }
  25. private void button1_Click(object sender, EventArgs e)
  26. {
  27. try
  28. {
  29. ConnectCamera();
  30. }
  31. catch (Exception ex)
  32. {
  33. MessageBox.Show(ex.Message + ex.StackTrace);
  34. }
  35. }
  36. private void button2_Click(object sender, EventArgs e)
  37. {
  38. if (plc == null || !plc.IsConnected)
  39. {
  40. Log("连接成功!");
  41. return;
  42. }
  43. }
  44. private void button3_Click(object sender, EventArgs e)
  45. {
  46. plc?.Close();
  47. }
  48. private void button4_Click(object sender, EventArgs e)
  49. {
  50. try
  51. {
  52. }
  53. catch (Exception ex)
  54. {
  55. MessageBox.Show(ex.Message + ex.StackTrace);
  56. }
  57. }
  58. private void nowTimer_Tick(object sender, EventArgs e)
  59. {
  60. try
  61. {
  62. var now = DateTime.Now;
  63. lblTime.Text = $"{now.Year}年 {now.Month}月 {now.Day}日 {now.Hour}:{now.Minute}:{now.Second}";
  64. }
  65. catch { }
  66. }
  67. private void receivePLCTimer_Tick(object sender, EventArgs e)
  68. {
  69. try
  70. {
  71. if (plc == null || !plc.IsConnected)
  72. {
  73. receivePLCTimer.Enabled = false;
  74. return;
  75. }
  76. }
  77. catch { }
  78. }
  79. private void uploadTimer_Tick(object sender, EventArgs e)
  80. {
  81. }
  82. private void btnSetting_Click(object sender, EventArgs e)
  83. {
  84. try
  85. {
  86. var settingForm = new SettingForm();
  87. if (settingForm.ShowDialog() == DialogResult.OK)
  88. {
  89. ConnectPLC();
  90. ConnectCamera();
  91. receivePLCTimer.Enabled = true;
  92. uploadTimer.Enabled = true;
  93. }
  94. }
  95. catch { }
  96. }
  97. private void ConnectPLC()
  98. {
  99. plc = new Plc(CpuType.S7200, "127.0.0.1", 1502, 0, 1);
  100. plc.Open();
  101. if (plc == null || !plc.IsConnected)
  102. {
  103. MessageBox.Show("PLC连接失败!");
  104. return;
  105. }
  106. }
  107. private void ConnectCamera()
  108. {
  109. hikApi = HikApi.Login("192.168.1.64", 8000, "admin", "password") as HikApi;
  110. if (hikApi == null || !hikApi.Connected)
  111. {
  112. MessageBox.Show("摄像头连接失败!");
  113. return;
  114. }
  115. hikApi.PlaybackService.StartPlayBack(hikApi.DefaultIpChannel, pictureBox1.Handle);
  116. }
  117. private void Log(params object[] msgs)
  118. {
  119. //richTextBox1.AppendText(string.Join(" ", msgs));
  120. //richTextBox1.AppendText("\n\n");
  121. //richTextBox1.ScrollToCaret();
  122. Console.WriteLine(string.Join(" ", msgs));
  123. }
  124. }
  125. }