c#与三菱PLC进行串口通信
public void Button2Click(object sender, System.EventArgs e)//启动按钮m0.0{
char[]c={(char)2,(char)49,(char)48,(char)49,(char)48,(char)48,(char)48,(char)49,(char)48,(char)49,(char)3,(char)66,(char)55};
//char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 };
int i=0;
string s="";
for (i=0;i<13;i++)
{
s=s+c[i].ToString();
}
serialPort1.Write(s);
ghu ();
}
public void Button5Click(object sender, EventArgs e)//停止按钮m0.1
{
char[]c={(char)2,(char)49,(char)48,(char)49,(char)48,(char)48,(char)48,(char)49,(char)48,(char)50,(char)3,(char)66,(char)56};
//char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 };
int i=0;
string s="";
for (i=0;i<13;i++)
{
s=s+c[i].ToString();
}
serialPort1.Write(s);
ghu();
}
public void ghu()//读取y的状态
{
char[]c={(char)2,(char)48,(char)48,(char)48,(char)65,(char)48,(char)48,(char)49,(char)3,(char)57,(char)53};
//char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 };
int i=0;
string s="";
for (i=0;i<11;i++)
{
s=s+c[i].ToString();
}
serialPort1.Write(s);
timer2.Enabled=true;
}
void Timer2Tick(object sender, EventArgs e)
{
string data1="";
bool tr;
SolidBrush bush3 = new SolidBrush(Color.Red);
SolidBrush bush4 = new SolidBrush(Color.Green);
Graphics g9 = this.pictureBox9.CreateGraphics();
Graphics g10 = this.pictureBox10.CreateGraphics();
data1=serialPort1.ReadExisting();
if (data1 != null)
{
int b = data1.Length;
if (b>0)
{
byte [] h=strToToHexByte(data1.Substring(1,2));//提取有用字符串转化成十进制数组
byte [] p=strToToHexByte(data1.Substring(3,2));
int oy =Convert.ToInt32(h [0]);//将十进制数组转换成整形整数
int oj=Convert.ToInt32(p [0]);
string eu=Convert.ToString(oy, 2);//将整数转化成二进制
eu =Fill(eu);
//string er1=Parent.ToSByte(er);
string ru= Convert.ToString(oj, 2);
ru =Fill(ru);
if (eu!=""&&ru!="")
{
tr=true;
}
else{
tr=false;
MessageBox.Show("y的状态读取不成功");
}
if (tr==true )
{
int? sum =null;
bool ?res=null;
for (int l = 0 ;l <eu.Length; l++)
{
sum =Convert.ToUInt16(eu.Substring(l,1));
switch (sum)
{
case 0: res=false;
break;
case 1: res=true ;
break ;
}
if(l==8&&res==true)
{
g9.FillEllipse(bush3, 0, 0, 20, 20);
}
else
{
g10.FillEllipse(bush4, 0, 0, 20, 20);
}
if(l==7&&res==true)
{
g10.FillEllipse(bush3, 0, 0, 20, 20);
}
else
{
g10.FillEllipse(bush4, 0, 0, 20, 20);
}
}
}
}
else
{
MessageBox.Show("y的状态读取不成功");
}
}
else{
MessageBox.Show("通信超时");
}
timer2.Enabled=false;
}
}
}
[local]1[/local]
一执行到data1时就跳出对话框了,下面的程序怎么不执行