|
本帖最后由 zhengqiqi 于 2021-6-15 22:45 编辑
本成程序基于VS2008编写,目前已经打通S7通讯协议,可以成功PLC读取数据,属于半成品,主要面对自已开发OEM机器与设备。
本程序为DOME代码,集成S7通讯协议,在WinCE成功与S71200通讯,有兴趣的朋友可以一起研究。
class MsgSocket
{
private Socket TCPSocket;
private int _ReadTimeout = 2000;
private int _WriteTimeout = 2000;
private int _ConnectTimeout = 1000;
public int LastError = 0;
public MsgSocket()
{
}
~MsgSocket()
{
Close();
}
public void Close()
{
if (TCPSocket != null)
{
//TCPSocket.Dispose();
TCPSocket = null;
}
}
private void CreateSocket()
{
TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//TCPSocket.NoDelay = true;
}
private void TCPPing(string Host, int Port)
{
// To Ping the PLC an Asynchronous socket is used rather then an ICMP packet.
// This allows the use also across Internet and Firewalls (obviously the port must be opened)
LastError = 0;
Socket PingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
//
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(Host), Port);
//
IAsyncResult result = PingSocket.BeginConnect(ipe, null, null);
//bool success = result.AsyncWaitHandle.WaitOne(_ConnectTimeout, true);
//if (!success)
//{
// LastError = S7Consts.errTCPConnectionFailed;
//}
}
catch
{
LastError = S7Consts.errTCPConnectionFailed;
};
PingSocket.Close();
}
public int Connect(string Host, int Port)
{
LastError = 0;
if (!Connected)
{
TCPPing(Host, Port);
if (LastError == 0)
try
{
CreateSocket();
//
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(Host), Port);
//
TCPSocket.Connect(ipe);
}
catch
{
LastError = S7Consts.errTCPConnectionFailed;
}
}
return LastError;
}
private int WaitForData(int Size, int Timeout)
{
bool Expired = false;
int SizeAvail;
int Elapsed = Environment.TickCount;
LastError = 0;
try
{
SizeAvail = TCPSocket.Available;
while ((SizeAvail < Size) && (!Expired))
{
Thread.Sleep(2);
SizeAvail = TCPSocket.Available;
Expired = Environment.TickCount - Elapsed > Timeout;
// If timeout we clean the buffer
if (Expired && (SizeAvail > 0))
try
{
byte[] Flush = new byte[SizeAvail];
TCPSocket.Receive(Flush, 0, SizeAvail, SocketFlags.None);
}
catch { }
}
}
catch
{
LastError = S7Consts.errTCPDataReceive;
}
if (Expired)
{
LastError = S7Consts.errTCPDataReceive;
}
return LastError;
}
public int Receive(byte[] Buffer, int Start, int Size)
{
int BytesRead = 0;
LastError = WaitForData(Size, _ReadTimeout);
if (LastError == 0)
{
try
{
BytesRead = TCPSocket.Receive(Buffer, Start, Size, SocketFlags.None);
}
catch
{
LastError = S7Consts.errTCPDataReceive;
}
if (BytesRead == 0) // Connection Reset by the peer
{
LastError = S7Consts.errTCPDataReceive;
Close();
}
}
return LastError;
}
public int Send(byte[] Buffer, int Size)
{
LastError = 0;
try
{
int BytesSent = TCPSocket.Send(Buffer, Size, SocketFlags.None);
}
catch
{
LastError = S7Consts.errTCPDataSend;
Close();
}
return LastError;
}
public bool Connected
{
get
{
return (TCPSocket != null) && (TCPSocket.Connected);
}
}
public int ReadTimeout
{
get
{
return _ReadTimeout;
}
set
{
_ReadTimeout = value;
}
}
public int WriteTimeout
{
get
{
return _WriteTimeout;
}
set
{
_WriteTimeout = value;
}
}
public int ConnectTimeout
{
get
{
return _ConnectTimeout;
}
set
{
_ConnectTimeout = value;
}
}
}
#endif
|
|