📄 dspic.cpp
字号:
// DSPIC.cpp
// CdsPIC is a class created for the 4MHz PIC16LF628-04/SO code
// that was written to provide an RS232 to 2-wire or Bit I/O Interface
// The RS232 Communications Routines used in this file are located
// in the DSIOLIB1.cpp/.h
//Jason Rauch
//March 25, 2002
//***************************************************************************
//---------------------------------------------------------------------------
// Copyright (C) 2002 Dallas Semiconductor Corporation, All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name of Dallas Semiconductor
// shall not be used except as stated in the Dallas Semiconductor
// Branding Policy.
#include "stdafx.h"
#include "DSPIC.h"
#define MAX_PORTNUM 1
//externally defined global vars
extern CString m_strComPort;
extern HANDLE ComID[MAX_PORTNUM];
extern OVERLAPPED osRead[MAX_PORTNUM],osWrite[MAX_PORTNUM];
//externally defined global functions
extern bool AcquireCOMPort(int,char *,char *);
extern void ReleaseCOMPort(int,char *);
extern void FlushCOM(int);
extern int WriteCOM(int, int, UCHAR *);
extern int ReadCOM(int, int, UCHAR *);
bool CdsPic::BoardPresent()
{//Call to Determine If Board Was Detected On Power-up
return m_bBoardPresent;
}
CdsPic::CdsPic()
{ // class constructor, opens serial port detects board.
// looks for board on COM1, 2, 3, and 4
// Open COM Port and Detect Board
m_bComIsOpen = FALSE;
m_bBoardPresent = FALSE;
char ComStr[5] = "COM1";
char ComStrActive[30] = "COM2";
if(!m_bBoardPresent && DetectBoard(ComStr))
{
strcpy(ComStrActive, ComStr);
}
else
{
if(ComStr[3] == '4')
ComStr[3] = '1';
else ComStr[3] = ComStr[3] + 1;
}
if(!m_bBoardPresent && DetectBoard(ComStr))
{
strcpy(ComStrActive, ComStr);
}
else
{
if(ComStr[3] == '4')
ComStr[3] = '1';
else ComStr[3] = ComStr[3] + 1;
}
if(!m_bBoardPresent && DetectBoard(ComStr))
{
strcpy(ComStrActive, ComStr);
}
else
{
if(ComStr[3] == '4')
ComStr[3] = '1';
else ComStr[3] = ComStr[3] + 1;
}
if(!m_bBoardPresent && DetectBoard(ComStr))
{
strcpy(ComStrActive, ComStr);
}
else
{
if(ComStr[3] == '4')
ComStr[3] = '1';
else ComStr[3] = ComStr[3] + 1;
}
if(!m_bBoardPresent)
{
m_bBoardPresent = FALSE;
m_bComIsOpen = FALSE;
// AfxMessageBox("The DS2W board could not be found. Fix and restart application.");
}
else
{
m_bBoardPresent = TRUE;
m_bComIsOpen = TRUE;
strcat(ComStrActive, " Has Been Initialized");
// AfxMessageBox(ComStrActive);
}
}
CdsPic::~CdsPic()
{ // class destructor, closes serial port, resets "present" variables.
char returnstring[80];
if(m_bComIsOpen)
{
ReleaseCOMPort(0, returnstring);
//MessageBox(returnstring);
}
m_bComIsOpen = FALSE;
m_bBoardPresent = FALSE;
}
bool CdsPic::DetectBoard(char *port)
{
// Open COM Port Here
char returnstring[80];
int NoBytes;
m_bComIsOpen = AcquireCOMPort(0, port, returnstring);
if(!m_bComIsOpen)
{
m_bComIsOpen = FALSE;
m_bBoardPresent = FALSE;
return FALSE;
}
else
{
// Port is open
// Now look for board
m_bBoardPresent = FALSE;
Sleep(50); // was 500
FlushCOM(0);
// Now detect board
// Now toggle DTR
if(!ResetBoard())
{
// Close port
ReleaseCOMPort(0, returnstring);
m_bComIsOpen = FALSE;
m_bBoardPresent = FALSE;
return FALSE;
}
// DTR toggled fine
// Read banner
NoBytes = ReadCOM(0,19,m_strBoardBanner);
if(NoBytes == 0)
{
m_strBoardBanner[0] = 0;
// Close port
ReleaseCOMPort(0, returnstring);
m_bComIsOpen = FALSE;
m_bBoardPresent = FALSE;
return FALSE;
}
else
{
m_strBoardBanner[NoBytes] = 0;
m_bBoardPresent = TRUE;
return TRUE;
}
}
return FALSE;
}
UCHAR * CdsPic::HardwareRevision()
{
return &m_strBoardBanner[0];
}
bool CdsPic::Read1(bool &state)
{
unsigned char buf[2]; // Local Rx/Tx com buffer
FlushCOM(0); // Clear COM Port Hardware Buffers
buf[0]=0xE1; // Load buf with read1 Command
buf[1]=0x00; // Second byte dummy byte, required
WriteCOM(0,2,buf); // Send Data via Serial Port
ReadCOM(0,1,buf); // Read for PIC Command Ack (0xf0h)
if(buf[0] == 0xFA)
return false; // return false
else
{
if(buf[0])
state = true;
else
state = false;
return true; // else return true
}
}
bool CdsPic::Read2(bool &state)
{
unsigned char buf[2]; // Local Rx/Tx com buffer
FlushCOM(0); // Clear COM Port Hardware Buffers
buf[0]=0xE2; // Load buf with read2 Command
buf[1]=0x00; // Second byte dummy byte, required
WriteCOM(0,2,buf); // Send Data via Serial Port
ReadCOM(0,1,buf); // Read for PIC Command Ack (0xf0h)
if(buf[0] == 0xFA)
return false; // return false
else
{
if(buf[0])
state = true;
else
state = false;
return true; // else return true
}
}
bool CdsPic::Read3(bool &state)
{
unsigned char buf[2]; // Local Rx/Tx com buffer
FlushCOM(0); // Clear COM Port Hardware Buffers
buf[0]=0xE3; // Load buf with read3 Command
buf[1]=0x00; // Second byte dummy byte, required
WriteCOM(0,2,buf); // Send Data via Serial Port
ReadCOM(0,1,buf); // Read for PIC Command Ack (0xf0h)
if(buf[0] == 0xFA)
return false; // return false
else
{
if(buf[0])
state = true;
else
state = false;
return true; // else return true
}
}
bool CdsPic::Read4(bool &state)
{
unsigned char buf[2]; // Local Rx/Tx com buffer
FlushCOM(0); // Clear COM Port Hardware Buffers
buf[0]=0xE4; // Load buf with read4 Command
buf[1]=0x00; // Second byte dummy byte, required
WriteCOM(0,2,buf); // Send Data via Serial Port
ReadCOM(0,1,buf); // Read for PIC Command Ack (0xf0h)
if(buf[0] == 0xFA)
return false; // return false
else
{
if(buf[0])
state = true;
else
state = false;
return true; // else return true
}
}
bool CdsPic::Read5(bool &state)
{
unsigned char buf[2]; // Local Rx/Tx com buffer
FlushCOM(0); // Clear COM Port Hardware Buffers
buf[0]=0xE5; // Load buf with read5 Command
buf[1]=0x00; // Second byte dummy byte, required
WriteCOM(0,2,buf); // Send Data via Serial Port
ReadCOM(0,1,buf); // Read for PIC Command Ack (0xf0h)
if(buf[0] == 0xFA)
return false; // return false
else
{
if(buf[0])
state = true;
else
state = false;
return true; // else return true
}
}
bool CdsPic::Read6(bool &state)
{
unsigned char buf[2]; // Local Rx/Tx com buffer
FlushCOM(0); // Clear COM Port Hardware Buffers
buf[0]=0xE6; // Load buf with read6 Command
buf[1]=0x00; // Second byte dummy byte, required
WriteCOM(0,2,buf); // Send Data via Serial Port
ReadCOM(0,1,buf); // Read for PIC Command Ack (0xf0h)
if(buf[0] == 0xFA)
return false; // return false
else
{
if(buf[0])
state = true;
else
state = false;
return true; // else return true
}
}
bool CdsPic::Read7(bool &state)
{
unsigned char buf[2]; // Local Rx/Tx com buffer
FlushCOM(0); // Clear COM Port Hardware Buffers
buf[0]=0xE7; // Load buf with read7 Command
buf[1]=0x00; // Second byte dummy byte, required
WriteCOM(0,2,buf); // Send Data via Serial Port
ReadCOM(0,1,buf); // Read for PIC Command Ack (0xf0h)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -