使用winio对直接访问IO PORT
2005-08-17 09:55
531 查看
首先下载winio库,查看readme在vc++下使用,参考写了个0x70读cmos和0x61控制speaker
void io_init()
{
DWORD dwPortVal;
DWORD dwPortVal2;
DWORD dwMemVal;
char port[128];
char mem[128];
char test[128];
bool bResult;
HANDLE hPhysicalMemory;
PBYTE pbLinAddr;
// Call InitializeWinIo to initialize the WinIo library.
bResult = InitializeWinIo();
if (bResult)
{
// Under Windows NT/2000/XP, after calling InitializeWinIo,
// you can call _inp/_outp instead of using GetPortVal/SetPortVal
OnReadCmos();
OnSpeakerSound();
// Map physical addresses 0xA0000 - 0xAFFFF into the linear address space
// of the application. The value returned from the call to MapPhysToLin is
// a linear address corresponding to physical address 0xA0000. In case of
// an error, the return value is NULL.
pbLinAddr = MapPhysToLin((PBYTE)0xA0000, 65536, &hPhysicalMemory);
if (pbLinAddr)
{
// Now we can use pbLinAddr to access physical address 0xA0000
*pbLinAddr = 10;
// When you're done with pbLinAddr, call UnmapPhysicalMemory
UnmapPhysicalMemory(hPhysicalMemory, pbLinAddr);
}
// Instead of using MapPhysToLin, we can use GetPhysLong/SetPhysLong
SetPhysLong((PBYTE)0xA0000, 10);
GetPhysLong((PBYTE)0xA0000, &dwMemVal);
sprintf(mem,"%x", dwMemVal);
write_log(NORMAL_LOG,"%s|%d: %s/n",__FILE__,__LINE__,mem);
// When you're done using WinIo, call ShutdownWinIo
ShutdownWinIo();
}
else
{
printf("Error during initialization of WinIo./n");
exit(1);
}
}
// 0x70是CMOS索引端口(只写)
// 0x71是CMOS数据端口
DWORD ReadCmos(DWORD index)
{
DWORD dwPortVal;
SetPortVal(0x70,index, 4);
GetPortVal(0x71, &dwPortVal, 4);
return dwPortVal;
}
void OnReadCmos()
{
CString strInfo;
CString strTmp;
DWORD data;
strInfo = "COMS DATA:/r/n";
// 读出CMOS 128个字节
for(int i=0;i<128;i++)
{
data = ReadCmos(i);
write_log(NORMAL_LOG,"%s|%d: Byte %02X:/t%02X/r/n",__FILE__,__LINE__,i,data);
strTmp.Format("Byte %02X:/t%02X/r/n", i, data);
strInfo+=strTmp;
}
GetDlgItem(IDC_EDIT)tWindowText(strInfo);
}
void OnSpeakerSound()
{
// TODO: Add your command handler code here
// C调
// 1 = 262 Hz
Sound(262);
Sleep(500);
NoSound();
// 2 = 288 Hz
Sound(288);
Sleep(500);
NoSound();
// 3 = 320 Hz
Sound(320);
Sleep(500);
NoSound();
}
// 0x61是speaker控制端口
// 0x43是8253/8254定时器控制端口
// 0x42是8253/8254定时器通道2的端口
void CRadioDlg::Sound(DWORD freq ){
DWORD data;
if(freq>=20 && freq<=20000)
{
freq = 1193181 / freq;
GetPortVal(0x61, &data, 4);
if((data & 3) == 0)
{
SetPortVal(0x61,data | 3, 4);
SetPortVal(0x43,0xb6, 4);
}
SetPortVal(0x42,(BYTE)(freq%256), 4);
SetPortVal(0x42,(BYTE)(freq/256), 4);
}
}
void NoSound( void )
{
DWORD data;
GetPortVal(0x61, &data, 1);
SetPortVal(0x61, data & 0xfc, 1);
}
void io_init()
{
DWORD dwPortVal;
DWORD dwPortVal2;
DWORD dwMemVal;
char port[128];
char mem[128];
char test[128];
bool bResult;
HANDLE hPhysicalMemory;
PBYTE pbLinAddr;
// Call InitializeWinIo to initialize the WinIo library.
bResult = InitializeWinIo();
if (bResult)
{
// Under Windows NT/2000/XP, after calling InitializeWinIo,
// you can call _inp/_outp instead of using GetPortVal/SetPortVal
OnReadCmos();
OnSpeakerSound();
// Map physical addresses 0xA0000 - 0xAFFFF into the linear address space
// of the application. The value returned from the call to MapPhysToLin is
// a linear address corresponding to physical address 0xA0000. In case of
// an error, the return value is NULL.
pbLinAddr = MapPhysToLin((PBYTE)0xA0000, 65536, &hPhysicalMemory);
if (pbLinAddr)
{
// Now we can use pbLinAddr to access physical address 0xA0000
*pbLinAddr = 10;
// When you're done with pbLinAddr, call UnmapPhysicalMemory
UnmapPhysicalMemory(hPhysicalMemory, pbLinAddr);
}
// Instead of using MapPhysToLin, we can use GetPhysLong/SetPhysLong
SetPhysLong((PBYTE)0xA0000, 10);
GetPhysLong((PBYTE)0xA0000, &dwMemVal);
sprintf(mem,"%x", dwMemVal);
write_log(NORMAL_LOG,"%s|%d: %s/n",__FILE__,__LINE__,mem);
// When you're done using WinIo, call ShutdownWinIo
ShutdownWinIo();
}
else
{
printf("Error during initialization of WinIo./n");
exit(1);
}
}
// 0x70是CMOS索引端口(只写)
// 0x71是CMOS数据端口
DWORD ReadCmos(DWORD index)
{
DWORD dwPortVal;
SetPortVal(0x70,index, 4);
GetPortVal(0x71, &dwPortVal, 4);
return dwPortVal;
}
void OnReadCmos()
{
CString strInfo;
CString strTmp;
DWORD data;
strInfo = "COMS DATA:/r/n";
// 读出CMOS 128个字节
for(int i=0;i<128;i++)
{
data = ReadCmos(i);
write_log(NORMAL_LOG,"%s|%d: Byte %02X:/t%02X/r/n",__FILE__,__LINE__,i,data);
strTmp.Format("Byte %02X:/t%02X/r/n", i, data);
strInfo+=strTmp;
}
GetDlgItem(IDC_EDIT)tWindowText(strInfo);
}
void OnSpeakerSound()
{
// TODO: Add your command handler code here
// C调
// 1 = 262 Hz
Sound(262);
Sleep(500);
NoSound();
// 2 = 288 Hz
Sound(288);
Sleep(500);
NoSound();
// 3 = 320 Hz
Sound(320);
Sleep(500);
NoSound();
}
// 0x61是speaker控制端口
// 0x43是8253/8254定时器控制端口
// 0x42是8253/8254定时器通道2的端口
void CRadioDlg::Sound(DWORD freq ){
DWORD data;
if(freq>=20 && freq<=20000)
{
freq = 1193181 / freq;
GetPortVal(0x61, &data, 4);
if((data & 3) == 0)
{
SetPortVal(0x61,data | 3, 4);
SetPortVal(0x43,0xb6, 4);
}
SetPortVal(0x42,(BYTE)(freq%256), 4);
SetPortVal(0x42,(BYTE)(freq/256), 4);
}
}
void NoSound( void )
{
DWORD data;
GetPortVal(0x61, &data, 1);
SetPortVal(0x61, data & 0xfc, 1);
}
相关文章推荐
- 使用WinIo直接访问IO
- 使用WinIo直接访问IO
- Spring boot项目 使用ip+port+contextPath进行访问的时候会直接显示出项目中的一些接口信息和相关数据
- Struts2.1.8 验证框架(validation)的使用中遇到的一些问题(直接访问jsp遇到的错误)
- 使用fwrite()和fread()函数直接访问文件
- 虚拟字符驱动,申请n页内存,使用mmap映射到应用程序空间,用户就可以直接访问不需要任何同步机制
- 使用webpack构建本地服务器,实现自动刷新,并直接在手机上访问
- 在DELPHI中使用ADO直接访问Excel数据文件
- C# System.IO.FileStream 读取被其他程序打开的文件提示“文件正由另一进程使用,因此该进程无法访问该文件。”
- 使用.NET身份验证防止不登录直接访问页面 .
- js使用闭包时,内部函数是直接访问外部函数的实际变量而非复制一份新变量
- 在ipv6下使用veno可以直接访问ipv4
- Struts2直接访问Servlet API(IoC--不推荐使用,非IoC---推荐使用)
- javascript 直接创建对象与间接创建对象 私有函数不能访问this变量 是否使用var声明变量几个问题探讨
- 在nginx中使用lua直接访问mysql和memcaced达到数据接口的统一
- tomcat部署web应用,并使用域名(IP)直接访问
- 【转贴】在Solaris中使用用户态程序直接访问I/O端口 - 上海嵌入式家园 贺工
- Tomcat去除项目名称和端口号,直接使用ip地址访问项目的方法
- 关于使用FileMapping和直接操作File+缓存的IO性能对比
- 关于weblogic 访问应用直接使用ip就可到达的配置方法