form en segundo plano
Publicado por corben dallas (11 intervenciones) el 22/07/2005 18:08:39
Hola a todos; con el siguiente codigo (que he sacado de la web de microsoft):
Public Structure DCB
Public DCBlength As Int32
Public BaudRate As Int32
Public fBitFields As Int32
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
Public Structure COMMTIMEOUTS
Public ReadIntervalTimeout As Int32
Public ReadTotalTimeoutMultiplier As Int32
Public ReadTotalTimeoutConstant As Int32
Public WriteTotalTimeoutMultiplier As Int32
Public WriteTotalTimeoutConstant As Int32
End Structure
Public Const GENERIC_READ As Int32 = &H80000000
Public Const GENERIC_WRITE As Int32 = &H40000000
Public Const OPEN_EXISTING As Int32 = 3
Public Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
Public Const NOPARITY As Int32 = 0
Public Const ONESTOPBIT As Int32 = 0
Public 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
Public Declare Auto Function GetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
ByRef lpDCB As DCB) As Boolean
Public Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
ByRef lpDCB As DCB) As Boolean
Public Declare Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Public Declare Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Public 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
Public 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
Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean
Form1.ActiveForm.FindForm.Enabled = True
Dim hSerialPort, hParallelPort As IntPtr
Dim Success As Boolean
Dim MyDCB As DCB
Dim MyCommTimeouts As COMMTIMEOUTS
Dim BytesWritten, BytesRead As Int32
Dim Buffer() As Byte
' Declare variables to use for encoding.
Dim oEncoder As New System.Text.ASCIIEncoding
Dim oEnc As System.Text.Encoding = oEncoder.GetEncoding(1252)
' Convert String to Byte().
Buffer = oEnc.GetBytes("Test")
' Serial port.
' Obtain a handle to the COM1 serial port.
hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
Verify that the obtained handle is valid.
f hSerialPort.ToInt32 = -1 Then
Throw New CommException("Unable to obtain a handle to the COM1 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 as appropriate.
' WARNING: Make sure to modify the properties in accordance with their supported values.
MyDCB.BaudRate = 9600
MyDCB.ByteSize = 8
MyDCB.Parity = NOPARITY
MyDCB.StopBits = ONESTOPBIT
' Reconfigure COM1 based on the properties of MyDCB.
Success = SetCommState(hSerialPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to reconfigure COM1")
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 as appropriate.
' WARNING: Make sure to modify the properties in accordance with their supported values.
MyCommTimeouts.ReadIntervalTimeout = 0
MyCommTimeouts.ReadTotalTimeoutConstant = 0
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
' Write data to COM1.
Success = WriteFile(hSerialPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
If Success = False Then
'Throw New CommException("Unable to write to COM1")
End If
' Read data from COM1.
Success = ReadFile(hSerialPort, Buffer, BytesWritten, BytesRead, IntPtr.Zero)
If Success = False Then
MsgBox("Problemas al leer el puerto serie")
End If
' Release the handle to COM1.
Success = CloseHandle(hSerialPort)
Form1.ActiveForm.Refresh()
Leo el puerto serie, que lo tengo conectado a un sensor el cual lo activa, yme realiza la accion que yo quiera. Todo funciona perfectamente, pero el problema que tengo es que no me parace la form por ningun lado y no se pueden ver los mensajes que envio. He probado a poner temporizadores pero ho hay manera.
Se queda en segundo plano.
Public Structure DCB
Public DCBlength As Int32
Public BaudRate As Int32
Public fBitFields As Int32
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
Public Structure COMMTIMEOUTS
Public ReadIntervalTimeout As Int32
Public ReadTotalTimeoutMultiplier As Int32
Public ReadTotalTimeoutConstant As Int32
Public WriteTotalTimeoutMultiplier As Int32
Public WriteTotalTimeoutConstant As Int32
End Structure
Public Const GENERIC_READ As Int32 = &H80000000
Public Const GENERIC_WRITE As Int32 = &H40000000
Public Const OPEN_EXISTING As Int32 = 3
Public Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
Public Const NOPARITY As Int32 = 0
Public Const ONESTOPBIT As Int32 = 0
Public 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
Public Declare Auto Function GetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
ByRef lpDCB As DCB) As Boolean
Public Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
ByRef lpDCB As DCB) As Boolean
Public Declare Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Public Declare Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Public 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
Public 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
Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean
Form1.ActiveForm.FindForm.Enabled = True
Dim hSerialPort, hParallelPort As IntPtr
Dim Success As Boolean
Dim MyDCB As DCB
Dim MyCommTimeouts As COMMTIMEOUTS
Dim BytesWritten, BytesRead As Int32
Dim Buffer() As Byte
' Declare variables to use for encoding.
Dim oEncoder As New System.Text.ASCIIEncoding
Dim oEnc As System.Text.Encoding = oEncoder.GetEncoding(1252)
' Convert String to Byte().
Buffer = oEnc.GetBytes("Test")
' Serial port.
' Obtain a handle to the COM1 serial port.
hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
Verify that the obtained handle is valid.
f hSerialPort.ToInt32 = -1 Then
Throw New CommException("Unable to obtain a handle to the COM1 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 as appropriate.
' WARNING: Make sure to modify the properties in accordance with their supported values.
MyDCB.BaudRate = 9600
MyDCB.ByteSize = 8
MyDCB.Parity = NOPARITY
MyDCB.StopBits = ONESTOPBIT
' Reconfigure COM1 based on the properties of MyDCB.
Success = SetCommState(hSerialPort, MyDCB)
If Success = False Then
Throw New CommException("Unable to reconfigure COM1")
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 as appropriate.
' WARNING: Make sure to modify the properties in accordance with their supported values.
MyCommTimeouts.ReadIntervalTimeout = 0
MyCommTimeouts.ReadTotalTimeoutConstant = 0
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
' Write data to COM1.
Success = WriteFile(hSerialPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
If Success = False Then
'Throw New CommException("Unable to write to COM1")
End If
' Read data from COM1.
Success = ReadFile(hSerialPort, Buffer, BytesWritten, BytesRead, IntPtr.Zero)
If Success = False Then
MsgBox("Problemas al leer el puerto serie")
End If
' Release the handle to COM1.
Success = CloseHandle(hSerialPort)
Form1.ActiveForm.Refresh()
Leo el puerto serie, que lo tengo conectado a un sensor el cual lo activa, yme realiza la accion que yo quiera. Todo funciona perfectamente, pero el problema que tengo es que no me parace la form por ningun lado y no se pueden ver los mensajes que envio. He probado a poner temporizadores pero ho hay manera.
Se queda en segundo plano.
Valora esta pregunta
![Me gusta: Está pregunta es útil y esta clara Me gusta: Está pregunta es útil y esta clara](/img/img.png?11.51)
![NO me gusta: Está pregunta no esta clara o no es útil No me gusta: Está pregunta no esta clara o no es útil](/img/img.png?11.51)
0