请教各位高手 怎样将串口收发数据 作成一个别的程序调用的dll啊

2004v2004 2004-10-25 03:05:08
怎样将串口收发数据 作成一个别的程序调用的dll啊

一无所之 请各位详细指导

谢谢了
...全文
92 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
2004v2004 2004-10-26
谢谢你 在类模块里没有 办法把command和mscomm 放到上面去啊
回复
aohan 2004-10-25
上面是叶帆写的关于串口通讯的例子,程序中的汉字全部成了乱码,所以删了不少注解
回复
aohan 2004-10-25
'*************************************************************************
Option Explicit
Private Declare Function GetCurrentTime Lib "kernel32" Alias "GetTickCount" () As Long

'*************************************************************************
Private Sub cmdSend_Click()
Dim bytData(10) As Byte
bytData(0) = &HA 'Êý¾Ý
txtMsg.Text = SendData(1, bytData, 1) '·¢ËÍÃüÁî
End Sub


Private Sub Form_Load()
OpenPort 1 '´ò¿ª´®¿Ú
End Sub

'*************************************************************************
Public Function OpenPort(PortNo As Integer, Optional InBufferSize As Integer = 1024, Optional OutBufferSize As Integer = 512) As Long
On Error GoTo ErrExit
MSComm1.CommPort = PortNo '²ÉÓÃCOM¶Ë¿Ú
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary '²ÉÓöþ½øÖÆ´«Êä
MSComm1.NullDiscard = False 'NULL×Ö·û´Ó¶Ë¿Ú´«Ë͵½½ÓÊÜ»º³åÇø
MSComm1.DTREnable = False 'DTRÏßÎÞЧ
MSComm1.EOFEnable = False '²»Ñ°ÕÒEOF·û
MSComm1.RTSEnable = False 'RTSÏßÎÞЧ
MSComm1.InBufferCount = 0 'Çå¿Õ½ÓÊÜ»º³åÇø
MSComm1.OutBufferCount = 0 'Çå¿Õ´«Ê仺³åÇø
MSComm1.SThreshold = 1 'Èç¹û´«Ê仺³åÇøÍêÈ«¿Õʱ²úÉúMSCommʼþ
MSComm1.RThreshold = 0 '²»²úÉúMSCommʼþ
MSComm1.InBufferSize = InBufferSize '½ÓÊÕ»º³åÇø ĬÈÏΪ1024¸ö×Ö½Ú
MSComm1.OutBufferSize = OutBufferSize '·¢ËÍ»º³åÇø ĬÈÏΪ512¸ö×Ö½Ú
MSComm1.PortOpen = True '´ò¿ª¶Ë¿Ú
OpenPort = 0
Exit Function
ErrExit:
OpenPort = 1
End Function
'*************************************************************************
Public Sub ClosePort()
On Error GoTo ErrExit
MSComm1.PortOpen = False '¹Ø±Õ¶Ë¿Ú
Exit Sub
ErrExit:

End Sub

'*************************************************************************
Public Function SendData(bytAddr As Byte, bytData() As Byte, Optional bytNum As Byte = 1) As Long
On Error GoTo ErrExit

Dim bytSendArray() As Byte '·¢ËÍÊý¾Ý»º³åÇø
Dim intGetDataLen As Integer 'Òª½ÓÊÕµÄÊý¾Ý³¤¶È
Dim sngTimeSpace As Single 'ÑÓʱʱ¼ä
Dim sngTime As Single
Dim bytReceiveArray() As Byte '½ÓÊÕµÄÊý¾Ý
Dim VarReceiveData As Variant '½ÓÊյıäÌåÊý¾Ý

Dim i As Long

ReDim bytSendArray(0 To bytNum * 2 + 2) As Byte '·¢ËÍÊý¾Ý»º³åÇø

bytSendArray(0) = &HAC 'ͬ²½×ÖÍ·
bytSendArray(1) = bytAddr 'ÏÂλ»úµØÖ·
bytSendArray(2) = bytAddr '¸±±¾

'Êý¾Ý
For i = 0 To bytNum * 2 - 1 Step 2
bytSendArray(i + 3) = bytData(i / 2)
bytSendArray(i + 4) = bytData(i / 2)
Next

MSComm1.InBufferCount = 0 'Çå¿Õ½ÓÊÕ»º³åÇø
MSComm1.Output = bytSendArray '·¢ËÍÊý¾Ý

Do
DoEvents
Loop Until MSComm1.OutBufferCount = 0 'µÈ´ý£¬Ö±µ½Êý¾Ý·¢ËÍÍê±Ï

intGetDataLen = 5
sngTimeSpace = intGetDataLen * (11000# / 9600#) * 10#

sngTime = GetCurrentTime() '

Do While True 'Êý¾Ý½ÓÊÕ

DoEvents
If MSComm1.InBufferCount >= intGetDataLen Then Exit Do

'³¬Ê±´¦Àí
If Abs(GetCurrentTime() - sngTime) > sngTimeSpace Then '³¬Ê±
SendData = 1
Exit Function
End If

Loop

VarReceiveData = MSComm1.Input
bytReceiveArray = VarReceiveData

'·µ»Ø֡УÑé
SendData = 2 'Ïȸ³Öµ½ÓÊÕµÄÊý¾Ý´íÎó
If bytReceiveArray(0) = &HAC Then '×ÖÍ·
If bytReceiveArray(1) = bytReceiveArray(2) And bytReceiveArray(3) = bytReceiveArray(4) Then
'Õ¾ºÅ£¬ÃüÁîÅжÏ
If bytReceiveArray(1) = bytSendArray(1) And bytReceiveArray(3) = &HAA Then
SendData = 0 'ÃüÁîÕýÈ·
End If
End If
End If

Exit Function

ErrExit:
SendData = 3
End Function

'*************************************************************************
Private Sub Form_Unload(Cancel As Integer)
ClosePort '¹Ø±Õ´®¿Ú
End Sub
回复
sunde2004 2004-10-25
你把MSCOMM属性看看设置一下就可以了.
它传输时就象时间控件一样,自动激活.
回复
2004v2004 2004-10-25
楼上的 我想把通过串口一来实现串口通讯 。但是mscomm该怎样用还有???

Private Sub Class_Initialize()

。。。。。。。。。。。。
End Sub
这里面怎样写代码 mscomm的oncom事件怎样触发啊
回复
cosio 2004-10-25
去新大陆中安电子去看看,那里有一个.dll 的文件,是条码机的!可以去看看!
如果不行,我发给你也可以!
回复
lyxhappy 2004-10-25
可能通过参数传递串口收发的数据呀
回复
lyxhappy 2004-10-25
可能通过参数传递串口收发的数据呀
回复
发动态
发帖子

1180

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
社区公告
暂无公告