PC与三菱PLC的通信程序

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Module PLCtoPC

Public outdata() As Byte '定义发送数组,用来存放转换后的命令数据

Public Rcvlen As Integer '定义接收到的数据长度

Public Rcv() As Byte '定义接收数组,用来存放接收到的数据 Public inString As String '定义输入命令字符串

Public RcvFinFlag As Boolean '定义接收完成标志

Public ReadFlag As Boolean '标志定义读命令

Public FinalDataLen As Integer '定义接收到数据的最终长度变量

Public SaveString As String '定义输入命令暂存字符串变量

Public P_duanFlag As Byte

Public P2_duanFlag As Byte

Public Rcvtemp As String '定义存放每次接收到的数据的暂存 Public outdata_1 As String

Public Y(100) As Boolean

'''

''' 发送命令函数

'''

''' 指令

'''

Public Sub send(inString As String)

Dim length As Byte

Rcvlen = -1 '接收数据存放数组的下标初始化 length = Len(inString) '求形参传递过来的字符串的长度 ReDim outdata(0 To length)

ReDim outdata(0 To length + 1) '重新定义发送数据数组,其元素个数为length+1

outdata(0) = &H2 '命令报文以控制代码STX开始

outdata(length + 1) = &H3

For i = 1 To length '字符串转换为ASCII码,送入发送数组

outdata(i) = Asc(Mid(inString, i, 1))

Next i

Call FCScheck(outdata) '产生校验和,形成发送帧

length = UBound(outdata) '

ReDim Preserve outdata(0 To length) '重新定义发送数据数组

outdata_1 = BitConverter.ToString(outdata)

frmParam.SerialPort1.Write(outdata, 0, outdata.Length) '发送命令帧

frmPlc.ListBox1.Items.Add(outdata_1)

End Sub

'求和校验子程序

Private Sub FCScheck(ByRef outdata() As Byte)

Dim BufLen As Integer, Buf As String '定义字符串长度变量和字符串变量

Dim i As Integer '

Dim CheckSum As Long '定义校验和变量

BufLen = UBound(outdata) '求outdata数组可用最大下标

CheckSum = 0 '初始化校验和 For i = LBound(outdata) + 1 To UBound(outdata) '求和时不包括的控制代码

CheckSum = (CheckSum + outdata(i) And &HFF) '对outdata 数组的元素求和,只保留低位字节

Next i

'若校验和只有一位,则高位添零,补足为2

Buf = IIf(Len(Hex(CheckSum)) = 1, "0" & Hex(CheckSum), Hex(CheckSum)) ReDim Preserve outdata(BufLen + 2)

outdata(BufLen + 1) = Asc(Mid(Buf, 1, 1)) '校验和转换为ASCII 码,低位在前

outdata(BufLen + 2) = Asc(Mid(Buf, 2, 1))

End Sub

Private Sub Y0_Click(sender As Object, e As EventArgs) Handles Y0.Click Static Dim flag As Boolean

If flag = 0 Then

inString = "70005"

flag += 1

Else

inString = "80005"

flag = 0

End If

Call send(inString) '调用发送子程序

End Sub

Private Sub Y1_Click(sender As Object, e As EventArgs) Handles Y1.Click Static Dim flag As Boolean

If flag = 0 Then

inString = "70105"

flag += 1

Else

inString = "80105"

flag = 0

End If

Call send(inString) '调用发送子程序

End Sub

Private Sub Y2_Click(sender As Object, e As EventArgs) Handles Y2.Click Static Dim flag As Boolean

If flag = 0 Then

inString = "70205"

flag += 1

Else

inString = "80205"

flag = 0

End If

Call send(inString) '调用发送子程序

End Sub

Private Sub Y3_Click(sender As Object, e As EventArgs) Handles Y3.Click Static Dim flag As Boolean

If flag = 0 Then

inString = "70305"

flag += 1

Else

inString = "80305"

flag = 0

End If

Call send(inString) '调用发送子程序

End Sub

Private Sub Y4_Click(sender As Object, e As EventArgs) Handles Y4.Click Static Dim flag As Boolean

If flag = 0 Then

inString = "70405"

flag += 1

Else

inString = "80405"

flag = 0

End If

Call send(inString) '调用发送子程序

End Sub

Private Sub Y5_Click(sender As Object, e As EventArgs) Handles Y5.Click Static Dim flag As Boolean

If flag = 0 Then

inString = "70505"

相关文档
最新文档