西门子S7-200 ModBus从站通讯程序

时间:2023-03-19来源:佚名

一直看到有些朋友在问ModBus主从站的通讯问题,今天抽空儿给大家发几篇。

事先申明,我是调用的主从站库,库本身并不是我编写的,我没有那么大的能耐。

另外,上位机是用VB编写,VB本身不是很熟,如果有什么错误,请大家海谅。

最后,这仅仅只是一个测试程序,如果各位想用到商业方面,还需要各位自己去完善。

说明:plc从站,PC作主站,用ModBus协议进行通讯。PC机读取PLCVW区的数据;PC机还读取PLC内部I或者O地址的状态。

///////////////////////////////////////

以下是PLC程序实现,方便贴出,已转换为STL

TITLE=程序注释

Network 1 // 网络标题

// 在第一个循环周期内初始化Modbus从站协议

LD SM0.1

CALL SBR3, 1, 12, 9600, 0, 0, 128, 32, 1000, &VB0, M10.1, MB11

Network 2

// 在每个循环周期内执行Modbus 从站协议

LD SM0.0

CALL SBR1, M10.2, MB12

Network 3

LD SM0.0

MOVW AIW0, VW100

/I 54, VW100

MOVW AIW2, VW102

/I 54, VW102

MOVW AIW4, VW104

/I 54, VW104

MOVW AIW6, VW106

/I 54, VW106

Network 4

LD SM0.0

= Q0.0

Network 5

LD I0.0

= Q0.1

Network 6

LD I0.1

= Q0.2

///////////////////////////////////

以下是VB源码

Option Explicit

Private Declare Function GetTickCount Lib "kernel32" () As Long

Dim x1

Dim p11, p12, p13, p14, p15, p16, p17, p18

Function CRC16(data() As Byte) As String

Dim CRC16Lo As Byte, CRC16Hi As Byte 'CRC寄存器

Dim CL As Byte, CH As Byte '多项式码&HA001

Dim SaveHi As Byte, SaveLo As Byte

Dim i As Integer

Dim Flag As Integer

CRC16Lo = &HFF

CRC16Hi = &HFF

CL = &H1

CH = &HA0

For i = 0 To UBound(data)

CRC16Lo = CRC16Lo Xor data(i) '每一个数据与CRC寄存器进行异或

For Flag = 0 To 7

SaveHi = CRC16Hi

SaveLo = CRC16Lo

CRC16Hi = CRC16Hi 2 '高位右移一位

CRC16Lo = CRC16Lo 2 '低位右移一位

If ((SaveHi And &H1) = &H1) Then '如果高位字节最后一位为1

CRC16Lo = CRC16Lo Or &H80 '则低位字节右移后前面补1

End If '否则自动补0

If ((SaveLo And &H1) = &H1) Then '如果LSB为1,则与多项式码进行异或

CRC16Hi = CRC16Hi Xor CH

CRC16Lo = CRC16Lo Xor CL

End If

Next Flag

Next i

Dim ReturnData(1) As Byte

ReturnData(0) = CRC16Hi 'CRC高位

ReturnData(1) = CRC16Lo 'CRC低位

CRC16 = ReturnData

End Function

Private Sub About_Click()

frmAbout.Show

End Sub

Private Sub Form_Load() '初始化

Timer1.Enabled = False '定时器1无效

Timer1.Interval = 1000 '定时器1时间为1S

Timer2.Enabled = True '定时器2有效

Timer2.Interval = 1000 '定时器2时间为1S

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

Text4.Text = ""

Text5.Text = ""

Text6.Text = ""

Text8.Text = ""

Text10.Text = ""

MSComm1.CommPort = 1 '设定端口号

MSComm1.Settings = "9600,n,8,1" '设定通讯波特率

MSComm1.InBufferSize = 1024 '接收缓冲器大小

MSComm1.OutBufferSize = 1024 '输出缓冲器大小

MSComm1.InputMode = comInputModeBinary '以二进制传输

MSComm1.RThreshold = 1 '

MSComm1.SThreshold = 0

MSComm1.InputLen = 0 '读取接收缓冲器所有字符

MSComm1.OutBufferCount = 0 '清空发送缓冲区

MSComm1.InBufferCount = 0 '清空接收缓冲区

If MSComm1.PortOpen = False Then

Command3.Caption = "打开串口"

Else

Command3.Caption = "关闭串口"

End If

End Sub

Private Sub Command1_Click()

Timer1.Enabled = True

End Sub

Private Sub Command2_Click() '退出程序,定时器1无效

Timer1.Enabled = False

Cls

Unload Me

End Sub

Private Sub Command3_Click()

On Error Resume Next

If MSComm1.PortOpen = False Then

MSComm1.PortOpen = True

Else

MSComm1.PortOpen = False

End If

If MSComm1.PortOpen Then '打开关闭按钮显示文字

Command3.Caption = "关闭串口"

Else

Command3.Caption = "打开串口"

End If

If Err Then '打开串口失败,则显示出错信息

MsgBox Error$, 48, "错误信息"

Exit Sub

End If

End Sub

Private Sub Timer1_Timer() '读V存储区数据

Dim CRC() As Byte

Dim FGetData As String

Dim aa() As Byte

Dim s As String

Dim str As String

Dim i As Integer

Dim bb, cc As String

ReDim aa(5) As Byte '定义动态数组

aa(0) = &HC

aa(1) = &H3

aa(2) = &H0

aa(3) = &H32

aa(4) = &H0

aa(5) = &H4

CRC = CRC16(aa)

str = CRC

s = ""

For i = 1 To LenB(str)

s = s Hex(AscB(MidB(str, i, 1)))

Next i

bb = Right(s, 2)

cc = Mid(s, 1, 2)

If Len(s) < 4 Then

cc = Mid(s, 1, 1)

End If

ReDim Preserve aa(0 To 7) As Byte

aa(6) = Val("&H" & bb)

aa(7) = Val("&H" & cc)

MSComm1.OutBufferCount = 0 '清空输出寄存器

MSComm1.Output = aa

FGetData = ReceiveData

Text5.Text = FGetData

p11 = Val("&H" & Mid(FGetData, 7, 4))

p12 = Val("&H" & Mid(FGetData, 11, 4))

p13 = Val("&H" & Mid(FGetData, 15, 4))

p14 = Val("&H" & Mid(FGetData, 19, 4))

End Sub

Private Sub Command4_Click() 'I状态

Dim CRC() As Byte

Dim FGetData As String

Dim aa() As Byte

Dim s As String

Dim str As String

Dim i As Integer

Dim bb, cc As String

ReDim aa(5) As Byte '定义动态数组

aa(0) = &HC

aa(1) = &H2

aa(2) = &H0

aa(3) = &H0

aa(4) = &H0

aa(5) = &H1

CRC = CRC16(aa)

str = CRC

s = ""

For i = 1 To LenB(str)

s = s Hex(AscB(MidB(str, i, 1)))

Next i

bb = Right(s, 2)

cc = Mid(s, 1, 2)

If Len(s) < 4 Then

cc = Mid(s, 1, 1)

End If

ReDim Preserve aa(0 To 7) As Byte

aa(6) = Val("&H" & bb)

aa(7) = Val("&H" & cc)

MSComm1.OutBufferCount = 0 '清空输出寄存器

MSComm1.Output = aa

FGetData = IReceiveData

p17 = Val("&H" & Mid(FGetData, 7, 2))

p18 = Val("&H" & Mid(FGetData, 9, 2))

End Sub

Private Sub Command5_Click() 'Q状态

Dim CRC() As Byte

Dim FGetData As String

Dim aa() As Byte

Dim s As String

Dim str As String

Dim i As Integer

Dim bb, cc As String

ReDim aa(5) As Byte '定义动态数组

aa(0) = &HC

aa(1) = &H1

aa(2) = &H0

aa(3) = &H0

aa(4) = &H0

aa(5) = &H1

CRC = CRC16(aa)

str = CRC

s = ""

For i = 1 To LenB(str)

s = s Hex(AscB(MidB(str, i, 1)))

Next i

bb = Right(s, 2)

cc = Mid(s, 1, 2)

If Len(s) < 4 Then

cc = Mid(s, 1, 1)

End If

ReDim Preserve aa(0 To 7) As Byte

aa(6) = Val("&H" & bb)

aa(7) = Val("&H" & cc)

MSComm1.OutBufferCount = 0 '清空输出寄存器

MSComm1.Output = aa

FGetData = QReceiveData

p15 = Val("&H" & Mid(FGetData, 7, 2))

p16 = Val("&H" & Mid(FGetData, 9, 2))

End Sub

Private Function ReceiveData() As String '返回V存储器区数据

Dim FGetData As String

Dim t1 As Long

Dim av As Variant

Dim i As Integer

Dim ReDataLen As Integer

FGetData = ""

t1 = GetTickCount() '取时间,做延时用

Do '循环等待接收数据

DoEvents

If MSComm1.InBufferCount > 0 Then '串口有数据了

ReDataLen = MSComm1.InBufferCount '取数据长度

av = MSComm1.Input '将串口数据取出来

For i = 0 To ReDataLen - 1

FGetData = FGetData & Right("00" & Hex(av(i)), 2)

Next i

End If

If Len(FGetData) >= 6 Then

If Len(FGetData) > Val("&H" & Mid(FGetData, 5, 2)) * 2 8 Then

ReceiveData = FGetData

Exit Function

End If

End If

If GetTickCount - t1 > 2000 Then '2秒没收完就不收了

ReceiveData = ""

Exit Function

End If

Loop

End Function

Private Function IReceiveData() As String '反回I状态

Dim FGetData As String

Dim t1 As Long

Dim av As Variant

Dim i As Integer

Dim ReDataLen As Integer

FGetData = ""

t1 = GetTickCount() '取时间,做延时用

Do '循环等待接收数据

DoEvents

If MSComm1.InBufferCount > 0 Then '串口有数据了

ReDataLen = MSComm1.InBufferCount '取数据长度

av = MSComm1.Input '将串口数据取出来

For i = 0 To ReDataLen - 1

FGetData = FGetData & Right("00" & Hex(av(i)), 2)

Next i

End If

If Len(FGetData) >= 6 Then

If Len(FGetData) > Val("&H" & Mid(FGetData, 5, 2)) * 2 8 Then

IReceiveData = FGetData

Exit Function

End If

End If

If GetTickCount - t1 > 2000 Then '2秒没收完就不收了

IReceiveData = ""

Exit Function

End If

Loop

End Function

Private Function QReceiveData() As String '反回Q状态

Dim FGetData As String

Dim t1 As Long

Dim av As Variant

Dim i As Integer

Dim ReDataLen As Integer

FGetData = ""

t1 = GetTickCount() '取时间,做延时用

Do '循环等待接收数据

DoEvents

If MSComm1.InBufferCount > 0 Then '串口有数据了

ReDataLen = MSComm1.InBufferCount '取数据长度

av = MSComm1.Input '将串口数据取出来

For i = 0 To ReDataLen - 1

FGetData = FGetData & Right("00" & Hex(av(i)), 2)

Next i

End If

If Len(FGetData) >= 6 Then

If Len(FGetData) > Val("&H" & Mid(FGetData, 5, 2)) * 2 8 Then

QReceiveData = FGetData

Exit Function

End If

End If

If GetTickCount - t1 > 2000 Then '2秒没收完就不收了

QReceiveData = ""

Exit Function

End If

Loop

End Function

Private Sub Timer2_Timer()

x1 = x1 1

Text10.Text = str(x1)

Text1.Text = str(p11)

Text2.Text = str(p12)

Text3.Text = str(p13)

Text4.Text = str(p14)

Text7.Text = str(p15)

If Text7.Text = 1 Then Text7.BackColor = RGB(255, 0, 255)

If Text7.Text = 0 Then Text7.BackColor = RGB(0, 255, 255)

'Text6.Text = str(p16)

Text9.Text = str(p17)

If Text9.Text = 1 Then Text9.BackColor = RGB(255, 0, 255)

If Text9.Text = 0 Then Text9.BackColor = RGB(0, 255, 255)

'Text8.Text = str(p18)

End Sub

    相关阅读

    软启动器12个故障代码

    软启动器常见故障问题的故障代码,参考如下: 1、故障代码Err01 故障类容:→断相;说明:主电路任意一相断相。 2、故障代码Err02 故障类容:→过电流;说明:超过额定电流12倍。 3、故...
    2022-11-26

    电焊机电流调到多少合适,根据焊条大小及焊接对象做调整

    2.5焊条电焊机电流大小应根据焊条的种类来定: 1、一般碳钢焊条焊接电流调到100A左右,不锈钢焊条稍低点。 2、焊方管2.5焊条电流: 60-80A电焊机利用正负两极在瞬间短路时产生的高温电...
    2023-01-09
    电焊机电流调到多少合适,根据焊条大小及焊接对象做调整

    电动车电池看参数的三种方法,轻松查看电动车电池型号

    新买的电动车电池是什么型号的,很多人并不是很清楚,而在需要换电池时搞不清楚型号就会很麻烦,那么电动车电池规格怎么,怎么来识别电动车电池的型号,与电工天下小编一起来...
    2022-11-30

    电动机的启动电流如何计算,电动机启动电流计算公式

    有关电动机启动电流的计算方法,三相电机启动电流瞬时是正常的多少倍,电动机的启动电流是额定电流 的多少,电动机 启动电流计算公式与实例,感兴趣的朋友参考下。 电动机启动...
    2022-11-22
    电动机的启动电流如何计算,电动机启动电流计算公式

    电焊机怎么调,电流与电压调整方法

    电焊机的调整方法,具体参考如下: 1、先把电流旋钮调到最小,把电压旋钮调到最大,试焊一下,不要动电压旋钮,逐步调大电流,直到能正常焊接时停上。 2、反过来,把电流旋钮先...
    2023-01-09
    电焊机怎么调,电流与电压调整方法

    【电力用户怎么分级,电力用户分类标准】

    【电力用户怎么分级,电力用户分类标准】 有网友咨询,电力用户怎么分级,针对分级的问题,电力用户分为一级用户至三级用户,那么电力用户分类标准是怎么样的,电力客户分级和...
    2022-12-10
    【电力用户怎么分级,电力用户分类标准】

    插座发出滋滋滋的声音是什么原因

    首先最有可能的原因就是电源线与插座接线端子接触不牢固,而造成的虚接,进而产生电弧也就是我们俗称的电火花,就会发出的滋滋声音。 插座内由于质量问题,电源接线柱与插头触...
    2022-11-10
    插座发出滋滋滋的声音是什么原因

    异步电动机正反转控制plc接线图及梯形图(图文)

    【 异步电动机 正反转控制plc 接线图及梯形图 】 用plc实现异步电动机的正反转控制,如何设计控制线路图,如何分配i/o,都是绘制plc接线图的关键,另外异步电动机正反转控制设计 过...
    2022-12-11
    异步电动机正反转控制plc接线图及梯形图(图文)

    网站栏目