求救关于 “access violation at address××:wtite of address××"的问题
这是我编写的一段用来采集设备读取的数据的程序,在Timer1Timer里测试读数据的情况。单步时候可以正常执行几遍,然后跳出这个错误,出现CPU的画面,不知道是什么原因。这个问题都折腾了好几天了,求高手帮忙。
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include "stdlib.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE gm_hDevice;
int h,w;
int CurNum=0;
int gm_nPlus=16;
unsigned long gm_nCounts;
short gm_pBuffer[16*10];
shortgm_pBufferTemp[16];
floatgm_Temp1;
floatgm_Temp2;
intgm_nRangeY;
int gm_nrTimes=0;//Y方向每导间隔
int ReadDataFalse=0;
//intgm_nStartPointy=0;//Y方向绘制开始点;
int ReadDataTrue=0;
HINSTANCE gm_Handle;
FARPROC gm_OpenDevice = NULL,
gm_ReadData =NULL,
gm_CloseDevice = NULL;
bool ReadDataResult =false;
bool CloseResult = false;
HANDLE (*lpOpenDevice)()= NULL;
bool (*lpReadData)(HANDLE ,short* ,ULONG *);
bool (*lpCloseDevice)(HANDLE);
//---------------------------------------
//---------------------------------------
typedef struct _PARAM
{
unsigned shortnSenseDegree;
unsigned shortnHightFre;
unsigned shortnTimeConstant;
unsigned shortnWorkFre;
unsigned shortnModeOfSign;
unsigned shortnGateOfJam;
unsigned shortnHold1;
unsigned shortnHold2;
}STRU_PARAM,*P_STRU_PARAM;
//---------------------------------------------------------------------
extern "C" _declspec(dllexport) HANDLE __stdcall OpenDevice();
extern "C" _declspec(dllexport) bool __stdcall CloseDevice(HANDLE hDevice);
extern "C" _declspec(dllexport) bool __stdcall ReadData(HANDLE hDevice,short* pBuffer,ULONG *nCounts);
extern "C" _declspec(dllexport) STRU_PARAM __stdcall ReadParam(HANDLE hDevice);
//-----------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Button3->Enabled=false;
Button4->Enabled=false;
Button5->Enabled=false;
}
//------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Button1->Enabled = false;
Button3->Enabled = false ;
Button4->Enabled = true;
Button5->Enabled = true;
Button7->Enabled = false;
Timer1->Enabled = true;
//静态调用EEGAMP.dll中的OpenDevice函数
gm_Handle = LoadLibrary("EEGAMP.dll");
gm_OpenDevice = GetProcAddress(gm_Handle,"OpenDevice");
gm_ReadData = GetProcAddress(gm_Handle,"ReadData");
gm_CloseDevice = GetProcAddress(gm_Handle,"CloseDevice");
lpOpenDevice = (HANDLE(__cdecl *)())gm_OpenDevice;
lpReadData = (bool(__cdecl *)(HANDLE,short* ,ULONG *))gm_ReadData;
lpCloseDevice = (bool(__cdecl *)(HANDLE))gm_CloseDevice;
gm_hDevice = lpOpenDevice();
if(gm_hDevice == INVALID_HANDLE_VALUE) Label1->Caption = "打开失败!";
}
//------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
do
{
ReadDataResult = lpReadData(gm_hDevice,&gm_pBuffer[0],&gm_nCounts);
ReadDataFalse++;
if(ReadDataFalse == 10) //连续10次未读到数据就关闭设备,重新打开
{
ReadDataFalse=0;
lpCloseDevice(gm_hDevice);
gm_hDevice = lpOpenDevice();
lpReadData(gm_hDevice,&gm_pBuffer[0],&gm_nCounts);
break;
}
}
while(ReadDataResult == false);
if(ReadDataResult==true)
{
ReadDataTrue++;
}
}
//---------------------------------------------
[解决办法]
“步时候可以正常执行几遍,然后跳出这个错误,出现CPU的画面,”
运行到哪个位置 会出错?
[解决办法]
//连续10次未读到数据就关闭设备,重新打开
do
{
ReadDataResult = lpReadData(gm_hDevice,&gm_pBuffer[0],&gm_nCounts);
ReadDataFalse++;
if(ReadDataFalse == 10) //连续10次未读到数据就关闭设备,重新打开
{
ReadDataFalse=0;
lpCloseDevice(gm_hDevice);不知道关闭成功没有?
gm_hDevice = lpOpenDevice();//不知道gm_hDevice 是否是NULL?
lpReadData(gm_hDevice,&gm_pBuffer[0],&gm_nCounts);//执行此处读取之后,下一步如果ReadDataResult ==false则马上执行ReadDataResult = lpReadData(gm_hDevice,&gm_pBuffer[0],&gm_nCounts);//连续执行两次会不会有问题。
break;
}
}
while(ReadDataResult == false);
if(ReadDataResult==true)
{
ReadDataTrue++;
}