C# SerialPort 读写三菱FX系列PLC
来源:互联网 发布:ads软件下载 编辑:程序博客网 时间:2024/04/27 18:22
1:串口初始化
com = new SerialPort("COM3", 9600, Parity.Even, 7, StopBits.One);
2:打开关闭串口
if (com.IsOpen)
{com.Close();}
com.Open();
if (com.IsOpen)
{ com.Close();}
3:C# ASCII转字符及字符转ASCII
public static string Chr(int asciiCode)
{
if (asciiCode >= 0 && asciiCode <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)asciiCode };
string strCharacter = asciiEncoding.GetString(byteArray);
return (strCharacter);
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}
public static int Asc(string character)
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
4:写入串口的命令字符串的和校验
/// <summary>
/// 和校验
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public string SumCheck(string data)
{
int sum = 0;
for (int i = 0; i < data.Length; i++)
{
sum += Asc(data.Substring(i, 1));
}
string res = sum.ToString("X");
res = res.Substring(res.Length - 2, 2);
return res;
}
5:写入PLC
private void btnWrite_Click(object sender, EventArgs e)
{
string[] write = new string[] { "2","2"}; //将准备写入PLC的值
//将要写入的值转换成16进制数,补齐两个字节,注意高低字节需要交换
string sWriteData = "";
for (int i = 0; i < write.Length; i++)
{
int val = Convert.ToInt32(write[i].Length>0?write[i]:"0");
string s = val.ToString("X");
while (s.Length<4)
{
s = "0" + s;
}
sWriteData += s.Substring(2,2)+s.Substring(0,2);
}
MessageBox.Show(sWriteData);
//写入命令,1表示写入,1194表示D202这个地址的16进制,04表示D202,D203为4个BYTE,1194=(202*2)+4096的16进制数,至于用它表示D202的起始位置,三菱故意要这么麻烦了.
sWriteData = "1119404" + sWriteData + Chr(3);
//chr(2)和chr(3)是构成命令的标志字符,然后加上校验和,命令组织完成
sWriteData = Chr(2) + sWriteData + SumCheck(sWriteData);
MessageBox.Show(sWriteData);
//写入串口
com.Write(sWriteData);
//byte[] data = Encoding.ASCII.GetBytes(sWriteData);
//com.Write(data,0,data.Length);
}
6:读PLC
private void btnRead_Click(object sender, EventArgs e)
{
this.txtRead0.Clear();
string sReadData = "";
//在读PLC中的数据之前,需要先发个指令给它,让它将数据发送到串口,下面的字符串中,chr(2),chr(3)为PLC命令的格式标志,0119404中,0表示读,1194表示D202的起始地址,04表示读D202,D203两个字,共4个字节,66为0119404和chr(3)的校验和,向串口写入"读"命令,其实和向plc地址中写入数据是一样的,只是没有数据,用0表示读
string sReadCmd = Chr(2) + "0119404" + Chr(3) + "66";
com.Write(sReadCmd);
//等待1秒钟
System.Threading.Thread.Sleep(1000);
// 从串口读数据
byte[] data = new byte[1024];
com.Read(data, 0, 1024);
//如果首位为2,则表示数据有效.这里有个问题,在第二次读,第2位才为'2',第三次又是首位为2,需要再测试
if (data[0]==2)
{
string sReceiveData = System.Text.Encoding.ASCII.GetString(data);
//MessageBox.Show(sReceiveData);
//解析命令,将读到的字符解析成数字,注意高低位的转换
for (int i = 1; i < 8; i += 4)
{
string sLow = sReceiveData.Substring(i,2);
string sHigh = sReceiveData.Substring(i + 2, 2);
//int res = Convert.ToInt32(sHigh)+ Convert.ToInt32(sLow);
int res = Convert.ToInt32(sHigh,16) + Convert.ToInt32(sLow,16);
this.txtRead0.Text += res.ToString() + ",";
}
}
- C# SerialPort 读写三菱FX系列PLC
- C# SerialPort 读写三菱FX系列PLC
- C# SerialPort 读写三菱FX系列PLC
- 三菱FX 系列PLC 编程通信协议总览
- 三菱FX系列PLC的modbusTCP通讯
- 三菱PLC FX系列编程口通信的若干问题
- 三菱FX系列PLC编程口通信协议总览
- 三菱FX系列PLC编程口通信协议举例
- 三菱FX系列PLC编程口通信协议举例
- 三菱Fx系列PLC的编程口协议
- 三菱FX系列PLC编程口通信协议举例
- 三菱FX系列PLC编程口通信协议总览
- 三菱FX系列PLC编程口通信协议举例
- 易控INSPEC软件与三菱FX系列PLC以太网通讯
- PC与三菱FX系列PLC通信实例
- 三菱A系列PLC
- 三菱FX系列plc编程软件SWOPC-FXGP/WIN-C中文版下载
- 三菱FX系列PLC教学课程---一个从基础讲起的电梯控制实例
- 通过ISDN拨号实现连接
- 深银色心扉之Silverlight-MMORPG游戏引擎第一阶段移植
- 通过CABLE MODEM连接Internet
- kuaji
- 通过ADSL连接Internet
- C# SerialPort 读写三菱FX系列PLC
- 使用Internet连接共享使局域网连接到Internet
- 软件生存周期模型总结
- 64位Linux上安装Memcached详细步骤
- 使用SyGate代理服务器使局域网连接到Internet
- VS2005中如何使用QueryPerformanceFrequency()
- 使用WinGate代理服务器使局域网连接到Internet
- 讨论:Java编程风格的改变
- Desktop Java