久久久久久久久蜜桃精品,激情五月网婷婷69v,日韩国产欧美精品在线,久久精品国产日本波多野结夜

咨詢熱線

18621916738

當(dāng)前位置:首頁(yè)  >  技術(shù)文章  >  西門子S7-200 ModBus從站通訊程序

西門子S7-200 ModBus從站通訊程序

更新時(shí)間:2016-01-04      點(diǎn)擊次數(shù):2059

西門子S7-200 ModBus從站通訊程序

一直看到有些朋友在問(wèn)ModBus主從站的通訊問(wèn)題,今天抽空兒給大家發(fā)幾篇。

事先申明,我是調(diào)用的主從站庫(kù),庫(kù)本身并不是我編寫的,我沒(méi)有那么大的能耐。

另外,上位機(jī)是用VB編寫,VB本身不是很熟,如果有什么錯(cuò)誤,請(qǐng)大家海諒。

zui后,這僅僅只是一個(gè)測(cè)試程序,如果各位想用到商業(yè)方面,還需要各位自己去完善。

說(shuō)明:PLC從站,PC作主站,用ModBus協(xié)議進(jìn)行通訊。PC機(jī)讀取PLCVW區(qū)的數(shù)據(jù);PC機(jī)還讀取PLC內(nèi)部I或者O地址的狀態(tài)。

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

以下是PLC程序?qū)崿F(xiàn),方便貼出,已轉(zhuǎn)換為STL

TITLE=程序注釋

Network 1 // 網(wǎng)絡(luò)標(biāo)題

// 在*個(gè)循環(huán)周期內(nèi)初始化Modbus從站協(xié)議

LD SM0.1

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

Network 2 

// 在每個(gè)循環(huán)周期內(nèi)執(zhí)行Modbus 從站協(xié)議

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 '多項(xiàng)式碼&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) '每一個(gè)數(shù)據(jù)與CRC寄存器進(jìn)行異或

For Flag = 0 To 7

SaveHi = CRC16Hi

SaveLo = CRC16Lo

CRC16Hi = CRC16Hi \ 2 '高位右移一位

CRC16Lo = CRC16Lo \ 2 '低位右移一位

If ((SaveHi And &H1) = &H1) Then '如果高位字節(jié)zui后一位為1

CRC16Lo = CRC16Lo Or &H80 '則低位字節(jié)右移后前面補(bǔ)1

End If '否則自動(dòng)補(bǔ)0

If ((SaveLo And &H1) = &H1) Then '如果LSB為1,則與多項(xiàng)式碼進(jìn)行異或

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 '定時(shí)器1無(wú)效

Timer1.Interval = 1000 '定時(shí)器1時(shí)間為1S

Timer2.Enabled = True '定時(shí)器2有效

Timer2.Interval = 1000 '定時(shí)器2時(shí)間為1S

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

Text4.Text = ""

Text5.Text = ""

Text6.Text = ""

Text8.Text = ""

Text10.Text = ""

MSComm1.CommPort = 1 '設(shè)定端口號(hào)

MSComm1.Settings = "9600,n,8,1" '設(shè)定通訊波特率

MSComm1.InBufferSize = 1024 '接收緩沖器大小

MSComm1.OutBufferSize = 1024 '輸出緩沖器大小

MSComm1.InputMode = comInputModeBinary '以二進(jìn)制傳輸

MSComm1.RThreshold = 1 '

MSComm1.SThreshold = 0

MSComm1.InputLen = 0 '讀取接收緩沖器所有字符

MSComm1.OutBufferCount = 0 '清空發(fā)送緩沖區(qū)

MSComm1.InBufferCount = 0 '清空接收緩沖區(qū)

If MSComm1.PortOpen = False Then

Command3.Caption = "打開(kāi)串口"

Else

Command3.Caption = "關(guān)閉串口"

End If

End Sub

Private Sub Command1_Click()

Timer1.Enabled = True

End Sub

Private Sub Command2_Click() '退出程序,定時(shí)器1無(wú)效

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 '打開(kāi)關(guān)閉按鈕顯示文字

Command3.Caption = "關(guān)閉串口"

Else

Command3.Caption = "打開(kāi)串口"

End If

If Err Then '打開(kāi)串口失敗,則顯示出錯(cuò)信息

MsgBox Error$, 48, "錯(cuò)誤信息"

Exit Sub

End If

End Sub

Private Sub Timer1_Timer() '讀V存儲(chǔ)區(qū)數(shù)據(jù)

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 '定義動(dòng)態(tài)數(shù)組

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狀態(tà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 '定義動(dòng)態(tài)數(shù)組

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狀態(tà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 '定義動(dòng)態(tài)數(shù)組

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存儲(chǔ)器區(qū)數(shù)據(jù)

Dim FGetData As String

Dim t1 As Long

Dim av As Variant

Dim i As Integer

Dim ReDataLen As Integer

FGetData = ""

t1 = GetTickCount() '取時(shí)間,做延時(shí)用

Do '循環(huán)等待接收數(shù)據(jù)

DoEvents

If MSComm1.InBufferCount > 0 Then '串口有數(shù)據(jù)了

ReDataLen = MSComm1.InBufferCount '取數(shù)據(jù)長(zhǎng)度

av = MSComm1.Input '將串口數(shù)據(jù)取出來(lái)

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秒沒(méi)收完就不收了

ReceiveData = ""

Exit Function

End If

Loop

End Function

Private Function IReceiveData() As String '反回I狀態(tài)

Dim FGetData As String

Dim t1 As Long

Dim av As Variant

Dim i As Integer

Dim ReDataLen As Integer

FGetData = ""

t1 = GetTickCount() '取時(shí)間,做延時(shí)用

Do '循環(huán)等待接收數(shù)據(jù)

DoEvents

If MSComm1.InBufferCount > 0 Then '串口有數(shù)據(jù)了

ReDataLen = MSComm1.InBufferCount '取數(shù)據(jù)長(zhǎng)度

av = MSComm1.Input '將串口數(shù)據(jù)取出來(lái)

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秒沒(méi)收完就不收了

IReceiveData = ""

Exit Function

End If

Loop

End Function

Private Function QReceiveData() As String '反回Q狀態(tài)

Dim FGetData As String

Dim t1 As Long

Dim av As Variant

Dim i As Integer

Dim ReDataLen As Integer

FGetData = ""

t1 = GetTickCount() '取時(shí)間,做延時(shí)用

Do '循環(huán)等待接收數(shù)據(jù)

DoEvents

If MSComm1.InBufferCount > 0 Then '串口有數(shù)據(jù)了

ReDataLen = MSComm1.InBufferCount '取數(shù)據(jù)長(zhǎng)度

av = MSComm1.Input '將串口數(shù)據(jù)取出來(lái)

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秒沒(méi)收完就不收了

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
 

聯(lián)系我們

上海翰粵自動(dòng)化系統(tǒng)有限公司 公司地址:上海市松江區(qū)思賢路2399弄137號(hào)   技術(shù)支持:化工儀器網(wǎng)
  • 聯(lián)系人:黃政武
  • QQ:76868608
  • 公司傳真:021-57657276
  • 郵箱:76868608@qq.com

掃一掃 更多精彩

微信二維碼

網(wǎng)站二維碼

青草视频在线观看精品综合-黑人巨大精品欧美久久-日本理论三级在线观看-九九热九九热这里只有精品| 亚洲av高清网站夜夜去-拍国产乱人伦偷精品视频-成人日韩欧美在线观看-无遮挡国产精品一级二级三级视频| 日本一区二区三区在线视频-国产午夜性生活免费视频-亚洲老熟妇av熟妇在线-久久热这里只有精品国产| 男女公园上摸下揉视频-日本精品视频一二区-激情久久综合久久人妻-伊人成人综合在线视频| 亚洲三级免费在线播放-国产男女做a视频免费在线观看-六月婷婷缴情七月丁香-国产黄色片三级久久久| 少妇裸淫交视频免费看-欧美日韩中文字幕第一页-91精品看黄网站在线观看-国产精品一区二区三区色噜噜| 天堂av日韩在线播放-中文字幕久久精品亚洲-国产精品沟厕在线播放-在线观看亚洲精品在线av| 亚洲最大的偷拍视频网站-国产三级精品三级男人的天堂-国产成人免费精彩视频-一区二区精品日韩国产精品| 国产一级r片内射视频播放-中文字幕最新精品资源-久久青青草原精品国产麻豆综合-深夜成人在线免费视频| 蜜臀精品国产亚洲av尤物-日韩人妻少妇中文字幕-赶碰97在线公开视频-亚洲欧美日韩天堂综合| 正在播粉嫩丰满国产极品-国产成人午夜福利av在线-国产精品自拍自在线播放-一区二区三区四区日本视频| 91九色精品人成在线观看-国产成人免费综合激情-新久久国产色av免费看-av网站国产主播在线| 精品国产自产在线观看-四虎av一区二区在线观看-91久久精品人妻中文字幕-av网页一区二区三区| 99久久国产自偷自自偷蜜月-日韩熟女激情中文字幕-亚洲狼人社区av在线观看-四虎成人精品国产永久| 亚洲综合精品一区二区在线-国产亚洲精品视频在线播放-国产精品经典三级免费观看-五月婷婷六月丁香视频| 国产精品日本一区二区不卡视频-尤物在线视频免费观看-中文字幕精品高清中国-最新精品国产自偷在自线| 69精品人妻一区二区三区蜜桃久-国产粉嫩清纯美女在线观看-国产成人高清视频免费-国产日韩精品一区二区三区四区| 免费十八禁一区二区三区-国产精品一区二区三区99-在线一区二区三区男男视频观看-精品欧美一区二区三区人妖| 人妻中文字幕一区二区三区-国产精品丝袜久久亚洲不卡-久久伊人精品色婷婷国产-日韩中精品文字幕在线一区| 中文字幕一区二区三区日韩精品-久久老熟女一区二区三区福利-久久精品国产自产对白一区-午夜欧美牲交激情网站| 亚洲av一区二区三区av-国产av一区二区三区香蕉-久久超碰免费欧美人妻-九一精品人妻一区二区三区| 少妇一级aa一区二区三区片-欧美欧美欧美欧美一级片-91在线观看视频下载-自拍视频在线观看一区二区| 日韩中文有码字幕在线观看-黑人国产一区二区三区-久久国产精品久久精品-国产激情在线一区二区三区| 爆操美女屁股在线观看免费-亚洲国产成人久久综合-亚洲一区二区免费中文麻豆-青青青青草原在线观看| 久久国色夜色精品国产-国产微拍福利一区二区-91超碰青草福利久久尤物-国产精品97在线观看| 亚洲一级特黄大片做受-国产91喷潮在线观看-日本不卡一区二区三区四区-在线观看高清视频一区二区三区| 国产人妻熟女呻吟在线观看-国产成人免费视频观看-国产久久热这里只有精品-中文字幕女同女同女同| 九九热在线视频中文字幕-午夜激情在线观看不卡-国产精彩激情视频在线观看-人妻丰满熟妇九九久久| 国产人妖直男在线视频-午夜福利视频合集91-亚洲五月自拍欧美第一页-国产主播免费在线一区二区| 日韩成人深夜免费在线观看-成人av一区二区在线播放-日韩无套内射免费精品-国产精品一区白嫩在线观看| 国产美女口爆吞精服务-亚洲无人区码一码二码三码-久久精品99国产精品最新-日本少妇激情在线视频| 亚洲毛片在线观看视频网站-午夜高清福利在线观看-性生活视频在线免费观看-女人吞精口爆在线视频| 日韩有码中文字幕在线视频-草草影院国产在线观看-日韩中文字幕有码午夜美女-亚洲第二十页中文字幕| 国产四虎视频在线观看-日本一区二区三区暖暖视频免费-91人妻人人澡人人添人人爽-在线日本高清日本免费| 亚洲精品色国语对白在线-黄片毛片av在线免费观看-久久精品有码av天堂-日韩一区二区三区高清视频| 一本色道亚州综合久久精品-91麻豆国产专区在线观看-一级二级三级国产视频-熟女av天堂免费高清| 69精品人妻一区二区三区蜜桃久-国产粉嫩清纯美女在线观看-国产成人高清视频免费-国产日韩精品一区二区三区四区| 乱中年女人伦中文字幕久久-国产成人高清免费视频网站-中文字幕亚洲人妻在线视频-中文字幕剧情av在线| 日本午夜av免费久久观看-国产精品夜色一区二区三区不卡-亚洲高清自有码中文字-青青草国产成人在线观看| 粉嫩小粉嫩小国产小视-老熟妇人妻久久中文字幕麻豆网-蜜臀av在线播放国产-成年人的三级视频网站| 自拍成人免费在线视频-91在线高清视频播放-国产美女口爆吞精系列-午夜福利黄片在线观看|