VB.NET中的常用方法

一、如何使用dll库:

  dll库是动态链接库,一般是别人提供的,用来做二次开发,相当于别人把一些函数包装在dll中,已经生成可以链接文件,你只能调用,但是不能看到方法的实现。所以给你提供dll的人一般也会提供dll的库函数说明,说明中会告诉你里面的函数的返回值,参数等一些东西。

  1、要使用dll库中的方法,首先把dll复制粘贴在你的.net项目中的\bin\Debug路径下面。

  2、在项目中新建一个模块Module,然后在里面声明要用的函数。例如:我新建一个叫API的模块,然后声明函数,具体过程如下:

  

Imports System.Text
Module API

    ‘驱动加载函数
    Public Declare Function RDR_LoadReaderDrivers Lib "rfidlib_reader.dll" (ByVal path As String) As Integer
    Public Declare Function RDR_GetLoadedReaderDriverCount Lib "rfidlib_reader.dll" () As Integer

    ‘设备连接、断开函数
    Public Declare Function RDR_Open Lib "rfidlib_reader.dll" (ByVal connstr As String, ByRef hr As UIntPtr) As Integer
    Public Declare Function RDR_Close Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr) As Integer

    ‘获取错误代码
    Public Declare Function RDR_GetReaderLastReturnError Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr) As Integer

    ‘
    Public Declare Function RDR_GetTagDataReportCount Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr) As UInt32

    ‘以下是读取读卡器参数的函数
    Public Declare Function RDR_GetReaderInfor Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr, ByVal Type As Byte, ByRef buffer As StringBuilder, ByRef nSize As UInt32) As Integer

    ‘以下是盘卡函数
    Public Declare Function RDR_CreateInvenParamSpecList Lib "rfidlib_reader.dll" () As UIntPtr
    Public Declare Function ISO15693_CreateInvenParam Lib "rfidlib_aip_iso15693.dll" (ByVal hInvenParamSpecList As UIntPtr, ByVal AntennaID As Byte, ByVal en_afi As Byte, ByVal afi As Byte, ByVal slot_type As Byte) As UIntPtr
    Public Declare Function RDR_TagInventory Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr, ByVal AIType As Byte, ByVal AntennaCount As Byte, ByRef AntennaIDs As Byte, ByVal InvenParamSpecList As UIntPtr) As Integer ‘AntennaIDs传入数组的第一个元素地址
    Public Declare Function RDR_GetTagDataReport Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr, ByVal Seekpos As Byte) As UIntPtr
    Public Declare Function ISO15693_ParseTagDataReport Lib "rfidlib_aip_iso15693.dll" (ByVal hTagReport As UIntPtr, ByRef aip_id As UInt32, ByRef tag_id As UInt32, ByRef ant_id As UInt32, ByRef dsfid As Byte, ByRef uid As Byte) As Integer

    ‘以下是销毁盘点参数列表
    Public Declare Function DNODE_Destroy Lib "rfidlib_reader.dll" (ByVal dn As UIntPtr) As Integer

    ‘盘点卡的两个宏定义
    Public Const RFID_SEEK_FIRST = 1
    Public Const RFID_SEEK_NEXT = 2

    ‘连接卡
    Public Declare Function ISO15693_Connect Lib "rfidlib_aip_iso15693.dll" (ByVal hr As UIntPtr, ByVal tagType As UInt32, ByVal address_mode As Byte, ByVal uid() As Byte, ByRef ht As UIntPtr) As Integer

    ‘读卡
    Public Declare Function ISO15693_ReadMultiBlocks Lib "rfidlib_aip_iso15693.dll" (ByVal hr As UIntPtr, ByVal ht As UIntPtr, ByVal readSecSta As Byte, ByVal blkAddr As UInt32, ByVal numOfBlksToRead As UInt32, ByRef numOfBlksRead As UInt32, ByRef bufBlocks As Byte, ByVal nSize As UInt32, ByRef bytesBlkDatRead As UInt32) As Integer

    ‘写卡
    Public Declare Function ISO15693_WriteMultipleBlocks Lib "rfidlib_aip_iso15693.dll" (ByVal hr As UIntPtr, ByVal ht As UIntPtr, ByVal blkAddr As UInt32, ByVal numOfBlks As UInt32, ByVal newBlksData() As Byte, ByVal bytesToWrite As UInt32) As Integer

    Public Declare Function LSG_CmdGetReports Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal Flag As Byte, ByVal recordsToGet As Byte) As Integer
    Public Declare Function LSG_ParseSCEventData Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal slData As String, ByVal nSize As Long, ByRef dir As Byte, ByRef m_time As Byte) As Integer
    Public Declare Function LSG_CmdGetCurrentFlowOfPeople Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByRef inFlow As Long, ByRef outFlow As Long) As Integer
    Public Declare Function LSG_CmdResetFlowOfPeople Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal mFlg As Byte) As Integer
    Public Declare Function LSG_CmdReverseDirection Lib "rfidlib_LSGate.dll" (ByVal hr As Long) As Integer
    Public Declare Function LSG_CmdGetSystemTime Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByRef year As Long, ByRef month As Byte, ByRef day As Byte, ByRef hour As Byte, ByRef minute As Byte, ByRef second As Byte) As Integer
    Public Declare Function LSG_CmdSetSystemTime Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal year As Long, ByVal month As Byte, ByVal day As Byte, ByVal hour As Byte, ByVal minute As Byte, ByVal second As Byte) As Integer

    Public Declare Function MultiByteToWideChar Lib "kernel32 " (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
    Public Declare Function WideCharToMultiByte Lib "kernel32 " (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
    Public Const CP_ACP = 0 ‘ default to ANSI code page
    Public Const CP_UTF8 = 65001 ‘ default to UTF-8 code page

End Module

声明完的函数我就可以在项目中调用了,例如:

     ‘设备连接串
        Dim connStr = "RDType=RD201;CommType=COM;COMName=" + CmboxcommCanBeUse.SelectedItem + ";BaudRate=38400;Frame=8E1;BusAddr=255"
        Dim connectOK As Integer = 99
        connectOK = RDR_Open(connStr, hr)

以上代码中的RDR_Open()函数就是在API模块中声明的,函数所在的位置是rfidlib_reader.dll中。

二、常用的一些方法:

Now()     获取当前日期时间,

Format() 格式化日期时间函数:

 Format(Now(), "yyyy-MM-dd-hh-mm-ss")

DataGridView用法:

GoodDataGridView.Rows.Add()为添加一行

GoodDataGridView.Rows.RemoveAt(1) 为删除第一行

GoodDataGridView(0, 0).Value = "序号"   为给某列某行的单元格设置值。其中(0,0)中前面的数字代表第几列,后面的数字代表第几行。
     GoodDataGridView.Rows.Add()
        GoodDataGridView(0, 0).Value = "序号"
        GoodDataGridView(1, 0).Value = "商品编码"
        GoodDataGridView(2, 0).Value = "商品名称"
        GoodDataGridView(3, 0).Value = "商品价格"

定义数组并初始化:

      ‘定义一组商品的属性的二进制编码,用来保存当前连接的标签的内容
        Dim goodsNumb() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        Dim goodsName() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        Dim goodsPric() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

UTF8 编码与解码:

Imports System.Text

Module UTF8

    ‘UTF8编码函数
    Public Function StringToUTF8Bytes(ByVal strData As String) As Byte()

        Dim bytes() As Byte
        bytes = Encoding.UTF8.GetBytes(strData)
        Return bytes

    End Function

    ‘UTF8解码函数
    Public Function UTF8BytesToString(ByVal strData() As Byte) As String

        Dim retstring As String
        retstring = Encoding.UTF8.GetString(strData)
        Return retstring

    End Function

End Module
时间: 2024-10-28 10:22:30

VB.NET中的常用方法的相关文章

Java实战之02Hibernate-03Session中的常用方法

九.Session中的常用方法 1.save方法 都是临时态————>持久态 2.persist方法 作用: 持久化临时态对象. 与save方法的区别: 开始了事务:persist和save没有区别. 不开启事务: persist:什么都不会做. save: hibernate3:计划保存数据,因为没有开启事务,自动回滚. hibernate5:提供一个内置事务执行保存操作. 1 /* 2 * save方法和persist方法 3 * 共同点: 4 * 都是把临时态对象转成持久态 5 * 区别:

VB.NET中的除法运算符 与 C#中的除法运算符

VB.NET中的除法运算符有两个:/(浮点除法).\(整数除法) C#中的除法运算符只有一个:/(除法) VB.NET中的除法运算符与C#中的除法运算符存在很大的差异,使用时注意区分. 关于VB.NET中的除法运算符的介绍(摘自MSDN): /(浮点除法):将两个数相除并返回以浮点数表示的结果. 所得结果的数据类型取决于操作数的类型. 下表显示如何确定结果的数据类型. 操作数数据类型 结果数据类型 两个表达式都是整数数据类型(SByte.Byte.Short.UShort.Integer.UIn

[转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBox.Show (IWin32Window, String) 在指定对象的前面显示具有指定文本的消息框. MessageBox.Show (String, String) 显示具有指定文本和标题的消息框.由 .NET Compact Framework 支持. MessageBox.Show (IWi

探秘VB.net中的shared与static

简单了解了一下C#之后,很快就要开始重构机房收费系统了,有人选择用C#来实现,不过还是想好好了解一些VB.net,用它来实现. 有了VB和面向对象基础,学习VB.NET是极易上手的.学习了几种语言的我们都有一种体会,编程语言就是一门语言,一个工具,通过它来实现某些功能.假如我们之前学过C++,C#等任何一门OO语言,通过这些语言了解了OOP,现在让你来学习VB.NET,肯定会特别容易,虽然他们的语法不一样,但其中的思想都是一样的,都有类.构造函数.属性-- VB.net和C#一些关键字的比较:

关于在VB.NET中调用使用VC++编写的类库dll的一点笔记

前言 结对作业要求一出来,我就立刻想到了把“计算核心”封装成dll,然后使用vb.net编写UI调用dll的思路.然而在实现过程中却遇到了很多的问题. 我在这个过程中是负责使用vb.net编写UI并调用编写好的DLL进行计算的. 目标 使用c++把类封装到dll,并在vb.net中调用该dll,使用该dll中封装好的类.在查找资料的过程中,发现vb.net调用dll的方法主要有两种. (IDE:Visual Studio 2013 professional) 方法一:使用Declare语句 vb

vb.net中,如何把byte array还原成为包含中文的字符串

这个问题,在网上好找了半天,但说实在话,不得不说,现在的程序员,脑子都僵化了. 一个能解决的也没有. 这还不说,多数人指出应当两边都用UNICODE,不要用ASC 2码之类的.我想说,这些是人家问的吗?不懂就是不懂,要是我就这样,何必不懂还要瞎JB指挥? 但我要说,这正是知其然不知其所以然,正确的回答,永远是先解释完提出问题的人,然后解决之. 然后,如果你愿意画蛇添足,再加上自己的想法.不想多说了,正确的代码我放在这里: '====================================

c#.net与vb.net中读写Cookie的方法!

Cookie (HttpCookie的实例)提供了一种在 Web 应用程序中存储用户特定信息的方法.例如,当用户访问您的站点时,您可以使用 Cookie 存储用户首选项或其他信息.当该用户再次访问您的网站时,应用程序便可以检索以前存储的信息. C#.net部分 创建Cookie方法 (1)Response.Cookies["userName"].Value = “admin";Response.Cookies[“userName”].Expires = DateTime.No

VB6中的引用传递 与 VB.NET中的引用传递的区别

首先注意一点,在VB6中缺省参数传递的方式是:引用传递,而在VB.NET中缺省参数传递的方式是:值传递. 然后我们看下面VB6中的引用传递与VB.NET中的引用传递的对比. VB6中的引用传递 Private Sub CommandButton1_Click() ChangeName CommandButton1.caption End Sub Private Sub ChangeName(caption As String) caption = "NameHasBeenChanged!&quo

String类中的常用方法_判断

字符串判断:1,字符串石佛包含某个字符串.boolean contains(str); 特殊之处:indexOf(String str),如果返回的值是-1,那么就表示不包含这个字符串 也可以实现判断. 2,字符串是否有内容.boolean isEmpty() 字符串为空时返回true 3,字符串是否以指定内容开头  boolean startsWith(String str) boolean startsWith(String str,int fromindex); 4,字符串是否以指定内容结