本文介绍了ecriture / lecture en C ++ledonnéedela port RS232 com1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我的问题是我有一个能够读取RS232的程序,但它保存在folder.txt中,我希望它保存在数据库中,你可以帮助请,并感谢关注我的请求看看这个是我的代码:

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:

//---------------------------------------------------------------------------
‪#‎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);
}

推荐答案

这篇关于ecriture / lecture en C ++ledonnéedela port RS232 com1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 02:25