You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
209 lines
4.1 KiB
C++
209 lines
4.1 KiB
C++
#include "Pci365CardMgr.h"
|
|
|
|
Pci365CardMgr* Pci365CardMgr::m_instance = nullptr;
|
|
|
|
void Pci365CardMgr::InitPci365Card()
|
|
{
|
|
if (m_bIniPci365Card)
|
|
{
|
|
return;
|
|
}
|
|
m_OutPortStateLow = 0;//记录当前输出端口的状态--用于向端口写数据
|
|
m_OutPortStateHigh = 0;//记录当前输出端口的状态--用于向端口写数据
|
|
|
|
if (ReadDll())//读取dll
|
|
{
|
|
//自己初始化pci 卡
|
|
IniPciCard();
|
|
|
|
//关闭激光端口
|
|
CtrlLaserGate(false);
|
|
}
|
|
}
|
|
|
|
bool Pci365CardMgr::ReadDll()
|
|
{
|
|
m_PciCh365Handle = LoadLibrary(L"CH365DLL.DLL");
|
|
if (!m_PciCh365Handle)
|
|
{
|
|
LaserLogger::GetInstance()->WriteLog(0, "LoadLibrary CH365DLL.DLL is error");
|
|
return false;
|
|
}
|
|
//读取需要的函数地址
|
|
bool result = true;
|
|
result &= ((CH365mOpenDevice = (pCH365mOpenDevice)GetProcAddress(m_PciCh365Handle, "CH365mOpenDevice")) != NULL);
|
|
result &= ((CH365mReadIoByte = (pCH365mReadIoByte)GetProcAddress(m_PciCh365Handle, "CH365mReadIoByte")) != NULL);
|
|
result &= ((CH365mSetA15_A8 = (pCH365mSetA15_A8)GetProcAddress(m_PciCh365Handle, "CH365mSetA15_A8")) != NULL);
|
|
result &= ((CH365mWriteIoByte = (pCH365mWriteIoByte)GetProcAddress(m_PciCh365Handle, "CH365mWriteIoByte")) != NULL);
|
|
result &= ((CH365mCloseDevice = (pCH365mCloseDevice)GetProcAddress(m_PciCh365Handle, "CH365mCloseDevice")) != NULL);
|
|
|
|
if (!result)
|
|
{
|
|
LaserLogger::GetInstance()->WriteLog(0, "CH365mOpenDevice is error");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Pci365CardMgr::IniPciCard()
|
|
{
|
|
//初始化pci 卡
|
|
m_DeviceHandle = CH365mOpenDevice(0, true, true);
|
|
if ((int)m_DeviceHandle == -1)
|
|
{
|
|
LaserLogger::GetInstance()->WriteLog(0, "ch365 pci is not discovered");
|
|
return false;
|
|
}
|
|
|
|
//初始化输入端口(否则输入端口无法读取)
|
|
UCHAR ChByte = 0xFF;
|
|
CH365mSetA15_A8(0, ChByte);//恢复高8位地址
|
|
|
|
LaserLogger::GetInstance()->WriteLog(0, "ch365 pci IniPciCard is OK");
|
|
|
|
m_bIniPci365Card = true;
|
|
|
|
return true;
|
|
}
|
|
|
|
void Pci365CardMgr::CtrlLaserGate(bool bon)
|
|
{
|
|
SPciPort PciPort;
|
|
PciPort.num = LASER_PORT;
|
|
/*if (gMirror->IsLaserSwitch())
|
|
bon = !bon;
|
|
*/
|
|
try
|
|
{
|
|
WritePortState(PciPort, bon);
|
|
}
|
|
catch (...)
|
|
{
|
|
LaserLogger::GetInstance()->WriteLog(0, "function:WritePortState is error");
|
|
}
|
|
}
|
|
|
|
void Pci365CardMgr::WritePortState(SPciPort PciPort, bool bOpen)
|
|
{
|
|
//检查pci 初始化状态
|
|
if (m_bIniPci365Card == false)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (PciPort.num == -1)
|
|
return;
|
|
//处理高八位还是低八位
|
|
bool bLow = LowOrHight(PciPort.num);
|
|
//获得输出端口状态
|
|
long &OutPortState = GetOutPortState(bLow);
|
|
//获得端口地址
|
|
int PortAddr = GetPortAddr(bLow, true);
|
|
//反向操作
|
|
if (PciPort.bReverse)
|
|
{
|
|
bOpen = !bOpen;
|
|
}
|
|
|
|
if (bOpen)
|
|
{
|
|
OutPortState = SetBitOn(OutPortState, PciPort.num);
|
|
WriteIoPortByte(PortAddr, OutPortState);
|
|
}
|
|
else
|
|
{
|
|
OutPortState = SetBitOff(OutPortState, PciPort.num);
|
|
WriteIoPortByte(PortAddr, OutPortState);
|
|
}
|
|
}
|
|
|
|
bool Pci365CardMgr::LowOrHight(int &PortNum)
|
|
{
|
|
bool bLow = true;//低八位
|
|
if (PortNum >= PORT_CNT)//高八位
|
|
{
|
|
bLow = false;
|
|
PortNum -= PORT_CNT;//统一8 位来处理
|
|
}
|
|
return bLow;
|
|
}
|
|
|
|
long& Pci365CardMgr::GetOutPortState(bool bLow)
|
|
{
|
|
if (bLow)
|
|
return m_OutPortStateLow;
|
|
else
|
|
return m_OutPortStateHigh;
|
|
}
|
|
|
|
int Pci365CardMgr::GetPortAddr(bool bLow, bool bWirte)
|
|
{
|
|
int addr;
|
|
if (bLow)//低八位
|
|
{
|
|
if (bWirte)
|
|
{
|
|
addr = OUT_PORT_ADDR_L;
|
|
}
|
|
else
|
|
{
|
|
addr = IN_PORT_ADDR_L;
|
|
}
|
|
}
|
|
else//高八位
|
|
{
|
|
if (bWirte)
|
|
{
|
|
addr = OUT_PORT_ADDR_H;
|
|
}
|
|
else
|
|
{
|
|
addr = IN_PORT_ADDR_H;
|
|
}
|
|
}
|
|
return addr;
|
|
}
|
|
|
|
long Pci365CardMgr::SetBitOn(long PortVal, int PortIdx)
|
|
{
|
|
PortVal = PortVal | (1 << PortIdx);
|
|
return PortVal;
|
|
}
|
|
|
|
BYTE Pci365CardMgr::SetBitOff(BYTE PortVal, int PortIdx)
|
|
{
|
|
PortVal = (PortVal)&(~(1 << PortIdx));
|
|
return PortVal;
|
|
}
|
|
|
|
void Pci365CardMgr::WriteIoPortByte(int PortAddr, long PortVal)
|
|
{
|
|
UCHAR ChByte;
|
|
if (PortAddr < 255)
|
|
{
|
|
USHORT mAddr = (USHORT)PortAddr;
|
|
ChByte = (UCHAR)PortVal;
|
|
CH365mWriteIoByte(0, (PVOID)mAddr, ChByte);//端口写入一次时间大约需要4us
|
|
}
|
|
else
|
|
{
|
|
ChByte = PortAddr >> 8;
|
|
CH365mSetA15_A8(0, ChByte);//设定高8位地址
|
|
|
|
USHORT mAddr = (USHORT)(PortAddr % 256);
|
|
ChByte = (UCHAR)PortVal;
|
|
CH365mWriteIoByte(0, (PVOID)mAddr, ChByte);
|
|
ChByte = 0xFF;
|
|
CH365mSetA15_A8(0, ChByte);//恢复高8位地址
|
|
}
|
|
}
|
|
|
|
Pci365CardMgr::Pci365CardMgr()
|
|
{
|
|
}
|
|
|
|
|
|
Pci365CardMgr::~Pci365CardMgr()
|
|
{
|
|
}
|