谁有mscomm串口读写的简单源码50分奉送

hyq8848 2005-12-22 09:25:32
谁有mscomm串口读写的简单源码50分奉送
最好提供你们的qq或msn 方便调试
谢谢拉
...全文
287 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Keepmyself 2006-01-04
  • 打赏
  • 举报
回复
Mscomm1.Settings=”9600,N,8,1” ‘9600波特,无奇偶校验,8位数据,一个停止位
Mscomm1.InputLen=0 ‘读入整个缓冲区
Mscomm1.Portopen=True ‘打开端口
发送短消息的代码如下:
MSComm1.Output = "AT+CMGF=1" + vbCr ‘设置发送的模式,注意:一定要加
上 vbCr
MSComm1.Output = "AT+CSCA=" & Chr$(34) & "8613010341500" & Chr$(34)
& ",129" + vbCr ‘8613010341500是短消息中心,各地的号码不一样,必须设置当地的短消
息号码
MSComm1.Output = "AT+CMGS=" & Chr$(34) & "13057575064" & Chr$(34)
& ",129" + vbCr ‘13057575064是对方手机号
MSComm1.Output = "test" & Chr$(26) ‘chr$(26)是Ctr+ Z
接受就是
strMess=mscomm1.input
zou19820704 2006-01-02
  • 打赏
  • 举报
回复
'--------------------------------------------------------
' DIALER.FRM
''
' Demonstrates how to dial phone numbers with a modem.
'
' For this program to work, your telephone and
' modem must be connected to the same phone line.
'--------------------------------------------------------
Option Explicit

DefInt A-Z

' This flag is set when the user chooses Cancel.
Dim CancelFlag

Private Sub CancelButton_Click()
' CancelFlag tells the Dial procedure to exit.
CancelFlag = True
CancelButton.Enabled = False
End Sub

Private Sub Dial(Number$)
Dim DialString$, FromModem$, dummy

' AT is the Hayes compatible ATTENTION command and is required to send commands to the modem.
' DT means "Dial Tone." The Dial command uses touch tones, as opposed to pulse (DP = Dial Pulse).
' Numbers$ is the phone number being dialed.
' A semicolon tells the modem to return to command mode after dialing (important).
' A carriage return, vbCr, is required when sending commands to the modem.
DialString$ = "ATDT" + Number$ + ";" + vbCr

' Communications port settings.
' Assuming that a mouse is attached to COM1, CommPort is set to 2
MSComm1.CommPort = 2
MSComm1.Settings = "9600,N,8,1"

' Open the communications port.
On Error Resume Next
MSComm1.PortOpen = True
If Err Then
MsgBox "COM2: not available. Change the CommPort property to another port."
Exit Sub
End If

' Flush the input buffer.
MSComm1.InBufferCount = 0

' Dial the number.
MSComm1.Output = DialString$

' Wait for "OK" to come back from the modem.
Do
dummy = DoEvents()
' If there is data in the buffer, then read it.
If MSComm1.InBufferCount Then
FromModem$ = FromModem$ + MSComm1.Input
' Check for "OK".
If InStr(FromModem$, "OK") Then
' Notify the user to pick up the phone.
Beep
MsgBox "Please pick up the phone and either press Enter or click OK"
Exit Do
End If
End If

' Did the user choose Cancel?
If CancelFlag Then
CancelFlag = False
Exit Do
End If
Loop

' Disconnect the modem.
MSComm1.Output = "ATH" + vbCr

' Close the port.
MSComm1.PortOpen = False
End Sub

Private Sub DialButton_Click()
Dim Number$, Temp$

DialButton.Enabled = False
QuitButton.Enabled = False
CancelButton.Enabled = True

' Get the number to dial.
Number$ = InputBox$("Enter phone number:", Number$)
If Number$ = "" Then Exit Sub
Temp$ = Status
Status = "Dialing - " + Number$

' Dial the selected phone number.
Dial Number$

DialButton.Enabled = True
QuitButton.Enabled = True
CancelButton.Enabled = False

Status = Temp$
End Sub

Private Sub Form_Load()
' Setting InputLen to 0 tells MSComm to read the entire
' contents of the input buffer when the Input property
' is used.
MSComm1.InputLen = 0

End Sub

Private Sub QuitButton_Click()
End
End Sub

guoguo1982 2006-01-01
  • 打赏
  • 举报
回复
顶一下!!
zou19820704 2005-12-22
  • 打赏
  • 举报
回复
1 前言

  在工业控制领域,我们经常需要进行计算机与其他设备之间的通信,而串行通信作为一种灵活、方便、可靠的通信方式被广泛采用。在开发串行通信程序的过程中,利用微软的MSComm通信控件则相对较简单,该控件具有丰富的与串行通信密切相关的属性及事件,提供了对串口的各种操作。MSComm控件在串口编程时非常方便,程序员不必花时间去了解较为复杂的API函数,而且在VB中容易使用。

  2 用visual basic 6.0进行串行通信程序设计的方法

  (1) 用visual basic 6.0进行串行通信程序的设计有两种方式,一是直接调用Windows API,另一种方式是使用通信组件,但后一种方式实际上是通过通信组件间接的调用了Windows API函数,其过程要比直接调用API要复杂,但是在程序的实现上要比前一种方式简单的多。在操作系统方面,Windows使用通信驱动程序Comm.drv,以便于应用程序能够使用标准的Windows API函数来传递和接收数据。串行外围设备的制造商则提供硬件驱动程序,以便让其硬件能与Windows连接。使用MSComm控件时,实际上就是使用了API函数,API函数将被Comm.drv解释并传送给外围设备驱动程序,使用MSComm的用户只需要关心如何使用MSComm所提供的属性或事件,以驱动API函数的接口完成工作。



串行通信程序的执行过程

  (2) MSComm提供了两种处理通信问题的方法,一是事件驱动方法,一是查询法。

  ①事件驱动法

  在使用事件驱动法设计程序时,每当有新字符到达或端口状态改变,或发生错误时,MSComm控件将触发OnComm事件,而应用程序在捕获该事件后,通过检查MSComm控件的CommEvent属性可以获知所发生的事件或错误,从而采取相应的操作。这种方法的优点是程序响应及时,可靠性高。

  ②查询法

  查询法适合于较小的应用程序,在这种情况下,每当应用程序执行完某一串行口操作后,将不断检查MSComm控件的CommEvent属性,以检查执行结果或检查某一事件是否发生。

  (3) 串行通信的两种格式

  进行串行通信时有两种传输方式:

  ①字符形式:通常以小于ASCII码128的字符码来传递,通常用于传送指令。

  ②二进制形式:将数据以二进制编码的方式传递,它可能含有ASCII码128以上的字符码,通常用来传送数据,以提高速度。

  3.串行通信实例

  下面给出在Win2000下利用VB6.0的MSComm控件开发计算机与一智能采集模块进行串行通信的程序实现。在工控领域内,智能采集模块有着相当重要的地位,它可以通过串口通讯协议(RS232、RS485等)或其他通讯协议与PC机相连,并与外界现场信号直接相连或与由传感器转换过的外界信号相连,由PC机中的程序控制并实现采集现场的模拟信号,并处理采集到的现场信号并输出模拟控制信号、开关量输入输出等功能。因此,智能采集模块在工业控制领域内有着极其广泛的应用。本例子中的智能采集模块与计算机进行通信时,采用一位开始位、一位停止位、7位数据位、偶校验方式,波特率为9600bps.

  (1)在窗体中添加名为MyComm的MSComm控件,并设置空间的属性,如下:

.CommPort=2 '使用COM2
.Setting="9600,e,7,1" '波特率9600,偶校验,7个数据位,1个停止位
.InBufferSize=40 '设置MyComm接收缓冲区为40个字节
.OutBufferSize=2 '设置MyComm发送缓冲区为2个字节
.InputMode=comInputModeBinary '设置接收数据模式为二进制模式
.InputLen=1 '设置一次从接收缓冲区读取字节数为1
.Sthreshold=1 '设置一次从发送缓冲区读取字节数为1
.PortOpen=True '打开通信口

'其他属性设置省略


  (2)下面是响应Mycomm_OnComm事件的处理程序,对数据库采集的处理程序采用MSComm事件驱动方式。

Private Sub MyComm_OnComm()
With MyComm
Select case .CommEvent
Case .comEvReceive
' 接收数据
' 对数据进行处理
case .comEVSend
'发送数据
case .comEventRxParity
'对奇偶校验错误进行处理
end select
end with
end sub

  4.结束语

  本文结合实例介绍了基于VB6.0的MSComm串行通信设计方法。
hyq8848 2005-12-22
  • 打赏
  • 举报
回复
最好是有中文注释 介绍一下使用方法
Winters_lee 2005-12-22
  • 打赏
  • 举报
回复
踩一脚,蹭点分

1,453

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧