如何实现流驱动中xxx_read函数传递多组数据
我现在编写了一个4路AD信号采集的驱动,驱动具体代码如下:
DWORD ADC_Read(DWORD hOpenContext,LPVOID pBuffer,DWORD Count)
{
int Channel[]={0,1,2,3};
pADCreg->ADCCON &= 0x8000;//initiallize ADCCON Register
pADCreg->ADCCON |= (1 << 14);//use prescale
pADCreg->ADCCON |= (ADCPRS << 6);// set prescale
pADCreg->ADCTSC &= ~(1<<2); //set ADCTSC normal ADC mode
////////读取4个通道AD转换的值//////////////////////////
for(int i=0; i<4; i++)
{
pADCreg->ADCCON &= ~(7<<3); //clear channel number
pADCreg->ADCCON |= ( Channel[i]<<3 ); //Choose Channel
pADCreg->ADCCON |= 1;//Enable Convertion
while(pADCreg->ADCCON & 0x1);//check if Enable_start is low
while(!(0x8000&pADCreg->ADCCON));// Check ECFLG
///////////////////Read Buffer/////////////////////////
*((DWORD *)pBuffer+i) = ((pADCreg->ADCDAT0) &= 0x03ff);//将数据传给Read Buffer
RETAILMSG(1,(TEXT("ADC_READ: Read Success! Channel_%x: '%x', ADCCON: '%x'\r\n"),i,
*((DWORD *)pBuffer+i),pADCreg->ADCCON));
}
//////////////////Read Buffer Over/////////////////////
return 4;
}
我自己调试发现应用程序中取地址的过程(蓝色标识部分)是错误的,取到的地址并不是驱动中pbuffer+1,pbuffer+2,pbuffer+3传过来的地址;同时我在Readfile函数中想要取16个字节大小的内容,但实际只取了4个字节(actlen的反馈值,也就是一个pbuffer的大小)的内容,出错的原因可能就在这里。所以现在产生两个问题:
unsafe private void button5_Click(object sender, EventArgs e)
{
int actlen;
bool ret;
float V;
uint m_DispAIN= 0;
uint Temp_DispAIN0 = 0, Temp_DispAIN1 = 0, Temp_DispAIN2 =0, Temp_DispAIN3 = 0;
ret = ReadFile(ADC, &m_DispAIN, 16, &actlen, IntPtr.Zero);
if (ret != true)
MessageBox.Show("read adc channel failed!");
else
{
// Channel 00
Temp_DispAIN0 = *(&m_DispAIN);
textBox1.Text = Convert.ToString(Temp_DispAIN0);
V = (float)(Temp_DispAIN0) / 1024; /* 转换为小数 */
Temp_DispAIN0 = (uint)(V * 5500); /* 计算出电压值 */
textBox2.Text = Convert.ToString(Temp_DispAIN0);
// Channel 01
Temp_DispAIN1 = *(&m_DispAIN + 32); textBox3.Text = Convert.ToString(Temp_DispAIN1);
V = (float)(Temp_DispAIN1) / 1024; /* 转换为小数 */
Temp_DispAIN1 = (uint)(V * 5500); /* 计算出电压值 */
textBox4.Text = Convert.ToString(Temp_DispAIN1);
//Channel 02
Temp_DispAIN2 = *(&m_DispAIN + 64); textBox5.Text = Convert.ToString(Temp_DispAIN2);
V = (float)(Temp_DispAIN2) / 1024; /* 转换为小数 */
Temp_DispAIN2 = (uint)(V * 5500); /* 计算出电压值 */
textBox6.Text = Convert.ToString(Temp_DispAIN2);
// Channel 03
Temp_DispAIN3 = *(&m_DispAIN + 96); textBox7.Text = Convert.ToString(Temp_DispAIN3);
V = (float)(Temp_DispAIN3) / 1024; /* 转换为小数 */
Temp_DispAIN3 = (uint)(V * 5500); /* 计算出电压值 */
textBox8.Text = Convert.ToString(Temp_DispAIN3);
}
}