Newsgroup: it.comp.programmare.win32
Titolo: acquisire da seriale

Salve
vorrrei gentilmente qualche indicazione su come acquisire dati da seriale.
Ho un oggetto che scarica dei dati (una sequenza di numeri) dalla seriale
ma non ho il software, come potrei procedere?
grazie

H2O ha scritto:

Per gestire la seriale dai un'occhiata a CreateFile/ReadFile/WriteFile e
API connesse (in MSDN c'è tutto). Però poi devi avere il protocollo di
comunicazione del dispositivo a cui ti colleghi. Senza protocollo... beh,
o è qualcosa di molto semplice oppure difficilmente potrai intuire il
significato dei dati ricevuti. Comincia col collegarti usando un software
di terminale e monitorare la seriale.
HTH,
Paolo.
-- 
questo articolo e` stato inviato via web dal servizio gratuito 
http://www.newsland.it/news
 segnala gli abusi ad 
ab...@newsland.it


Io ho implementato alcuni driver per dei digitizer su porta seriale.
Le funzioni che ho utilizzato sono  queste (piu' o meno)
//Accedere alla porta seriale (ovviamente al posto di COM1 metti la porta
che ti interessa
HANDLE hPort = CreateFile ("COM1", GENERIC_READ | GENERIC_WRITE,  0,  NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//Recupero i parametri della porta
DCB dcb;
GetCommState(hPort, &dcb);
//Ad esempio cambio la velocita'
dcb.BaudRate = 19200;
//Aggiorno i parametri cambiati
SetCommState(hPort, &dcb);
//Leggo i dati (loop unfinito, ma si puo' interrompere quando vuoi)
 for (;;)
{
  COMSTAT ComStat;
  DWORD ErrorFlags;
  char Buffer[8192];  //Buffer di lettura di 8K
  //Controllo se ho qualcosa da leggere...
  ClearCommError (hPort, &ErrorFlags, &ComStat);
  if (ComStat.cbInQue > 0)
  {
        //Leggo dalla porta
        ReadFile (*m_hCom, Buffer, (ComStat.cbInQue > 8192) ? 8192 :
ComStat.cbInQue, &ComStat.cbInQue, NULL);
        //Faccio qualche cosa con Buffer...
  }
}
//Chiudo la porta
CloseHandle (hPort);



#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// SerialPort.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CSerialPort window
//////////////////////////////////////////////////////////////////////
// SerialPort.h: implementation of the CSerialPort class.
// Written by Shibu K.V (
shi...@erdcitvm.org
)
// Copyright (c) 2002
//
// To use CSerialPort, follow these steps:
//   - Copy the files SerialPort.h & SerialPort.cpp and paste it in
your
//		Projects Directory.
//   - Take Project tab from your VC++ IDE,Take Add to
Project->Files.Select files 
//		SerialPort.h & SerialPort.cpp and click ok
//	 -	Add the line #include "SerialPort.h" at the top of
your Dialog's Header File.
//   - Create an instance of CSerialPort in your dialogs header
File.Say
//		CSerialPort port;
 
// Warning: this code hasn't been subject to a heavy testing, so
// use it on your own risk. The author accepts no liability for the 
// possible damage caused by this code.
//
// Version 1.0  7 Sept 2002.
//////////////////////////////////////////////////////////////////////
class CSerialPort 
{
// Construction
public:
	CSerialPort();
// Attributes
public:
// Operations
public:
// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSerialPort)
	//}}AFX_VIRTUAL
// Implementation
public:
	void ClosePort();
	BOOL ReadByte(BYTE &resp);
	BOOL WriteByte(BYTE bybyte);
	BOOL OpenPort(CString portname);
	BOOL SetCommunicationTimeouts(DWORD ReadIntervalTimeout,DWORD
ReadTotalTimeoutMultiplier,DWORD ReadTotalTimeoutConstant,DWORD
WriteTotalTimeoutMultiplier,DWORD WriteTotalTimeoutConstant);
	BOOL ConfigurePort(DWORD BaudRate,BYTE ByteSize,DWORD
fParity,BYTE  Parity,BYTE StopBits);
	HANDLE hComm;
	DCB      m_dcb;
	COMMTIMEOUTS m_CommTimeouts;
	BOOL     m_bPortReady;
	BOOL     bWriteRC;
	BOOL     bReadRC;
	DWORD iBytesWritten;
	DWORD iBytesRead;
	DWORD dwBytesRead;
	~CSerialPort();
};
// SerialPort.cpp : implementation file
//
#include <afxwin.h>
#include "SerialPort.h"
/*
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
*/
/////////////////////////////////////////////////////////////////////////////
// SerialPort.cpp: implementation of the CSerialPort class.
// Written by Shibu K.V (
shi...@erdcitvm.org
)
// Copyright (c) 2002
//
// To use CSerialPort, follow these steps:
//   - Copy the files SerialPort.h & SerialPort.cpp and paste it in
your
//		Projects Directory.
//   - Take Project tab from your VC++ IDE,Take Add to
Project->Files.Select files 
//		SerialPort.h & SerialPort.cpp and click ok
//	 -	Add the line #include "SerialPort.h" at the top of
your Dialog's Header File.
//   - Create an instance of CSerialPort in your dialogs header
File.Say
//		CSerialPort port;
 
// Warning: this code hasn't been subject to a heavy testing, so
// use it on your own risk. The author accepts no liability for the 
// possible damage caused by this code.
//
// Version 1.0  7 Sept 2002.
//////////////////////////////////////////////////////////////////////
// CSerialPort
CSerialPort::CSerialPort()
{
}
CSerialPort::~CSerialPort()
{
}
/////////////////////////////////////////////////////////////////////////////
// CSerialPort message handlers
BOOL CSerialPort::OpenPort(CString portname)
{
portname= "//./" +portname;
hComm = CreateFile(portname,
                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      0,
                      OPEN_EXISTING,
                      0,
                      0);
		if(hComm==INVALID_HANDLE_VALUE){
		//MessageBox("Cannot open Communication
Port.Please\nQuit the program and Re-start your PC.","Com Port
Error",MB_OK+MB_ICONERROR);
		return false;}
		else
			return true;
}
BOOL CSerialPort::ConfigurePort(DWORD BaudRate, BYTE ByteSize, DWORD
fParity, BYTE Parity, BYTE StopBits)
{
	if((m_bPortReady = GetCommState(hComm, &m_dcb))==0){
		MessageBox(NULL,"GetCommState
Error","Error",MB_OK+MB_ICONERROR);
		CloseHandle(hComm);
	return false;}
m_dcb.BaudRate =BaudRate;
m_dcb.ByteSize = ByteSize;
m_dcb.Parity =Parity ;
m_dcb.StopBits =StopBits;
m_dcb.fBinary=TRUE;
m_dcb.fDsrSensitivity=false;
m_dcb.fParity=fParity;
m_dcb.fOutX=false;
m_dcb.fInX=false;
m_dcb.fNull=false;
m_dcb.fAbortOnError=TRUE;
m_dcb.fOutxCtsFlow=FALSE;
m_dcb.fOutxDsrFlow=false;
m_dcb.fDtrControl=DTR_CONTROL_DISABLE;
m_dcb.fDsrSensitivity=false;
m_dcb.fRtsControl=RTS_CONTROL_DISABLE;
m_dcb.fOutxCtsFlow=false;
m_dcb.fOutxCtsFlow=false;
m_bPortReady = SetCommState(hComm, &m_dcb);
if(m_bPortReady ==0){
		MessageBox(NULL,"SetCommState
Error","Error",MB_OK+MB_ICONERROR);
		CloseHandle(hComm);
	return false;}
return true;
}
BOOL CSerialPort::SetCommunicationTimeouts(DWORD ReadIntervalTimeout,
DWORD ReadTotalTimeoutMultiplier, 
DWORD ReadTotalTimeoutConstant, 
DWORD WriteTotalTimeoutMultiplier, 
DWORD WriteTotalTimeoutConstant)
{
if((m_bPortReady = GetCommTimeouts (hComm, &m_CommTimeouts))==0)
   return false;
m_CommTimeouts.ReadIntervalTimeout =ReadIntervalTimeout;
m_CommTimeouts.ReadTotalTimeoutConstant =ReadTotalTimeoutConstant;
m_CommTimeouts.ReadTotalTimeoutMultiplier =ReadTotalTimeoutMultiplier;
m_CommTimeouts.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant;
m_CommTimeouts.WriteTotalTimeoutMultiplier
=WriteTotalTimeoutMultiplier;
		m_bPortReady = SetCommTimeouts (hComm,
&m_CommTimeouts);
		if(m_bPortReady ==0){
MessageBox(NULL,"StCommTimeouts function failed","Com Port
Error",MB_OK+MB_ICONERROR);
		CloseHandle(hComm);
		return false;}
		return true;
}
BOOL CSerialPort::WriteByte(BYTE bybyte)
{
	iBytesWritten=0;
if(WriteFile(hComm,&bybyte,1,&iBytesWritten,NULL)==0)
return false;
else return true;
}
BOOL CSerialPort::ReadByte(BYTE	&resp)
{
BYTE rx;
resp=0;
DWORD dwBytesTransferred=0;
if (ReadFile (hComm, &rx, 1, &dwBytesTransferred, 0)){
			 if (dwBytesTransferred == 1){
				 resp=rx;
				 return true;}}
 
	return false;
}
void CSerialPort::ClosePort()
{
CloseHandle(hComm);
return;
}

H2O ha scritto:

Anche se con ritardo, ti volevo segnalare i componenti della TurboPower,
dapprima a pagamento, ora rilasciati OpenSource. Li trovi su
http://sourceforge.net/projects/tpapro/
 (per quanto riguarda la porta
seriale). Dia un'occhiata all'indirizzo
http://www.bsdg.org/resources/turbopower.html
 per altri componenti sempre
della TurboPower.
Saluti
