⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 实际是一个网络编程中模拟读写串口的程序.不过是创建了一个文件,把文件当作串口进行操作
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "COM"
   ClientHeight    =   3135
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4095
   LinkTopic       =   "Form1"
   ScaleHeight     =   3135
   ScaleWidth      =   4095
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Cmd_W 
      Caption         =   "写"
      Height          =   495
      Left            =   2760
      TabIndex        =   3
      Top             =   600
      Width           =   855
   End
   Begin VB.CommandButton Cmd_R 
      Caption         =   "读"
      Height          =   495
      Left            =   2760
      TabIndex        =   2
      Top             =   1800
      Width           =   855
   End
   Begin VB.TextBox Text2 
      BackColor       =   &H80000000&
      Height          =   1215
      Left            =   240
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   1
      Top             =   1440
      Width           =   2055
   End
   Begin VB.TextBox Text1 
      Height          =   1215
      Left            =   240
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   0
      Top             =   120
      Width           =   2055
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Public Ccc, C_2, C_3 As String
Public My_bool As Boolean
Public hand_c As Long

Private Att_lp As SECURITY_ATTRIBUTES
Private CommString1 As String
Private Err_Code As Integer
Private Suzu(1024) As Byte






Private Sub Cmd_R_Click()

Dim hFile As Long
Dim buff(1024) As Byte
Dim msg As String
Dim www As SECURITY_ATTRIBUTES
Dim sss As OVERLAPPED
Dim cnt As Integer
Dim cnts As Long


hFile = CreateFile("D:\test.txt", GENERIC_READ, FILE_SHARE_WRITE Or FILE_SHARE_READ, www, OPEN_EXISTING, 0, 0)


If hFile <> INVALID_HANDLE_VALUE Then
    For cnt = 0 To 22
      buff(cnt) = &H0
      
    Next cnt

    Call ReadFile(hFile, buff(0), 1024, cnts, sss)
    msg = ""
    For cnt = 0 To 22
      msg = msg + Chr(buff(cnt))
    Next cnt
    Call CloseHandle(hFile)
    MsgBox msg
Else
    msg = "error:    " & Str(hFile)
   MsgBox msg
End If

















'C_2 = "D:\Documents and Settings\Administrator\桌面\CS\cline\text.txt"
'Call ReadF(hand_c, Suzu(0))
Dim s1, s2 As String
Dim i, j, k, l As Integer

i = 1024
k = 1
'For j = 0 To i
'   s1 = s1 + CStr(Suzu(j))
    
'Next j
s1 = msg
Text2.Text = s1
Text1.Text = Text2.Text


End Sub


Private Sub Cmd_W_Click()

Dim hFile As Long
Dim buff(256) As Byte
Dim msg As String
Dim www As SECURITY_ATTRIBUTES
Dim sss As OVERLAPPED
Dim cnt As Integer
Dim cnts As Long

Dim s1, s2 As String
Dim i, j, k, m As Integer
s1 = Trim(Text1.Text)
i = Len(s1)
k = 1
m = 0
For j = 0 To i - 1
    m = Asc(Mid(s1, k, 1))
    Suzu(j) = m
    k = k + 1
Next j


'hFile = INVALID_HANDLE_VALUE
hFile = CreateFile("E:\test.txt", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, www, CREATE_ALWAYS, 0, 0)
If hFile <> INVALID_HANDLE_VALUE Then
    Call WriteF(hFile, Suzu(0))
   
    Call CloseHandle(hFile)
Else
    msg = "error:    " & Str(hFile)
   MsgBox msg
End If










End Sub

Private Sub Form_Load()
'Option1.Value = True
End Sub



'写数据

Public Function WriteF(hand_c As Long, Suzu As Byte) As Boolean

Dim Writebuf As Long
Dim W_ha As OVERLAPPED
'Call Open_Comm(Op_Comm)
If WriteFile(hand_c, Suzu, 22, Writebuf, W_ha) <> 0 Then
    
    WriteF = True
Else
    Err_Code = 6
    MsgBox ErrCode(Err_Code)
  
    WriteF = False
End If

End Function


'读数据
Public Function ReadF(hand As Long, Suzu As Byte) As Boolean

Dim Readbuf As Long
Dim R_ha As OVERLAPPED

If ReadFile(hand, Suzu, 80, Readbuf, R_ha) <> 0 Then
       ReadF = True
Else
    Err_Code = 7
    MsgBox ErrCode(Err_Code)
   
    ReadF = False
End If


End Function


'关闭串口
Public Function CloseComm(hand As Long) As Boolean
If CloseHandle(hand) <> 0 Then
 
    CloseComm = True
Else
    Err_Code = 8
    MsgBox ErrCode(Err_Code)
 
    CloseComm = False
End If

End Function

'错误处理函数

Public Function ErrCode(Err_h As Integer) As String
Select Case Err_h
Case 1
ErrCode = "串口打开失败!"
Case 2
ErrCode = "获得串口失败!"
Case 3
ErrCode = "DCB填充失败!"
Case 4
ErrCode = "串口初始化失败!"
Case 5
ErrCode = "串口设置失败!"
Case 6
ErrCode = "写失败!"
Case 7
ErrCode = "读失败!"
Case 8
ErrCode = "关闭串口失败!"
Case Else
ErrCode = "其它错误!"
End Select
End Function


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -