Problème avec le port série

swatch72
Messages postés
1
Date d'inscription
lundi 10 novembre 2003
Statut
Membre
Dernière intervention
10 novembre 2003
- 10 nov. 2003 à 14:13
hbarou
Messages postés
1
Date d'inscription
mercredi 7 mai 2003
Statut
Membre
Dernière intervention
16 octobre 2004
- 16 oct. 2004 à 12:58
Bonjour,

Je sois récupérer des informations météo depuis un capteur sur le port série. J'utilise un module (SerialPort.bas) pour effectuer les requetes sur le port série et lire les réponses.
Lorsque j'exécute le programme depuis l'interface VB, pas de problème. Lorsque je compile et crée le .EXE, alors plus de communication.
Je vous joins le code source du module.
Si vous pouviez m'expliquer ce qu'il faut que je change, cela me ferait gagner un temps fou.
D'avance merci.

Olivier.

________________________________________

Option Explicit

Global ComNum As Long
Global bRead(255) As Byte

Type COMSTAT
fCtsHold As Long
fDsrHold As Long
fRlsdHold As Long
fXoffHold As Long
fXoffSent As Long
fEof As Long
fTxim As Long
fReserved As Long
cbInQue As Long
cbOutQue As Long
End Type

Type COMMTIMEOUTS
ReadIntervalTimeout As Long
ReadTotalTimeoutMultiplier As Long
ReadTotalTimeoutConstant As Long
WriteTotalTimeoutMultiplier As Long
WriteTotalTimeoutConstant As Long
End Type

Type DCB
DCBlength As Long
BaudRate As Long
fBinary As Long
fParity As Long
fOutxCtsFlow As Long
fOutxDsrFlow As Long
fDtrControl As Long
fDsrSensitivity As Long
fTXContinueOnXoff As Long
fOutX As Long
fInX As Long
fErrorChar As Long
fNull As Long
fRtsControl As Long
fAbortOnError As Long
fDummy2 As Long
wReserved As Integer
XonLim As Integer
XoffLim As Integer
ByteSize As Byte
Parity As Byte
StopBits As Byte
XonChar As Byte
XoffChar As Byte
ErrorChar As Byte
EofChar As Byte
EvtChar As Byte
End Type

Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function GetLastError Lib "kernel32" () As Long
Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Declare Function GetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Declare Function BuildCommDCB Lib "kernel32" Alias "BuildCommDCBA" (ByVal lpDef As String, lpDCB As DCB) As Long
Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long

Function fin_com()
fin_com = CloseHandle(ComNum)
End Function

Function FlushComm()
FlushFileBuffers (ComNum)
End Function

Function Init_Com(ComNumber As String, Comsettings As String) As Boolean
On Error GoTo handelinitcom
Dim ComSetup As DCB, Answer, Stat As COMSTAT, RetBytes As Long
Dim retval As Long
Dim CtimeOut As COMMTIMEOUTS, BarDCB As DCB
' Open the communications port for read/write (&HC0000000).
' Must specify existing file (3).
ComNum = CreateFile(ComNumber, &HC0000000, 0, 0&, &H3, 0, 0)
If ComNum = -1 Then
MsgBox "Com Port " & ComNumber & " not available. Use Serial settings (on the main menu) to setup your ports.", 48
Init_Com = False
Exit Function
End If
'Setup Time Outs for com port
CtimeOut.ReadIntervalTimeout = 20
CtimeOut.ReadTotalTimeoutConstant = 1
CtimeOut.ReadTotalTimeoutMultiplier = 1
CtimeOut.WriteTotalTimeoutConstant = 10
CtimeOut.WriteTotalTimeoutMultiplier = 1
retval = SetCommTimeouts(ComNum, CtimeOut)
If retval = -1 Then
retval = GetLastError()
MsgBox "Unable to set timeouts for port " & ComNumber & " Error: " & retval
retval = CloseHandle(ComNum)
Init_Com = False
Exit Function
End If
retval = BuildCommDCB(Comsettings, BarDCB)
If retval = -1 Then
retval = GetLastError()
MsgBox "Unable to build Comm DCB " & Comsettings & " Error: " & retval
retval = CloseHandle(ComNum)
Init_Com = False
Exit Function
End If
retval = SetCommState(ComNum, BarDCB)
If retval = -1 Then
retval = GetLastError()
MsgBox "Unable to set Comm DCB " & Comsettings & " Error: " & retval
retval = CloseHandle(ComNum)
Init_Com = False
Exit Function
End If

Init_Com = True
handelinitcom:
Exit Function
End Function

Function ReadCommPure() As String
On Error GoTo handelpurecom
Dim RetBytes As Long, i As Integer, ReadStr As String, retval As Long
Dim CheckTotal As Integer, CheckDigitLC As Integer
retval = ReadFile(ComNum, bRead(0), 255, RetBytes, 0)
ReadStr = ""
If (RetBytes > 0) Then
For i = 0 To RetBytes - 1
ReadStr = ReadStr & Chr(bRead(i))
Next i
Else
FlushComm
End If
'Return the string read from serial port
ReadCommPure = ReadStr
handelpurecom:
Exit Function
End Function

Function WriteCOM32(COMString As String) As Integer
On Error GoTo handelwritelpt
Dim RetBytes As Long, LenVal As Long
Dim retval As Long

If Len(COMString) > 255 Then
WriteCOM32 Left$(COMString, 255)
WriteCOM32 Right$(COMString, Len(COMString) - 255)
Exit Function
End If

For LenVal = 0 To Len(COMString) - 1
bRead(LenVal) = Asc(Mid$(COMString, LenVal + 1, 1))
Next LenVal
' bRead(LenVal) = 0
retval = WriteFile(ComNum, bRead(0), Len(COMString), RetBytes, 0)
' FlushComm
WriteCOM32 = RetBytes

handelwritelpt:
Exit Function
End Function

4 réponses

cs_phil23
Messages postés
79
Date d'inscription
mardi 23 septembre 2003
Statut
Membre
Dernière intervention
14 juillet 2005
1
10 nov. 2003 à 20:17
Pourquoi perdre beaucoup de temps à essayer d'utiliser ce module ? Utilise plutôt le contrôle MSCOMM de VB6 ou la classe Rs232 de VB.net.
0
Ickik
Messages postés
193
Date d'inscription
lundi 27 janvier 2003
Statut
Membre
Dernière intervention
10 mars 2010
2
12 nov. 2003 à 12:51
bonjour,
le composant MSCOMM a été créé pour palier a ce genre de besoins qui est trop lourd d'apres moi.
En entrant simplement les parametres tu peux controler ton port serie.
Il y a meme des sources en exemple nu peu partout sur le site.
A+
0
cs_himi
Messages postés
2
Date d'inscription
jeudi 8 avril 2004
Statut
Membre
Dernière intervention
8 avril 2004

8 avril 2004 à 21:15
himi
0
hbarou
Messages postés
1
Date d'inscription
mercredi 7 mai 2003
Statut
Membre
Dernière intervention
16 octobre 2004

16 oct. 2004 à 12:58
Ton appli m'interresse, en effet je dois developper un module d'ecoute sur un port serie mais je ne peux pas utiliser mscomm32.
Est ce que tu pourrais me contacter s'il te plait. MErci
0