PDA

View Full Version : Lettura seriale


Fenomeno85
14-12-2003, 13:50
Allora siccome in basic non so neanche da dove si parte allora mi chiedo come faccio a leggere la seriale?

Allora il codice del pic in una funzione ha l'istruzione:

printf ("%d", valore);

in basic come faccio a pigliare questo valore??

>Sempre E Solo Lei<

maxithron
14-12-2003, 21:13
www.vbitalia.it/static/comm2.htm

Fenomeno85
31-12-2003, 17:14
grazie per il link

allora sto impazzendo :cry:
questo è il mio codice, ma perchè non vuole creare sta connessione con la porta?? Mi da sempre -1

Private Type DCB
DCBlength As Long
BaudRate As Long
fBitFields 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
wReserved1 As Integer
End Type

'dichiarazione per poter leggere la porta
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, _
ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal _
lpOverlapped As Long) As Long

'dichiarazione per creare una connessione verso una porta
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile _
As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB As DCB) As Long

Private Sub cmdinvia_Click()
Dim Situazione_Porta As DCB
GetCommState nPorta, Situazione_Porta
Dim Situazione As String
Situazione = "Baud Rate: " & Situazione_Porta.BaudRate & vbCrLf & _
"Byte Size: " & Situazione_Porta.ByteSize & vbCrLf & "--------------------------" & vbCrLf
Select Case Situazione_Porta.Parity
Case NOPARITY: Situazione = Situazione & "Parity: No Parity" & vbCrLf
Case ODDPARITY: Situazione = Situazione & "Parity: Odd" & vbCrLf
Case EVENPARITY: Situazione = Situazione & "Parity: Even" & vbCrLf
Case MARKPARITY: Situazione = Situazione & "Parity: Mark" & vbCrLf
Case SPACEPARITY: Situazione = Situazione & "Parity: Space" & vbCrLf
End Select
Select Case Situazione_Porta.StopBits
Case ONESTOPBIT: Situazione = Situazione & "Stop Bits: 1" & vbCrLf
Case ONE5STOPBITS: Situazione = Situazione & "Stop Bits 1 1/2" & vbCrLf
Case TWOSTOPBITS: Situazione = Situazione & "Stop Bits: 2" & vbCrLf
End Select
MsgBox Situazione
End Sub

'serve a chiudere la porta aperta in precedenza se si chiude con la x
Private Sub Form_Unload(Cancel As Integer)
CloseHandle nPorta
End Sub

Private Sub Esci_Click()
End
CloseHandle nPorta
End Sub

'procedura di avvio che perve a stabilire una connessione
Private Sub Form_Load()
Dim nPorta As Long 'l'intero di riferimento alla porta
nPorta = CreateFile("LTP1", GENERIC_READ Or GENERIC_WRITE, 0, 0&, OPEN_EXISTING, 0, 0)
lblnporta.Caption = nPorta
If nPorta = -1 Then
MsgBox "Impossibile accedere alla porta seriale"
Else
Timer1.Enabled = True
End If
End Sub

'rileva dati dalla porta
Private Sub Timer1_Timer()
Const Lunghezza_Buffer = 3
Dim Dati_Ricevuti As Long 'indica quanti dati abbiamo rivecuto
Dim Carattere(1 To Lunghezza_Buffer) As Byte
vRitorno = ReadFile(nPorta, Carattere(1), Lunghezza_Buffer, Dati_Ricevuti, 0)
If Dato_Ricevuto <> 0 Then
For i = 1 To Dati_Ricevuti
If i = 1 Then
txttemperatura = Carattere(i)
End If
If i = 2 Then
txtacqua = Carattere(i)
End If
If i = 3 Then
txtvento = Carattere(i)
End If
Next i
End If
End Sub

Fenomeno85
02-01-2004, 13:30
gente help me :cry:

anche se conoscete un altro metodo mi va bene basta che riesca a leggere sti benedetti dati ;)

>Sempre E Solo Lei<

Fenomeno85
03-01-2004, 18:25
uppino :cry:

~§~ Sempre E Solo Lei ~§~

Fenomeno85
04-01-2004, 19:13
nessuno la sa sta cosa??

cionci neppure te?? :wtf:

~§~ Sempre E Solo Lei ~§~

Fenomeno85
05-01-2004, 13:53
83 visite e nessuno mi da una mano :cry:

~§~ Sempre E Solo Lei ~§~

cionci
05-01-2004, 16:45
Ma non hai detto che ti volevi collegare con la seriale ?!?!?! Allora perchè c'è "LTP1" sulla CreateFile ?!?!?

cionci
05-01-2004, 16:48
Sinceramente non ho mia visto una guida migliore:

http://www.vbitalia.it/static/comm1.htm
http://www.vbitalia.it/static/comm2.htm
http://www.vbitalia.it/static/comm3.htm
http://www.vbitalia.it/static/comm4.htm
http://www.vbitalia.it/static/comm5.htm

Fenomeno85
05-01-2004, 20:02
cionci il codice vien da li! E, il problema è che da sempre -1 quando cerca di fare la connessione!

C'è LPT1 perchè avevo provato a vedere se su quella porta riuscivo a collegarmi ma non funziona :(

~§~ Sempre E Solo Lei ~§~

Fenomeno85
06-01-2004, 13:20
cionci perchè non mi vuole mai aprire la connessione con una maledettissima porta?

~§~ Sempre E Solo Lei ~§~

cionci
06-01-2004, 14:55
A me quel codice in VB non funziona, ma lo stesso codice riprotato in C funziona perfettamente...

#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM1";

hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);

if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}

// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.

fSuccess = GetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}

// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.

dcb.BaudRate = CBR_57600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit

fSuccess = SetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}

printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
return (0);
}

Fenomeno85
06-01-2004, 17:02
cionci sai per caso se attraverso c posso comunicare o con un database MySQL oppure con Access?

~§~ Sempre E Solo Lei ~§~

cionci
07-01-2004, 03:42
Certo...entrambi tramite il rispettivo driver ODBC...
Ecco le API: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odch21apr.asp

maxithron
07-01-2004, 09:51
mmh...il codice vb a me funziona...non è che ti manca il controllo mscomm.ocx?