byte[] dd = { 0x10, 0x02, 0x00, 0x5C, 0x5E, 0x16 };
// serialPort1.Write(dd, 0, dd.Length);
axMSComm1.Output = dd;
System.Threading.Thread.Sleep(100);
string q = q8 + q7 + q6 + q5 + q4+ q3 + q2+ q1;
int dataer = Convert.ToInt32(q, 2);//二进制转十进制
string datah = Convert.ToString(dataer, 16);//十进制转十六进制
while (datah.Length < 2) datah = "0"+datah;
string temp = "02007C320100000000000E00050501120A1002000100008200000000040008"+datah;
int sum = 0;
for (int i = 0; i < (temp.Length / 2); i++)//求校验
{
int c=Convert .ToInt32 (temp.Substring ((i*2),2),16);
sum = sum + c;
}
string check = Convert.ToString(sum, 16);
string checkdata = check.Substring(check.Length - 2, 2);//校验和后两位
//textBox1.Text = checkdata;
checkdata = checkdata.ToUpper();
temp = "68202068"+temp+checkdata+"16" ;
byte[] outdata3 = new byte[38];
for (int i = 0; i < (temp.Length / 2); i++)
{
outdata3[i] = Convert.ToByte((temp.Substring(i * 2, 2)), 16);
}
//serialPort1.Write(outdata3, 0, outdata3.Length);
axMSComm1.Output = outdata3;
2019-12-21 20:01:20
1.28MB
PLC通讯
1