473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ir on serial port vb.net 2005

haw to read data from an Infrared Infrared Remote Control
Nov 17 '06 #1
2 3763
'Simple RS232 Serial Port VB .Net class provided by Embedded Data Systems,
LLC.
'
'This code should be usable on both desktop and compact framework platforms
'by setting the constant 'COMPACT_FRAMEWORK' at compile time.
'This code may be freely copied providing you are using it to integrated
'products manufactured by Embedded Data Systems.
'This code is provided for sample purposes only, and is without warranty
'or guarantee of any kind.

Option Strict On
Public Class SerialPort
Implements IDisposable
#If COMPACT_FRAMEWORK Then
private Declare Function CreateFile Lib "coredll.dll" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As
Int32, _
ByVal hTemplateFile As IntPtr) As IntPtr
Private Declare Function GetCommState Lib "coredll.dll" (ByVal nCid As
IntPtr, ByRef lpDCB As DCB) As Boolean
Private Declare Function SetCommState Lib "coredll.dll" (ByVal nCid As
IntPtr, ByRef lpDCB As DCB) As Boolean
Private Declare Function GetCommTimeouts Lib "coredll.dll" (ByVal hFile As
IntPtr, ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Private Declare Function SetCommTimeouts Lib "coredll.dll" (ByVal hFile As
IntPtr, ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Private Declare Function WriteFile Lib "coredll.dll" (ByVal hFile As
IntPtr, ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToWrite As Int32,
ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As
Boolean
Private Declare Function ReadFile Lib "coredll.dll" (ByVal hFile As
IntPtr, ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, ByRef
lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean
Private Declare Function CloseHandle Lib "coredll.dll" (ByVal hObject As
IntPtr) As Boolean
#Else
Private Declare Auto Function CreateFile Lib "kernel32.dll" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As
Int32, _
ByVal hTemplateFile As IntPtr) As IntPtr
Private Declare Auto Function GetCommState Lib "kernel32.dll" (ByVal nCid
As IntPtr, ByRef lpDCB As DCB) As Boolean
Private Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid
As IntPtr, ByRef lpDCB As DCB) As Boolean
Private Declare Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal
hFile As IntPtr, ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Private Declare Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal
hFile As IntPtr, ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Private Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As
IntPtr, ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToWrite As Int32,
ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As
Boolean
Private Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As
IntPtr, ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, ByRef
lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean
Private Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal
hObject As IntPtr) As Boolean
#End If

Public Const NOPARITY As Int32 = 0 'No Parity
Public Const ONESTOPBIT As Int32 = 0 '1 Stop Bit

Private Structure DCB
Public DCBlength As Int32
Public BaudRate As Int32
Public fBitFields As Int32 'See Comments in Win32API.Txt
Public wReserved As Int16
Public XonLim As Int16
Public XoffLim As Int16
Public ByteSize As Byte
Public Parity As Byte
Public StopBits As Byte
Public XonChar As Byte
Public XoffChar As Byte
Public ErrorChar As Byte
Public EofChar As Byte
Public EvtChar As Byte
Public wReserved1 As Int16 'Reserved; Do Not Use
End Structure
Private fBinary As Integer = 1 'binary mode, no EOF check
Private fParity As Integer = 2 ' enable parity checking
Private fOutxCtsFlow As Integer = 4 'CTS output flow control
Private fOutxDsrFlow As Integer = 8 'DSR output flow control
Private fDTRControlDisable As Integer = 0 ' DTR flow control type (2 bits)
Private fDtrControlEnable As Integer = 16 ' Enables DTR Line, and leaves
it on.
Private fDTRControlHandshake As Integer = 32 'Standard DTR handshaking
Private fDsrSensitivity As Integer = 64 'DSR sensitivity
Private fTXContinueOnXoff As Integer = 128 'XOFF continues Tx
Private fOutX As Integer = 256 'XON/XOFF out flow control
Private fInX As Integer = 512 'XON/XOFF in flow control
Private fErrorChar As Integer = 1024 'enable error replacement
Private fNull As Integer = 2048 'enable null stripping
Private fRTSControlDisable As Integer = 0 'Flow control Line (2 bits)
Private fRTSControlEnable As Integer = 4096 'Enable RTS Flow control line,
and leave it on
Private fRTSControlHandshake As Integer = 8192 'Enables standard RTS
handshaking
Private fAbortOnError As Integer = 16384 'abort reads/writes on error
Private fDummy2 As Integer = 32768 'reserved

Private hSerialPort As IntPtr
Private MyDCB As DCB
Private MyCommTimeouts As COMMTIMEOUTS
Private oEncoder As New System.Text.ASCIIEncoding
Private oEnc As System.Text.Encoding = oEncoder.GetEncoding(1252)
Private portName As String

Private Const GENERIC_READ As Int32 = &H80000000
Private Const GENERIC_WRITE As Int32 = &H40000000
Private Const OPEN_EXISTING As Int32 = 3
Private Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80

Private Structure COMMTIMEOUTS
Public ReadIntervalTimeout As Int32
Public ReadTotalTimeoutMultiplier As Int32
Public ReadTotalTimeoutConstant As Int32
Public WriteTotalTimeoutMultiplier As Int32
Public WriteTotalTimeoutConstant As Int32
End Structure

'Opens the serial port using the parameters supplied.
Public Sub New(ByVal portName As String, ByVal baudRate As Integer, ByVal
parity As Byte, ByVal byteSize As Byte, ByVal stopBits As Byte)
Me.portName = portName
openPort(portName, baudRate, parity, byteSize, stopBits)
End Sub
'Transmits up to 48 bytes out the port
Public Sub tx(ByVal whatToSend As String)
Dim bytesWritten As Int32
Dim outbuffer(48) As Byte
Dim success As Boolean

outbuffer = oEnc.GetBytes(whatToSend)
success = WriteFile(hSerialPort, outbuffer, outbuffer.Length,
bytesWritten, IntPtr.Zero)

If success = False Then
Throw New CommException("Unable to write to " & Me.portName)
End If

End Sub
'Reads from port until some data is recieved, or timeOut is exceeded
'Timeout is currently hardcoded at 3000 mS, in openPort()
Public Function rx() As String
Dim bytesRead As Int32
Dim inBuffer(48) As Byte
Dim retVal As String
Dim success As Boolean

success = ReadFile(hSerialPort, inBuffer, inBuffer.Length, bytesRead,
IntPtr.Zero)
If success = False Then
Throw New CommException("Unable to read from " & portName)
End If
retVal = oEnc.GetString(inBuffer, 0, bytesRead)
Return retVal
End Function
Private Sub openPort(ByVal portName As String, ByVal baudRate As Integer,
ByVal parity As Byte, ByVal byteSize As Byte, ByVal stopBits As Byte)

Dim Success As Boolean
' Obtain a handle to the serial port.
hSerialPort = CreateFile(portName, GENERIC_READ Or GENERIC_WRITE, 0,
IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)

' Verify that the obtained handle is valid.
If hSerialPort.ToInt32 = -1 Then
Throw New CommException("Unable to obtain a handle to the COM port")
End If

' Retrieve the current control settings.
Success = GetCommState(hSerialPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to retrieve the current control
settings")
End If

' Modify the properties of MyDCB
MyDCB.BaudRate = baudRate
MyDCB.ByteSize = byteSize
MyDCB.Parity = parity
MyDCB.StopBits = stopBits
MyDCB.fBitFields = MyDCB.fBitFields Or fRTSControlDisable Or
fDTRControlDisable

' Reconfigure port based on the properties of MyDCB.
Success = SetCommState(hSerialPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to reconfigure COM")
End If

' Retrieve the current time-out settings.
Success = GetCommTimeouts(hSerialPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to retrieve current time-out
settings")
End If

' Modify the properties of MyCommTimeouts
MyCommTimeouts.ReadIntervalTimeout = 5
MyCommTimeouts.ReadTotalTimeoutConstant = 3000
MyCommTimeouts.ReadTotalTimeoutMultiplier = 0
MyCommTimeouts.WriteTotalTimeoutConstant = 0
MyCommTimeouts.WriteTotalTimeoutMultiplier = 0

' Reconfigure the time-out settings, based on the properties of
MyCommTimeouts.
Success = SetCommTimeouts(hSerialPort, MyCommTimeouts)
If Success = False Then
Throw New CommException("Unable to reconfigure the time-out settings")
End If

'Flush any garbage on port
flushComIn()
End Sub

'Closes the physical port, and prepares this object for destruction
Public Sub Dispose() Implements System.IDisposable.Dispose
Static disposed As Boolean = False
' Release the handle to port.
CloseHandle(hSerialPort)
disposed = True
End Sub

'Flushes any data already received on the port
Public Sub flushComIn()
Dim ExistingCommTimeouts As COMMTIMEOUTS
Dim tempCommTimeouts As COMMTIMEOUTS
Dim success As Boolean
Dim BytesRead As Int32
Dim Buffer(32) As Byte

' Retrieve the current time-out settings.
success = GetCommTimeouts(hSerialPort, ExistingCommTimeouts)

With tempCommTimeouts
.ReadIntervalTimeout = 1
.WriteTotalTimeoutConstant = 0
.WriteTotalTimeoutMultiplier = 0
.ReadTotalTimeoutConstant = 1
End With

' Temporarily reconfigure the time-out settings
success = SetCommTimeouts(hSerialPort, tempCommTimeouts)

'Read data from port.
success = ReadFile(hSerialPort, Buffer, 32, BytesRead, IntPtr.Zero)

'Reset the time-out settings to previous values
success = SetCommTimeouts(hSerialPort, ExistingCommTimeouts)

End Sub

Class CommException
Inherits ApplicationException

Sub New(ByVal Reason As String)
MyBase.New(Reason)
End Sub

End Class

End Class
"evle" <ev****@msn.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
haw to read data from an Infrared Infrared Remote Control

Nov 28 '06 #2
i only want to read one char 0x000000000000128C
can you help?
thanks any way
Nov 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: halukg | last post by:
I am trying to send a 6 byte char array from the serial port in new C# 2005 Express: com.Write(new string(new char { (char)34, (char)14, (char)192, (char)51, (char)0, (char)0 }, 0, 6)); I am...
1
by: David | last post by:
I have written an application in VB.NET 2003 that uses the SAX serial component for RS232 communications with hardware. The program sets up 2 serial ports so that it can talk to 2 different...
2
by: joe | last post by:
Has anyone seen any source code that shows how to use the serial port in VB 2005 (System.IO.Port)? Especially how to use the asynchronous read? John
1
by: henrycortezwu | last post by:
Hi All, I'm trying to connect to a virtual port (COM19, OUTGOING, "Bluetooth Serial Port") using VS2005 System.IO.Ports. When I ran the ff code below here's what happens. 1) VS2005 Compiles w/o...
0
by: pandi | last post by:
Hi, I am using a check scanner.It has functions which gives output like image ,checknumber. i am using serial port to get this output in my coding. My problem is i am not able to read the display...
5
by: Franklin M. Gauer III | last post by:
Hi All, I've written an ASP.NET application (webservice) that does simple serial communications via the .NET 2.0 SerialComm object. The application runs fine on my development machine. The...
4
by: cmdolcet69 | last post by:
This code below write a serial command to the com port then read the results and disaplays it into the label1.text property when button1_click event is triggered. I think what is going on is that...
2
by: Nasif | last post by:
Currently I am writing a program which sends and receives messages through serial port to a device. I am using C# and Microsoft Visual studio 2005 for windows program. But my problem is when i try...
0
by: Dhananjay | last post by:
Hi, I am working on an VB.Net application which I want to communicate to external device using comm port (Serial Port) . So for that first I am trying to simulate the communication on serial...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.