winform画面
源代码
namespace C_通过串口与FX3U通讯
{
public partial class formMain : Form
{
public formMain()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
ActFXCPU fxCPU = new ActFXCPU();
/// <summary>
/// 打开通讯连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Conn_Click(object sender, EventArgs e)
{
string cpu;
int type;
int n;
if (cb_Port.Text.Length > 0)
{
string port = cb_Port.Text;
char sp = 'M';
string[] spm = new string[4];
spm = port.Split(sp);
fxCPU.Close();
fxCPU.ActPortNumber = Convert.ToInt16(spm[1]);
fxCPU.ActBaudRate = Convert.ToInt16(cb_BaudRate.Text);
n = fxCPU.Open();
if (n == 0)
{
lb_connStsTxt.Text = "通讯成功!";
lb_connStsTxt.ForeColor = Color.Green;
fxCPU.GetCpuType(out cpu, out type);
lb_PLCStyleTxt.Text = cpu;
}
else
{
lb_connStsTxt.Text = "通讯失败!";
lb_connStsTxt.ForeColor = Color.Red;
}
}
}
/// <summary>
/// 读取1次X点事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_XReadOnce_Click(object sender, EventArgs e)
{
// readInput();
}
private void readInput()
{
while (true)
{
int[] arrX = new int[10];
int iRetX = fxCPU.ReadDeviceRandom("X0\nX1\nX2\nX3\nX4\nX5\nX6\nX7", 8, out arrX[0]);
//创建存放lable标签的集合
List<Control> lableListX = new List<Control>();
//检索出所有符合名称的标签控件
for (int j = 0; j < 10; j++)
{
//返回值为集合
Control[] conLable = this.Controls.Find("lb_X" + j.ToString(), true);
if (conLable.Count() >= 1)
{
//判断控件是否是标签
if (conLable[0] is Label)
{
lableListX.Add(conLable[0]);
}
}
}
for (int i = 0; i < lableListX.Count; i++)
{
Label lab = lableListX as Label;
lab.BackColor = arrX == 1 ? Color.Green : Color.Red;
}
int[] arrY = new int[10];
int iRetY = fxCPU.ReadDeviceRandom("Y0\nY1\nY2\nY3\nY4\nY5", 6, out arrY[0]);
List<Control> lableListY = new List<Control>();
for (int j = 0; j < 10; j++)
{
Control[] conLable = this.Controls.Find("lb_Y" + j.ToString(), true);
if (conLable.Count() >= 1)
{
if (conLable[0] is Label)
{
lableListY.Add(conLable[0]);
}
}
}
for (int i = 0; i < lableListY.Count; i++)
{
Label lab = lableListY as Label;
lab.BackColor = arrY == 1 ? Color.Green : Color.Red;
}
//读取M点状态
int getM0;
int iRetM = fxCPU.GetDevice("M60",out getM0);
if (getM0 == 1)
{
btn_M60.Text = "M60_ON";
btn_M60.BackColor = Color.Green;
}
else
{
btn_M60.Text = "M60_OFF";
btn_M60.BackColor = Color.Red;
}
//读取测试脉冲
int[] arrD0 = new int[2];
int iRetD0 = fxCPU.ReadDeviceRandom("D0", 1,out arrD0[0]);
textBox1.Text = arrD0[0].ToString();
}
}
/// <summary>
/// 连续读取X点事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_XReadCon_Click(object sender, EventArgs e)
{
//创建一个连续读取委托
ThreadStart myInputReadConn = new ThreadStart(readInput);
//创建一个后台线程
Thread myThread = new Thread(myInputReadConn);
myThread.IsBackground = true;
myThread.Start();
}
private void btn_M60_Click(object sender, EventArgs e)
{
int S;
if (btn_M60.Text=="M60_OFF")
{
S = fxCPU.SetDevice("M60", 1);
}
else
{
S = fxCPU.SetDevice("M60", 0);
}
}
}
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |