C# Buzzer Sample Code

Post Reply
spinbot
Posts: 8
Joined: Sat Jul 25, 2009 5:07 am

C# Buzzer Sample Code

Post by spinbot » Thu Dec 03, 2009 5:52 pm

Hi all,

Here's a C# class that controls the buzzer on the board. I think it will only run on the new CE6 images which have system support for the buzzer. This makes it super easy to make the buzzer work! I can convert it to VB.Net if people want it...

Code: Select all

    class Buzzer {

        [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
        internal static extern int DeviceIoControlCE(int hDevice, int dwIoControlCode, byte[] lpInBuffer, int nInBufferSize, byte[] lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr lpOverlapped);

        [DllImport("coredll", SetLastError = true)]
        private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

        private static IntPtr _pwmFile = CreateFile("PWM1:", 0x40000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero);

        private Buzzer() {
        }

        public static void Beep() {
            Beep(1000, 20);
        }

        public static void Beep(uint Frequency, int DurationMS) {
            uint frequency = Frequency;
            byte[] buffer = new byte[4];
            int accessType = 2;
            buffer[0] = (byte)(frequency & 0xff);
            frequency = frequency >> 8;
            buffer[1] = (byte)(frequency & 0xff);
            frequency = frequency >> 8;
            buffer[2] = (byte)(frequency & 0xff);
            frequency = frequency >> 8;
            buffer[3] = (byte)(frequency & 0xff);

            DeviceIoControlCE((int)_pwmFile, accessType, buffer, 4, buffer, 0, ref accessType, IntPtr.Zero);
            var t = new System.Threading.Timer(TurnOff, null, DurationMS, System.Threading.Timeout.Infinite);
        }

        private static void TurnOff(Object obj) {
            byte[] buffer = new byte[4];
            int accessType = 1;
            DeviceIoControlCE((int)_pwmFile, accessType, buffer, 4, buffer, 0, ref accessType, IntPtr.Zero);
        }
    }
Last edited by spinbot on Tue Apr 27, 2010 11:10 pm, edited 1 time in total.

mohanraj
Posts: 3
Joined: Tue Apr 13, 2010 3:21 am

Re: C# Buzzer Sample Code

Post by mohanraj » Tue Apr 20, 2010 2:08 am

Dear Spinbot,

I need it in vb.net , can i use this code in Wince5.0

spinbot
Posts: 8
Joined: Sat Jul 25, 2009 5:07 am

Re: C# Buzzer Sample Code

Post by spinbot » Wed Apr 21, 2010 4:10 pm

Sorry, this code uses new stuff in the CE6 image.
I can convert it to VB if you want but you must be using the new firmware.

Cheers

mohanraj
Posts: 3
Joined: Tue Apr 13, 2010 3:21 am

Re: C# Buzzer Sample Code

Post by mohanraj » Tue Apr 27, 2010 9:35 pm

yes i can upgrade to wince6 kindly provide the sample code for buzzer in vb.net
thanks

spinbot
Posts: 8
Joined: Sat Jul 25, 2009 5:07 am

Re: C# Buzzer Sample Code

Post by spinbot » Tue Apr 27, 2010 11:10 pm

Ok, here's the same code in VB:

Code: Select all

Imports System.Runtime.InteropServices

Public Class Buzzer

    <DllImport("coredll.dll")> _
    Public Shared Function DeviceIoControl(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 IntPtr) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Public Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As UInteger, ByVal dwShareMode As UInteger, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As UInteger, ByVal dwFlagsAndAttributes As UInteger, ByVal hTemplateFile As IntPtr) As IntPtr
    End Function

    Private Shared _pwmFile As IntPtr = CreateFile("PWM1:", &H40000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero)

    Private Sub New()
    End Sub

    Public Shared Sub Beep()
        Beep(1000, 20)
    End Sub

    Public Shared Sub Beep(ByVal Frequency As UInteger, ByVal DurationMS As Integer)
        Dim freq As UInteger = Frequency
        Dim buffer(4) As Byte
        Dim accessType As Integer = 2
        buffer(0) = CType((Frequency And &HFF), Byte)
        Frequency = Frequency >> 8
        buffer(1) = CType((Frequency And &HFF), Byte)
        Frequency = Frequency >> 8
        buffer(2) = CType((Frequency And &HFF), Byte)
        Frequency = Frequency >> 8
        buffer(3) = CType((Frequency And &HFF), Byte)

        DeviceIoControl(_pwmFile, accessType, buffer, 4, buffer, 0, accessType, IntPtr.Zero)
        Dim t As New System.Threading.Timer(AddressOf TurnOff, Nothing, DurationMS, System.Threading.Timeout.Infinite)
    End Sub

    Private Shared Sub TurnOff(ByVal obj As Object)
        Dim buffer(4) As Byte
        Dim accessType As Integer = 1
        DeviceIoControl(_pwmFile, accessType, buffer, 4, buffer, 0, accessType, IntPtr.Zero)
    End Sub
End Class
Let me know what you think

Cheers

ferbla
Posts: 1
Joined: Tue Jun 01, 2010 10:52 am

Re: C# Buzzer Sample Code

Post by ferbla » Tue Jun 01, 2010 10:59 am

Hello,
I'm using this code, but facing a problem...

My application (wince 6, vbnet2005) beeps the buzzer one time every 1 second... this works ok, for about 15 minutes, after that the buzzer doesnt beep anymore. I already looked up my app, and there is nothing there that can be bothering... also I made another app, where the only thing that I have is a 1000ms timer that turns on the buzzer for 200ms.

Does anyone have any idea of what can be happening?
Thank you all.
Fernando

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest