Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
actually my problem is that i have a programm which is able to read RS232 but it saves in the folder.txt and i want it to save in the data base can you help please and thanks for paying attention to my request look this is my code:
C++
//---------------------------------------------------------------------------
‪#‎include‬ <stdio.h> 
#include <string.h> 
#include <windows.h> 
#include <windef.h> 
#include <stdlib.h> 
#include <iostream>
#include <cstdlib>
#include <ctime>
‪#‎pragma‬ hdrstop
//---------------------------------------------------------------------------
#pragma argsused 
using namespace std;
int main() 
{
HANDLE handle,hStdout,handle_FILE_TEXT; 
DCB dcb; 
COMMTIMEOUTS timeouts={0}; 
BOOL fSuccess; 
int ExitRemoteMode=255; 
int EnterRemoteModeNow=70; 
BOOL bResult,PortReady;
BYTE*buf2[2000]={0}; 
DWORD nblu,nbecrit;
int tosend2,tosend1;
/*--------------------------------------------------------------------*/ 
/* Ouverture du port de communication pour l'ANALYSEUR DE SPECTRE */ 
/*--------------------------------------------------------------------*/ 
hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
handle_FILE_TEXT = CreateFile( 
"c:intissar.txt", 
GENERIC_WRITE | GENERIC_READ, 
0, 
NULL, 
CREATE_ALWAYS, 
FILE_ATTRIBUTE_SYSTEM, 
NULL 
) ; 
if(handle_FILE_TEXT == INVALID_HANDLE_VALUE) 
{ 
printf("Impossible to open the port (error %d)\n", GetLastError()); 
return 0;
getchar(); 
}
handle = CreateFile( 
"COM1", 
GENERIC_READ | GENERIC_WRITE , 
0, 
NULL, 
OPEN_EXISTING, 
FILE_ATTRIBUTE_NORMAL, 
NULL 
) ;
/*-----------------------------------------------------------*/ 
/* Verifier si handle ouvert correctement */ 
/*-----------------------------------------------------------*/
if(handle == INVALID_HANDLE_VALUE) 
{ 
printf("Impossible to open the port (error %d)\n", GetLastError()); 
return 0;
}
/*-----------------------------------------------------------*/ 
/* Ajustement des parametres */ 
/*-----------------------------------------------------------*/
PortReady=SetupComm(handle,5000,5000); 
fSuccess = GetCommState(handle, &dcb);
if (!fSuccess) 
{ 
printf("problem 1");
getchar(); 
}
dcb.BaudRate = CBR_9600 ; /* speed */ 
dcb.ByteSize =8 ; 
dcb.Parity=NOPARITY; 
dcb.StopBits = ONESTOPBIT; 
dcb.fNull=FALSE; 
dcb.fRtsControl=RTS_CONTROL_DISABLE; 
dcb.fInX=FALSE; 
dcb.fOutX=FALSE; 
dcb.fDtrControl=DTR_CONTROL_DISABLE;
PortReady=SetCommState(handle,&dcb);
if(!SetCommState(handle,&dcb)) 
{ 
printf("problem 2"); 
getchar();
}
/*-----------------------------------------------------------*/ 
/* Les timeouts */ 
/*-----------------------------------------------------------*/
timeouts.ReadIntervalTimeout=500; 
timeouts.ReadTotalTimeoutMultiplier=550; 
timeouts.ReadTotalTimeoutConstant=550; 
timeouts.WriteTotalTimeoutMultiplier=550; 
timeouts.WriteTotalTimeoutConstant=550;
SetCommTimeouts(handle,&timeouts);
if(!SetCommTimeouts(handle,&timeouts)) 
{ 
printf("Problem for timeouts (erreur %d)\n", GetLastError()); 
return 0; 
}
/*-----------------------------------------------------------*/ 
/* reception de donnees */ 
/*-----------------------------------------------------------*/
tosend1=17; 
tosend2=0;
/* je dois lui envoyer une serie de byte pour lui dire de m'envoyer les !)% BYTE qui m'enteressent */
WriteFile(handle,&EnterRemoteModeNow,1,&nbecrit,NULL); 
WriteFile(handle,&tosend1,1,&nbecrit,NULL); 
WriteFile(handle,&tosend2,1,&nbecrit,NULL);
/*Receive Data from the spectrum analyser*/ 
ReadFile(handle,&buf2,2014,&nblu,NULL);
/*Write to the screen*/ 
WriteFile(hStdout,buf2,2014,&nbecrit,NULL);
/* save in intissar.txt what the spectrum analyser has sent*/ 
WriteFile(handle_FILE_TEXT,buf2,2014,&nbecrit,NULL);
WriteFile(handle,&ExitRemoteMode,1,&nbecrit,NULL);
CloseHandle(handle_FILE_TEXT); 
CloseHandle(handle); 
CloseHandle(hStdout); 
getchar();
return(fSuccess);
}
Posted
Updated 22-May-14 6:57am
v2
Comments
[no name] 21-May-14 16:21pm    
Okay so replace the file writing code with code to write to your database. What exactly is the problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900