求救关于 “access violation at address××:wtite of address××"的问题

zhy_nobel 2010-04-15 11:43:45
这是我编写的一段用来采集设备读取的数据的程序,在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];
short gm_pBufferTemp[16];
float gm_Temp1;
float gm_Temp2;
int gm_nRangeY;
int gm_nrTimes=0; //Y方向每导间隔
int ReadDataFalse=0;
//int gm_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 short nSenseDegree;
unsigned short nHightFre;
unsigned short nTimeConstant;
unsigned short nWorkFre;
unsigned short nModeOfSign;
unsigned short nGateOfJam;
unsigned short nHold1;
unsigned short nHold2;
}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++;
}
}
//---------------------------------------------------------------------------------
...全文
120 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhy_nobel 2010-04-19
  • 打赏
  • 举报
回复
还是有问题啊,不过我知道是指针的问题了,谢谢两位。
zhy_nobel 2010-04-16
  • 打赏
  • 举报
回复
一般是在timer的事件执行完一遍的时候出错的。
zhy_nobel 2010-04-16
  • 打赏
  • 举报
回复
谢谢tinki,我修改程序看一下。
tinki 2010-04-16
  • 打赏
  • 举报
回复
//连续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++;
}
周药师 2010-04-15
  • 打赏
  • 举报
回复
“步时候可以正常执行几遍,然后跳出这个错误,出现CPU的画面,”
运行到哪个位置 会出错?

13,826

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧