因為工作需要,使用了蜂鳴器,參照了別人C# 的code 改用vs2008 vb.net 撰寫,提供給大家參考
Module Module1
Declare Function CreateFile Lib "coredll" ( _
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As Integer, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFLagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer
Declare Function DeviceIoControl Lib "coredll.dll" ( _
ByVal hDevice As Integer, _
ByVal dwIoControlCode As Integer, _
ByVal lpInBuffer() As Byte, _
ByVal nInBufferSize As Integer, _
ByVal lpOutBuffer() As Byte, _
ByVal nOutBufferSize As Integer, _
ByRef lpBytesReturned As Integer, _
ByVal lpOverlapped As Integer) As Integer
Public Const OPEN_EXISTING As Integer = 3
Public Const INVALID_HANDLE_VALUE = -1
Public Const GENERIC_READ As Integer = &H80000000
Public Const GENERIC_WRITE As Integer = &H40000000
Public Const PWM_STOP As Integer = 1
Public Const PWM_SET_FEQ As Integer = 2
End Module
Public Class Form1
Dim hPort_PWM As Integer = -1
Public Sub Delayms(ByVal tm As Integer)
System.Threading.Thread.Sleep(tm)
Application.DoEvents()
End Sub
Private Sub Btn_PwmOn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn_PwmOn.Click
Dim pwm_frq(3) As Byte
Dim pwm_data(10) As Byte
Dim pwm_data1(3) As Byte
Dim delay_tm As Integer = Val(txt_delay.Text)
'4000
pwm_frq(0) = &HA0
pwm_frq(1) = &HF
pwm_frq(2) = 0
pwm_frq(3) = 0
hPort_PWM = CreateFile("PWM1:", GENERIC_READ + GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
If hPort_PWM = INVALID_HANDLE_VALUE Then
MessageBox.Show("Open PWM Driver Fail")
Else
DeviceIoControl(hPort_PWM, PWM_SET_FEQ, pwm_frq, 4, pwm_data, 0, 0, IntPtr.Zero)
Delayms(delay_tm)
DeviceIoControl(hPort_PWM, PWM_STOP, pwm_data1, 4, pwm_data, 0, 0, IntPtr.Zero)
CloseHandle(hPort_PWM)
End If
End Sub