|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Junior Member
Iscritto dal: Oct 2007
Messaggi: 1
|
RS232 lettura con C++
Ciao a tutti, ho un problema
![]() ho un problema con un programma scritto in C++. il progetto che ho preso dalla rete(con consenso dell'autore) aveva un ingresso da porta parallela. Vorrei tramutarla in in gresso RS232 dalla COM1. Ho modificato cosi il codice in modo che il dato che veniva preso dalla parallela ora viene preso dalla com1, peccato che devo aver sbagliato qualcosa, perchè non funziona. Ecco il codice di uno dei file del progetto, quello nel quale ho cambiato le varie cose. // MCP3002.cpp: implementation of the MCP3002 class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "MCP3002.h" #include "MultimediaTimer.h" #include "ADC3002.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif //_________________________________________ //Delcare out timer function //Function must be static static void TimerFunction(void* obj,UINT idEvent); ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// MCP3002::MCP3002() { //inizializzazione membro privato. hComm = CreateFile("COM1", //porta sorgente dati GENERIC_READ,//solo in lettura 0,//handle non condiviso 0, // OPEN_EXISTING,// FILE_FLAG_OVERLAPPED,// 0); if (hComm == INVALID_HANDLE_VALUE)exit(1); // error opening port; abort DCB dcb; BOOL res; dcb.ByteSize = 8; dcb.StopBits =ONESTOPBIT; dcb.BaudRate = CBR_9600; dcb.Parity = NOPARITY; dcb.fAbortOnError = 0; //Set new state res = SetCommState(hComm,&dcb); } MCP3002::~MCP3002() { if (hComm != INVALID_HANDLE_VALUE)CloseHandle(hComm);//chiude se gia inizializato prima } void TimerFunction(void* obj,UINT idEvent) { //This is our timer actevated function //First get a pointer to class functions MCP3002* mcp = (MCP3002*) obj; //Activate class functions mcp->HandleTimer(idEvent); } void MCP3002::HandleTimer(UINT nIDEvant) { //This function is activated from MM timer events //Because we need to make 5000 samples do them if(i_mmcount<=5000) { //GetADC Samples Channel_0[i_mmcount] = ADC_Ch_0(); Channel_1[i_mmcount] = ADC_Ch_1(); //update counter i_mmcount++; } //After we have done all samples stop sampling and MM timer if(i_mmcount>5000) { MMT_Stop();//Stop MMTimer DoEndNotification();//Make a notification to the program } } void MCP3002::MMT_Stop() { //This function stops the MM timer Sleep(10); TEnd = CTime::GetCurrentTime(); //Stop timer MMTimer.StopTimer(); Sleep(10); } void MCP3002::MMT_Start(unsigned short LPT) { //This function sets the LPT type //Zero the counter and after this stop the timer //Now it can be restarted and do samples i_mmcount = 0; LPT_PORT = LPT; //If MMTimer alredy runs stop it now! MMT_Stop(); //Before start timer get the time moment TStart = CTime::GetCurrentTime(); //Do timer event any 2 msec = 500Hz +- 2Hz MMTimer.StartTimer(this,2,10,TimerFunction); } unsigned short MCP3002::ADC_Ch_0() { //adc3002.GetChanne_0(LPT_PORT,10); return (unsigned short)GetByteOnSerial(); } unsigned short MCP3002::GetByteOnSerial() { unsigned short c; DWORD NumberOfBytesRead; DWORD nNumberOfBytesToRead = 1; BOOL bResult; nNumberOfBytesToRead=1; DWORD nNumberOfBytesRead=0; bResult = ReadFile( hComm, &c, nNumberOfBytesToRead, &NumberOfBytesRead, NULL ); FILE *pFile; //ho provato a stampare su un file i dati per vedere cosa pFile=fopen("myfile.txt","w+"); //veniva fuori. scrive solo 6 lettere. if(pFile!=NULL){ fprintf (pFile, "%x\n",&c); fclose (pFile); } if(NumberOfBytesRead>0) return (unsigned short)c; return 0; } unsigned short MCP3002::ADC_Ch_1() { return (unsigned short)GetByteOnSerial(); } void MCP3002::SetEndSampleNotification(void *obj, EndSampleNotification endnotification) { //Sets the end sample notification event //so it will be activated after sampling period object = obj; //See declaration of EndSampleNotification EndNotification = endnotification; } void MCP3002: ![]() { //This function activates a function and pass a parameters //to in our class int len; len = 5000; //activates a function EndNotification see declaretion EndNotification(object,&Channel_0[0],&Channel_1[0],len,TStart,TEnd); } Questo è un file che fa parte di un progetto che potrete trovare per intero a http://www.codeproject.com/tools/ecg_dsp.asp ![]() Se vi viene in mente qualcosa ditemi. Grazie mille!!! |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 05:10.