[go: up one dir, main page]

Menu

[r993]: / trunk / VTS3 / ptp / WinPTP.cpp  Maximize  Restore  History

Download this file

136 lines (104 with data), 2.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// WinPTP.cpp: implementation of the WinPTP class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "VTS.h"
#include "WinPTP.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
UINT WinPTPThreadFunc( LPVOID pParam );
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
WinPTP::WinPTP( const char *config )
{
InitPort(config);
}
void WinPTP::Indication( const BACnetNPDU &pdu )
{
BACnetOctet *msg;
// check for nothing to send
if (pdu.pduLen == 0)
return;
// allocate a buffer big enough for the data
msg = new BACnetOctet[ pdu.pduLen ];
memcpy( msg, pdu.pduData, pdu.pduLen );
SendData(msg, pdu.pduLen);
delete msg;
}
void WinPTP::SendData( BACnetOctet *data, int len )
{
byte framebuf[1014]; //max size we need if all are escaped
int framelength;
//get PTP frame
ptpNPDUtoFrame(data, len, framebuf, sizeof(framebuf), &framelength);
FilterData( framebuf, framelength, portSending );
ptpTransmit(data, len);
}
WinPTP::~WinPTP()
{
ptpDisconnect(0);
ptpDeinit();
}
void WinPTP::InitPort(const char *config)
{
if( ptpInit(config) != ptpERSuccess )
{
TRACE("can not init the serial port");
portStatus = -1;
}
if( ptpConnect("Password") != ptpERSuccess )
{
TRACE("can not connect the serial port");
portStatus = -1;
}
m_Continue = true;
// start it suspended
m_Thread = AfxBeginThread( WinPTPThreadFunc, this,
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );
// dont let windows delete the thread object
m_Thread->m_bAutoDelete = FALSE;
// now let it run
// Called to resume execution of a thread created with the CREATE_SUSPENDED flag
m_Thread->ResumeThread();
// clear the port status, all is well
portStatus = 0;
}
UINT WinPTPThreadFunc( LPVOID pParam )
{
WinPTPPtr pServer = (WinPTPPtr)pParam;
BACnetAddress srcAddr;
PKTPTP packet;
byte buf[1014]; //max size we need if all are escaped
int length;
// packet.pktPreamble = 0x55FF;
// packet.pktFrameType = 2;
// packet.pktLen=7;
// packet.pktHeaderCRC = 0x21;
// packet.pktData[0] = 10;
// packet.pktData[1] = 10;
// packet.pktData[2] = 10;
// packet.pktData[3] = 99;
// packet.pktData[4] = 0xBA;
// packet.pktData[5] = 0xC0;
// packet.pktData[6] = 0xAB;
// packet.pktDataCRC = 0x94E3;
while (pServer->m_Continue)
{
if(ptpReceive(&packet))
{
TRACE("receive ptp data");
//get PTP frame
ptpGetFrame(packet, buf, sizeof(buf), &length);
// let the application filter it
pServer->FilterData( buf, length, BACnetPort::portReceiving );
pServer->Response( BACnetNPDU(srcAddr, packet.pktData, packet.pktLen));
}
else
Sleep(50);
}
return 0;
}