现在的200smart功能很强大,也带以太网通讯,与上位机交换数据更加方便了,以下是我经常使用的通讯范烈分享给大家。
PLC端:
上位机端(VB.NET):
Public Structure _srvPLCPar
Public SrvStart As Boolean
Public SrvStartOK As Boolean
Public SrvExit As Boolean
Public ComOKCount As Long
Public ComLongCycleCount As Long
Public ComErrCount As Long
Public ComCurCycle As Integer
Public ComMinCycle As Integer
Public ComMaxCycle As Integer
End Structure
Public srvPLCPar As _srvPLCPar
'PLC通讯服务
Public Sub srvPLC()
srvPLCPar.SrvStart = True
srvPLCPar.SrvStartOK = False
srvPLCPar.SrvExit = False
srvPLCPar.ComOKCount = 0
srvPLCPar.ComLongCycleCount = 0
srvPLCPar.ComErrCount = 0
srvPLCPar.ComCurCycle = 0
srvPLCPar.ComMinCycle = 100
srvPLCPar.ComMaxCycle = 0
ReDim IQ.I_MB_RXBuf(31)
ReDim IQ.Q_MB_TXBuf(31)
Dim DatLen As Integer = 500
Dim LT As Long = 0, CT As Long = 0
Dim ns As System.Net.Sockets.NetworkStream
Dim tcpc As System.Net.Sockets.TcpClient
Dim tcpl As System.Net.Sockets.TcpListener
Dim TXBuf(DatLen - 1) As Byte, RXBuf(DatLen - 1) As Byte
Dim wdog As Integer = 0
Dim tc As _TypeCvt
Con:
srvPLCPar.SrvStartOK = False
Try
Dim ip() As Byte = {192, 168, 0, 240}
tcpl = New System.Net.Sockets.TcpListener(New Net.IPAddress(ip), 1986)
tcpl.Start()
If tcpc Is Nothing = False Then
tcpc.Close()
tcpc = Nothing
End If
Do Until tcpl.Pending = True
If srvPLCPar.SrvExit Then GoTo FSH
System.Threading.Thread.Sleep(10)
Loop
tcpc = tcpl.AcceptTcpClient
tcpl.Stop()
If ns Is Nothing = False Then
ns.Dispose()
ns = Nothing
End If
ns = tcpc.GetStream
Do Until tcpc.Available = 0
ns.ReadByte()
Loop
Catch ex As Exception
GoTo FSH
End Try
While 1
If srvPLCPar.SrvExit Then GoTo FSH
'************************************************************************************************************************************************************
'数据整合PC->PLC
TXBuf(0) = (IQ.Q_MB_ClrRS << 1) + (IQ.Q_MB_Send << 0)
TXBuf(2) = IQ.Q_MB_SlaveAdr
TXBuf(3) = IQ.Q_MB_SlaveRW
tc.u32 = IQ.Q_MB_SlaveRWAdr
TXBuf(4) = tc.byt3
TXBuf(5) = tc.byt2
TXBuf(6) = tc.byt1
TXBuf(7) = tc.byt0
tc.u32 = 0
tc.u16 = IQ.Q_MB_SlaveRWCount
TXBuf(8) = tc.byt1
TXBuf(9) = tc.byt0
Array.Copy(IQ.Q_MB_TXBuf, 0, TXBuf, 10, IQ.Q_MB_TXBuf.Length)
'************************************************************************************************************************************************************
Try
ns.Write(TXBuf, 0, DatLen)
Catch ex As Exception
GoTo Con
End Try
wdog = 0
Do Until tcpc.Available >= DatLen
If srvPLCPar.SrvExit Then GoTo FSH
Threading.Thread.Sleep(1)
wdog = wdog + 1
If wdog >= 1000 Then
GoTo Con
End If
Loop
Try
ns.Read(RXBuf, 0, DatLen)
Catch ex As Exception
GoTo Con
End Try
'************************************************************************************************************************************************************
'数据解析PLC->PC
IQ.I_MB_RS = RXBuf(0)
IQ.I_MB_ErrCode = RXBuf(1)
Array.Copy(RXBuf, 10, IQ.I_MB_RXBuf, 0, IQ.I_MB_RXBuf.Length)
'************************************************************************************************************************************************************
srvPLCPar.ComOKCount = srvPLCPar.ComOKCount + 1
srvPLCPar.SrvStartOK = True
Threading.Thread.Sleep(5)
End While
FSH:
If tcpc Is Nothing = False Then
tcpc.Close()
tcpc = Nothing
End If
If ns Is Nothing = False Then
ns.Dispose()
ns = Nothing
End If
srvPLCPar.SrvStart = False
srvPLCPar.SrvStartOK = False
srvPLCPar.SrvExit = False
End Sub