//研华输出卡PCI1752输出状态回读,在实例化后调用此函数的函数前面要加 unsafe /// <summary> /// 研华输出卡PCI1752输出状态回读,在实例化后调用此函数的函数前面要加 unsafe /// </summary> /// <returns>返回输出位状态的数据结构</returns> public unsafe Bits GetOutputStatus() { Bits TempBits=new Bits(); try { //Initial the return value; for (int a = 0; a < 64; a++) { TempBits.OutBits[a] = false; } if (PasswordIsCorrect == false || SuccessBuiltNew == false) { return TempBits; } //Read the output status first for each port, total 8 ports byte ReadPortData = 0; for (int Port = 0; Port <= 7; Port++) { if (NeedFormControlFlag == true) { TargetPCI1752Card.Read(Port, out ReadPortData); } else { TargetDOCard.DoRead(Port, out ReadPortData); } //Judge the status and change the return value for each bit of port for (int Bit = 0; Bit <= 7; Bit++) { //Once the bit is 1, then set the return value bit as true if ((ReadPortData >> Bit & 0x1) == 1) { TempBits.OutBits[Port * 8 + Bit] = true; } else { TempBits.OutBits[Port * 8 + Bit] = false; } } } return TempBits; } catch (Exception ex) { ErrorMessage = ex.Message; return TempBits; } }
public bool SetOutBit(int iBit, bool bOn) { if (bInitOK == false) { return(false); } ErrorCode ret; int port = iBit / 8; int Chanalbit = iBit % 8; lock (lockObj) { //更新现在值 ret = m_dio.DoRead(0, 8, portData); string strTest; for (int i = 0; i < 8; i++) { strTest = Convert.ToString(portData[i], 2); strTest = strTest.PadLeft(8, '0'); for (int j = 0; j < 8; j++) { if (strTest[7 - j] == '1') { bBitStatus[i * 8 + j] = true; } else { bBitStatus[i * 8 + j] = false; } } } byte portValue; if (bOn) { portValue = (byte)(portData[port] | (1 << (Chanalbit))); } else { byte ValveTest = (byte)~(1 << (Chanalbit)); portValue = (byte)(portData[port] & ValveTest); } //写入值 ret = m_dio.DoWrite(port, portValue); } return(true); }