上位机控制欧姆龙PLC
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;namespace DXF贴合机触摸屏{ public partial class Form1 : Form { bool isOpen = false; public static string sw1; public Form1() { InitializeComponent(); } private void button5_Click(object sender, EventArgs e) { } private void button6_Click(object sender, EventArgs e) { } private void button13_Click(object sender, EventArgs e) { if (isOpen == false) { try { serialPort1.Open(); isOpen = true; button13.Text = "已连接"; } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { try//打开串口 { serialPort1.Close(); isOpen = false; //isSetProperty = false; button13.Text= "打开串口"; } catch (Exception ex) { MessageBox.Show(ex.Message); } } } private void button1_Click(object sender, EventArgs e) { serialPort1.DiscardOutBuffer(); if (button14.Text == "地址设置中") { Form2 f2 = new Form2(); if (f2.Visible) f2.Hide(); else f2.Show(); } if (button14.Text == "地址设置") { //sw1=Form2.sw; serialPort1.Write(sw1); Thread.Sleep(100); serialPort1.DiscardOutBuffer(); string sw2 = "@00FA0000000000102B10002000001000004*\r\n"; serialPort1.Write(sw2); } // button1.Enabled = false; } private void button14_Click(object sender, EventArgs e) { if (button14.Text =="地址设置") { button14.Text ="地址设置中"; return; } if (button14.Text == "地址设置中") { button14.Text = "地址设置"; return; } } private void Form1_Load(object sender, EventArgs e) { serialPort1.PortName = "COM3"; //串口号 serialPort1.BaudRate = 115200; //波特率115200 serialPort1.DtrEnable = true; //DTR启用 serialPort1.RtsEnable = true; //RTS启用 serialPort1.DataBits = 8; //数据位8 serialPort1.Parity = System.IO.Ports.Parity.None; //无校验 serialPort1.StopBits = System.IO.Ports.StopBits.One; //停止位1 sw1 = SystemConfig.GetConfigData("sw0", string.Empty); } private void button2_Click(object sender, EventArgs e) { serialPort1.DiscardOutBuffer(); if (button14.Text == "地址设置中") { Form3 f3 = new Form3(); if (f3.Visible) f3.Hide(); else f3.Show(); } if (button14.Text == "地址设置") { string sw1 = Form3.tz; serialPort1.Write(sw1); } } }}
评论