show me the code
简单到无法用言语来表述,直接使用即可。
第一部分 串口classclass CommCtrl
{
public:
CommCtrl(void);
~CommCtrl(void);
int GetString ();
int SendString(int n);
bool init(char * Comport);
char CMD[20];
private:
DWORD bytes_written ; // Number of bytes written to the port
HANDLE comport ; // Handle COM port
int bStatus;
DCB comSettings; // Contains various port settings
COMMTIMEOUTS CommTimeouts;
};
#include "CommCtrl.h"
CommCtrl::CommCtrl(void)
{
}
CommCtrl::~CommCtrl(void)
{
}
int CommCtrl::SendString(int n)
{
DWORD bytes_written = 0;
int Status = WriteFile(comport, // Handle
CMD, // Outgoing data
n, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);
return Status;
}
int CommCtrl::GetString ()
{
DWORD bytes_read = 0;
int Status = ReadFile(comport, // Handle
CMD, // Incoming data
sizeof(CMD), // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
return Status;
}
bool CommCtrl::init(char * Comport)
{
int nRetCode = 0;
int com = 0;
char port_name[10];
wsprintf( port_name, "COM%s", Comport );
if ((comport =
CreateFile(port_name,
GENERIC_READ | GENERIC_WRITE, // for reading and writing
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
{
return false;
}
// Set timeouts in milliseconds
CommTimeouts.ReadIntervalTimeout = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 50;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 50;
bStatus = SetCommTimeouts(comport,&CommTimeouts);
//if (bStatus != 0)
//{
// return false;
//}
// Set Port parameters.
// Make a call to GetCommState() first in order to fill
// the comSettings structure with all the necessary values.
// Then change the ones you want and call SetCommState().
GetCommState(comport, &comSettings);
// High speed VISCA settings
comSettings.BaudRate = 9600;
comSettings.ByteSize = 8;
comSettings.Parity = NOPARITY;
comSettings.StopBits = ONESTOPBIT;
comSettings.fParity = FALSE;
bStatus = SetCommState(comport, &comSettings);
if (bStatus == 0)
{
return false;
}
return true;
}
第二部分 协议封装
class CSonyD70Control
{
public:
CSonyD70Control(void);
~CSonyD70Control(void);
void ViscaDown (char* buffer, int pan, int tilt);
void ViscaUp (char* buffer, int pan, int tilt);
void ViscaLeft (char* buffer, int pan, int tilt);
void ViscaRight (char* buffer, int pan, int tilt);
void ViscaStop (char* buffer);
void ViscaHome (char* buffer);
void ViscaBoot (char* buffer);
void ViscaSetPreset (char* buffer, int preset);
void ViscaPreset (char* buffer, int preset);
void ViscaZoom (char* buffer, int dir_speed);
void ViscaZoomAuto (char* buffer);
void ViscaLightOn (char* buffer);
void ViscaLightOff (char* buffer);
void ViscaFocusPlus(char * buffer);
void ViscaFocusDec(char * buffer);
void ViscaFocusStop(char * buffer);
};
// Responses
// 90 41 FF = received
// 90 51 FF = done
#include "SonyD70Control.h"
#include
#include
#include
#define ZOOM_IN 0x20
#define ZOOM_OUT 0x30
#define SPEED_0 0x00
#define SPEED_7 0x07
CSonyD70Control::CSonyD70Control(void)
{
}
CSonyD70Control::~CSonyD70Control(void)
{
}
void CSonyD70Control::ViscaUp (char* buffer, int pan, int tilt)
{
wsprintf( buffer, "%c%c%c%c%c%c%c%c%c\0", 0x81, 0x01, 0x06, 0x01, pan, tilt, 0x03, 0x01, 0xFF );
}
void CSonyD70Control::ViscaDown (char* buffer, int pan, int tilt)
{
wsprintf( buffer, "%c%c%c%c%c%c%c%c%c\0", 0x81, 0x01, 0x06, 0x01, pan, tilt, 0x03, 0x02, 0xFF );
}
void CSonyD70Control::ViscaLeft (char* buffer, int pan, int tilt)
{
wsprintf( buffer, "%c%c%c%c%c%c%c%c%c\0", 0x81, 0x01, 0x06, 0x01, pan, tilt, 0x01, 0x03, 0xFF );
}
void CSonyD70Control::ViscaRight (char* buffer, int pan, int tilt)
{
wsprintf( buffer, "%c%c%c%c%c%c%c%c%c\0", 0x81, 0x01, 0x06, 0x01, pan, tilt, 0x02, 0x03, 0xFF );
}
void CSonyD70Control::ViscaStop (char* buffer)
{
wsprintf( buffer, "%c%c%c%c%c%c%c%c%c\0", 0x81, 0x01, 0x06, 0x01, 0x18, 0x18, 0x03, 0x03, 0xFF );
}
void CSonyD70Control::ViscaHome (char* buffer)
{
wsprintf( buffer, "%c%c%c%c%c\0", 0x81, 0x01, 0x06, 0x04, 0xFF );
}
/****************** Initialization *******************/
void CSonyD70Control::ViscaBoot (char* buffer)
{
wsprintf( buffer, "%c%c%c%c%c\0", 0x88, 0x01, 0x00, 0x01, 0xFF );
}
/****************** Presets *******************/
// Preset is 1 thru 16
void CSonyD70Control::ViscaSetPreset (char* buffer, int preset)
{
wsprintf( buffer, "%c%c%c%c%c%c%c\0", 0x81, 0x01, 0x04, 0x3F, 0x01, preset - 1, 0xFF );
}
void CSonyD70Control::ViscaPreset (char* buffer, int preset)
{
wsprintf( buffer, "%c%c%c%c%c%c%c\0", 0x81, 0x01, 0x04, 0x3F, 0x02, preset - 1, 0xFF );
}
/****************** Presets *******************/
void CSonyD70Control::ViscaZoom (char* buffer, int dir_speed)
{
//zoom stop 81 01 04 07 00 FF speed 5
wsprintf( buffer, "%c%c%c%c%c%c\0", 0x81, 0x01, 0x04, 0x07, dir_speed, 0xFF );
}
//void ViscaZoomStop (char* buffer)
//{
// //zoom stop 81 01 04 07 00 FF speed 5
//
// wsprintf( buffer, "%c%c%c%c%c%c\0", 0x81, 0x01, 0x04, 0x07, 0x00, 0xFF );
//}
void CSonyD70Control::ViscaZoomAuto (char* buffer)
{
//zoom stop 81 01 04 07 00 FF speed 5
wsprintf( buffer, "%c%c%c%c%c%c\0", 0x81, 0x01, 0x04, 0x038, 0x02, 0xFF );
}
void CSonyD70Control::ViscaLightOn (char* buffer)
{
//zoom stop 81 01 04 07 00 FF speed 5
wsprintf( buffer, "%c%c%c%c%c%c\0", 0x81, 0x01, 0x04, 0x033, 0x02, 0xFF );
}
void CSonyD70Control::ViscaLightOff (char* buffer)
{
//zoom stop 81 01 04 07 00 FF speed 5
wsprintf( buffer, "%c%c%c%c%c%c\0", 0x81, 0x01, 0x04, 0x033, 0x03, 0xFF );
}
//聚焦加
void CSonyD70Control::ViscaFocusPlus(char * buffer)
{
wsprintf( buffer, "%c%c%c%c%c%c\0", 0x81, 0x01, 0x04, 0x08, 0x03, 0xFF );
}
//聚焦减
void CSonyD70Control::ViscaFocusDec(char * buffer)
{
wsprintf( buffer, "%c%c%c%c%c%c\0", 0x81, 0x01, 0x04, 0x08, 0x02, 0xFF );
}
void CSonyD70Control::ViscaFocusStop(char * buffer)
{
wsprintf( buffer, "%c%c%c%c%c%c\0", 0x81, 0x01, 0x04, 0x08, 0x00, 0xFF );
}