您现在所在的是:

串口通信

回帖:1个,阅读:3496 [上一页] [1] [下一页]
2459
sgx
文章数:474
年度积分:50
历史总积分:2459
注册时间:2001/11/22
发站内信
发表于:2003/12/13 21:53:00
#0楼
基于MODBUS协议的串行通讯例程(VB)
'基于MODBUS协议的和平软起动器(http://www.hapn.com)串行通讯例程
'可通过MODBUS协议获取:主电压、电流、运行状态、DIP开关设置、硬接线状态、统计数据等;
'并可通过MODBUS协议控制:启动、急停、软停、双重参数调节、慢速正/反转、节能等;
'本例程为通过通讯获取软启动器当前运行状态
'编制:董斌  dbboss@21cn.com

Private Const Read_Coil_Status = &H1
Private Const Read_Input_Status = &H2
Private Const Read_Holding_Registers = &H3
Private Const Read_Input_Registers = &H4
Private Const Force_Single_Coil = &H5
Private Const Single_Registers = &H6
Private Const Diagnostics = &H8
Private Const Force_Multiple_Coil = &HF
Private Const Force_Multiple_Register = &H10

Public Function CRC16(data() As Byte) As tCRC
   Dim CRC16Hi As Byte
   Dim CRC16Lo As Byte
   Dim Result As tCRC
     CRC16Hi = &HFF
     CRC16Lo = &HFF
   Dim i As Integer
   Dim iIndex As Long
   For i = 0 To UBound(data)
       iIndex = CRC16Lo Xor data(i)
       CRC16Lo = CRC16Hi Xor GetCRCLo(iIndex)        '低位处理
       CRC16Hi = GetCRCHi(iIndex)                    '高位处理
   Next i
   With Result
       .bytLow = CRC16Lo       'CRC低位
       .bytHigh = CRC16Hi       'CRC高位
   End With
   CRC16 = Result
End Function

'CRC低位字节值表
Private Function GetCRCLo(Ind As Long) As Byte
   GetCRCLo = Choose(Ind + 1, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _
   &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40)
End Function

'CRC高位字节值表
Private Function GetCRCHi(Ind As Long) As Byte
   GetCRCHi = Choose(Ind + 1, &H0, &HC0, &HC1, &H1, &HC3, &H3, &H2, &HC2, &HC6, &H6, &H7, &HC7, &H5, &HC5, &HC4, &H4, &HCC, &HC, &HD, &HCD, &HF, &HCF, &HCE, &HE, &HA, &HCA, &HCB, &HB, &HC9, &H9, &H8, &HC8, &HD8, &H18, &H19, &HD9, &H1B, &HDB, &HDA, &H1A, &H1E, &HDE, &HDF, &H1F, &HDD, &H1D, &H1C, &HDC, &H14, &HD4, &HD5, &H15, &HD7, &H17, &H16, &HD6, &HD2, &H12, &H13, &HD3, &H11, &HD1, &HD0, &H10, &HF0, &H30, &H31, &HF1, &H33, &HF3, &HF2, &H32, &H36, &HF6, &HF7, &H37, &HF5, &H35, &H34, &HF4, &H3C, &HFC, &HFD, &H3D, &HFF, &H3F, &H3E, &HFE, &HFA, &H3A, &H3B, &HFB, &H39, &HF9, &HF8, &H38, &H28, &HE8, &HE9, &H29, &HEB, &H2B, &H2A, &HEA, &HEE, &H2E, &H2F, &HEF, &H2D, &HED, &HEC, &H2C, &HE4, &H24, &H25, &HE5, &H27, &HE7, &HE6, &H26, &H22, &HE2, &HE3, &H23, &HE1, &H21, &H20, &HE0, &HA0, &H60, _
   &H61, &HA1, &H63, &HA3, &HA2, &H62, &H66, &HA6, &HA7, &H67, &HA5, &H65, &H64, &HA4, &H6C, &HAC, &HAD, &H6D, &HAF, &H6F, &H6E, &HAE, &HAA, &H6A, &H6B, &HAB, &H69, &HA9, &HA8, &H68, &H78, &HB8, &HB9, &H79, &HBB, &H7B, &H7A, &HBA, &HBE, &H7E, &H7F, &HBF, &H7D, &HBD, &HBC, &H7C, &HB4, &H74, &H75, &HB5, &H77, &HB7, &HB6, &H76, &H72, &HB2, &HB3, &H73, &HB1, &H71, &H70, &HB0, &H50, &H90, &H91, &H51, &H93, &H53, &H52, &H92, &H96, &H56, &H57, &H97, &H55, &H95, &H94, &H54, &H9C, &H5C, &H5D, &H9D, &H5F, &H9F, &H9E, &H5E, &H5A, &H9A, &H9B, &H5B, &H99, &H59, &H58, &H98, &H88, &H48, &H49, &H89, &H4B, &H8B, &H8A, &H4A, &H4E, &H8E, &H8F, &H4F, &H8D, &H4D, &H4C, &H8C, &H44, &H84, &H85, &H45, &H87, &H47, &H46, &H86, &H82, &H42, &H43, &H83, &H41, &H81, &H80, &H40)
End Function

Public Function Get_Logic_Status(objRS485_Comm As MSComm, nSerialLink As Byte, _
               tTimeOut As Double) As tLogic_Status
'取软启动器逻辑状态
Dim CRC As tCRC, blnTimeOut As Boolean
Dim outByte() As Byte, T As Double, inByte() As Long, i As Long, r As Integer
Dim inChr As String, wStatus As Long, Result As tLogic_Status
ReDim outByte(0 To 5) As Byte
outByte(0) = nSerialLink                       '从设备号
outByte(1) = Read_Input_Registers              '功能调用号
outByte(2) = &H0                               '数据所在开始地址(高字节)
outByte(3) = &H0                               '数据所在开始地址(低字节)
outByte(4) = &H0                               '读取数据个数(高字节)
outByte(5) = &H1                               '读取数据个数(低字节)
CRC = CRC16(outByte())                         '计算CRC
ReDim Preserve outByte(0 To 7) As Byte
With CRC
   outByte(6) = .bytLow
   outByte(7) = .bytHigh
End With
objRS485_Comm.InBufferCount = 0                '清通讯口输入缓存区
objRS485_Comm.Output = outByte                 '输出
Do
   DoEvents
Loop Until objRS485_Comm.OutBufferCount = 0    '输出直至完毕
T = Timer
Do
   If Timer - T > tTimeOut Then               '超时?
       blnTimeOut = True
   End If
Loop Until (objRS485_Comm.InBufferCount >= 7) Or blnTimeOut '超时或接收数据长度〉7时结束接收
If objRS485_Comm.InBufferCount > 0 Then                     '处理接收数据
   objRS485_Comm.InputLen = 1
   i = 0
   Do
       inChr = objRS485_Comm.Input
       ReDim Preserve inByte(0 To i) As Long
       inByte(i) = AscB(inChr)
       i = i + 1
   Loop Until objRS485_Comm.InBufferCount = 0
   wStatus = CLng(inByte(3) * 256) + inByte(4)
   With Result
       .blnInsulation = wStatus And &H40&                        '绝缘报警
       .blnMotorRunningSlowSpeedReverse = wStatus And &H80&      '慢速反转
       .blnMotorRunningSlowSpeedForward = wStatus And &H100&     '慢速正转
       .blnMotorRunningEnergySaveON = wStatus And &H200&         '节能
       .blnDualAdjON = wStatus And &H400&                        '双重参数调节
       .blnMotorRunning = wStatus And &H800&                     '电机运行
       .blnMotorStartProcess = wStatus And &H1000&               '电机起动过程中
       .blnMotorSoftStopProcess = wStatus And &H2000&            ’电机软停过程中
       .blnMotorStopped = wStatus And &H4000&        ‘电机停止
       .blnTripped = wStatus And &H8000&              ‘跳闸
   End With
   Get_Logic_Status = Result
End If
objRS485_Comm.InBufferCount = 0
End Function

‘本程序在Windows9x/2k/xp vb6.0 下调试通过
骐骥一跃,不能十步;
驽马十驾,功在不舍
17528
ahljj
文章数:3386
年度积分:240
历史总积分:17528
注册时间:2002/3/23
发站内信
发表于:2012/6/7 13:31:19
#1楼
此楼内容不符合板块规定,不予显示! 查看原帖内容>>

关于我们 | 联系我们 | 广告服务 | 本站动态 | 友情链接 | 法律声明 | 非法和不良信息举报

工控网客服热线:0755-86369299
版权所有 工控网 Copyright©2024 Gkong.com, All Rights Reserved

46.8003