|
发表于 2011-2-19 10:30:00
|
显示全部楼层
再提供你VC下的类库的源码,但是不完整,不过SendMessage之类的是完整的。
头文件:- #if !defined _NXT_BT_H_
- #define _NXT_BT_H_
- #include <windows.h>
- class CNXT_BT_Class
- {
- public:
- CNXT_BT_Class(); // 构造函数,设置串口基本参数,打开串口
- CNXT_BT_Class(char* strComPortName); // 构造函数,设置串口基本参数,打开串口
- ~CNXT_BT_Class(); // 析构函数,关闭串口,释放内存
- private:
- HANDLE hCom; // 文件句柄,指向蓝牙虚拟的串口
- int iRetCode; // 保存返回值,以备查询。无错误时为0
- char* strComPort; // 代表串口的字符串。在我的电脑上应该是"COM8"
- int iBaudRate; // 波特率
- int iMailBoxIdx; // NXT对应的mailbox
- unsigned char byteIn[64]; // 输入用字符串
- unsigned char byteOut[64]; // 输出用字符串
- unsigned char byteTemp[256]; // 临时用字符串
- public:
- int SetMailBoxIndex(int iMailBoxIndex); // 设置NXT mailbox。构造函数中默认设成mailbox 1 (对应值为0)
- int BTSendLogic(bool bLogic); // 设置NXT mailbox的logic value。用于NXT-G中蓝牙设成logic时,true/false
- int BTSendMessage(char* strMessage); // 发送字符串
- int BTReadMessage(); // 读回字符串
- int GetBatteryLevel(); // 读回电池电量,毫伏
- int GetFirmwareVersion(double* ProtocolVer, double* FirmwareVer); // 读回NXT firmware值,尚未写好。
- };
- #endif
复制代码 程序文件:- #include "NXT_BT.H"
- #include <windows.h>
- #include <string.h>
- #include <stdio.h>
- CNXT_BT_Class::CNXT_BT_Class()
- {
- int iRetVal;
- DWORD iErrCode;
- strComPort = new char[strlen("COM8")];
- strComPort = "COM8";
- iBaudRate = 96000;
- iMailBoxIdx = 0;
- hCom=CreateFile("COM8", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
- if(hCom==(HANDLE)-1)
- {
- iRetCode = -1;
- return;
- }
- COMMTIMEOUTS TimeOuts;
- //设定读超时
- TimeOuts.ReadIntervalTimeout=MAXDWORD;
- TimeOuts.ReadTotalTimeoutMultiplier=0;
- TimeOuts.ReadTotalTimeoutConstant=0;
-
- //设定写超时
- TimeOuts.WriteTotalTimeoutMultiplier=100;
- TimeOuts.WriteTotalTimeoutConstant=500;
- iRetVal = SetCommTimeouts(hCom,&TimeOuts); //设置超时
-
- DCB dcb;
- iRetVal = GetCommState(hCom,&dcb);
- // 设定串口参数
- dcb.BaudRate = iBaudRate; //波特率为9600
- dcb.ByteSize = 7; //每个字节有7位
- dcb.Parity = 2; //偶校验
- dcb.StopBits = 0; //无停止位
- iRetVal = SetCommState(hCom, &dcb);
- if (iRetVal == 0)
- {
- iErrCode = GetLastError();
- iRetCode = iErrCode;
- CloseHandle(hCom);
- return;
- }
-
- PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
- iRetCode = 0;
- return;
- }
- CNXT_BT_Class::CNXT_BT_Class(char* strComPortName)
- {
- int iRetVal;
- DWORD iErrCode;
- strComPort = new char[strlen(strComPortName)];
- strComPort = strComPortName;
- iBaudRate = 96000;
- iMailBoxIdx = 0;
- hCom=CreateFile("COM8", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
- if(hCom==(HANDLE)-1)
- {
- iRetCode = -1;
- return;
- }
- ::Sleep(100); // wait the device ready
- COMMTIMEOUTS TimeOuts;
- //设定读超时
- TimeOuts.ReadIntervalTimeout=MAXDWORD;
- TimeOuts.ReadTotalTimeoutMultiplier=0;
- TimeOuts.ReadTotalTimeoutConstant=0;
-
- //设定写超时
- TimeOuts.WriteTotalTimeoutMultiplier=100;
- TimeOuts.WriteTotalTimeoutConstant=500;
- iRetVal = SetCommTimeouts(hCom,&TimeOuts); //设置超时
-
- DCB dcb;
- iRetVal = GetCommState(hCom,&dcb);
- // 设定串口参数
- dcb.BaudRate = iBaudRate; //波特率为9600
- dcb.ByteSize = 7; //每个字节有7位
- dcb.Parity = 2; //偶校验
- dcb.StopBits = 0; //无停止位
- iRetVal = SetCommState(hCom, &dcb);
- if (iRetVal == 0)
- {
- iErrCode = GetLastError();
- iRetCode = iErrCode;
- CloseHandle(hCom);
- return;
- }
-
- PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
- iRetCode = 0;
- return;
- }
- CNXT_BT_Class::~CNXT_BT_Class()
- {
- if (hCom)
- {
- CloseHandle(hCom);
- }
- }
- int CNXT_BT_Class::SetMailBoxIndex(int iMailBoxIndex)
- {
- iMailBoxIdx = iMailBoxIndex;
- return iMailBoxIdx;
- }
- int CNXT_BT_Class::BTSendLogic(bool bLogic)
- {
- DWORD iBytes;
- BOOL bRetVal;
- byteOut[0] = 0x6; // 消息长度6字节
- byteOut[1] = 0x0; // NXT为0
- byteOut[2] = 0x80; // 需要回复。如不需要回复为0x80
- byteOut[3] = 0x9; // 0x09为发送消息
- byteOut[4] = 0x0;
- byteOut[5] = 0x2;
- byteOut[6] = bLogic;
- byteOut[7] = 0x0;
-
- bRetVal = WriteFile(hCom, byteOut, 8, &iBytes, NULL);
- return bRetVal;
- }
- int CNXT_BT_Class::BTSendMessage(char* strMessage)
- {
- DWORD iBytes;
- BOOL bRetVal;
- int iMsgLength;
- iMsgLength = strlen(strMessage);
- byteOut[0] = iMsgLength + 5; // 消息长度
- byteOut[1] = 0x0; // NXT为0
- byteOut[2] = 0x80; // 不需要回复。如需要回复为0x0
- byteOut[3] = 0x9; // 0x09为通过Bluetooth发送信息给NXT
- byteOut[4] = 0x0; // Box Number ---- 1
- byteOut[5] = iMsgLength + 1; // 发送的字符串的字节数
- for (int i=0; i<iMsgLength; i++)
- {
- byteOut[6+i] = *(strMessage+i);
- }
- byteOut[iMsgLength+6] = '\0';
- bRetVal = WriteFile(hCom, byteOut, iMsgLength+7, &iBytes, NULL);
- return iBytes;
- }
- int CNXT_BT_Class::BTReadMessage()
- {
- int iRetVal;
- DWORD iBytes;
- iRetVal = ReadFile(hCom, &byteIn[0], 1, &iBytes, NULL); // 返回消息字节数
- iRetVal = ReadFile(hCom, &byteIn[1], 1, &iBytes, NULL); // NXT为0
- for (int i=2; i<=byteIn[0]+1; i++)
- {
- iRetVal = ReadFile(hCom, &byteIn[i], 1, &iBytes, NULL); // 读回全部消息
- }
- return byteIn[0]+3;
- }
- int CNXT_BT_Class::GetBatteryLevel()
- {
- int iBatteryLevel;
- BOOL bRetVal;
- DWORD iBytes;
- // 以下发命令到NXT主机
- byteOut[0] = 0x2; // 消息长度2字节
- byteOut[1] = 0x0; // NXT为0
- byteOut[2] = 0x0; // 需要回复。如不需要回复为0x80
- byteOut[3] = 0xB; // 0x0B为读回电池状态
- bRetVal = WriteFile(hCom, byteOut, 4, &iBytes, NULL);
- ::Sleep(800); // 给NXT准备的时间。经测试,至少700ms
- // 从NXT主机读回数据,前两字节代表消息长度
- bRetVal = ReadFile(hCom, &byteIn[0], 1, &iBytes, NULL); // 返回消息字节数
- bRetVal = ReadFile(hCom, &byteIn[1], 1, &iBytes, NULL); // NXT为0
- for (int i=2; i<=byteIn[0]+1; i++)
- {
- bRetVal = ReadFile(hCom, &byteIn[i], 1, &iBytes, NULL); // 读回全部消息
- }
- iBatteryLevel = byteIn[5] + byteIn[6]*256; // 换算成电压
- return iBatteryLevel;
- }
- int CNXT_BT_Class::GetFirmwareVersion(double* ProtocolVer, double* FirmwareVer)
- {
- BOOL bRetVal;
- DWORD iBytes;
- // 以下发命令到NXT主机
- byteOut[0] = 0x2; // 消息长度2字节
- byteOut[1] = 0x0; // NXT为0
- byteOut[2] = 0x1; // 需要回复。如不需要回复为0x80
- byteOut[3] = 0x88; // 0x0B为读回电池状态
- bRetVal = WriteFile(hCom, byteOut, 4, &iBytes, NULL);
- ::Sleep(800); // 给NXT准备的时间。经测试,至少700ms
- // 从NXT主机读回数据,前两字节代表消息长度
- for (int i=0; i<7; i++)
- {
- bRetVal = ReadFile(hCom, &byteIn[i], 1, &iBytes, NULL); // 返回消息字节数
- // printf("Byte %d = %d\n", i, byteIn[i]);
- }
- *ProtocolVer = byteIn[4] + (double)(byteIn[3])/100.0; // Protocol Version
- *FirmwareVer = byteIn[6] + (double)(byteIn[5])/100.0; // Firmware Version
- return 1;
- }
复制代码 |
|