源代码如下:
#include "stdafx.h"
#include <iostream>
#include "string"
#include <fstream>
#include "Windows.h"
#include "Winbase.h"
#include "SerialPort.h"
using namespace std;
#define RECFRAMELEN 3
#define uchar unsigned char
#define SENDFRAMELEN 10
uchar receiveData[RECFRAMELEN - 1];
uchar sendBuff[SENDFRAMELEN] = {'1',};
uchar AnalysisData(uchar* data)
{
return data[0];
}int main()
{ CSerialPort RS485; for (;;)//循环等待连接串口
{
if (RS485.InitPort(3, 9600))
break;
else
Sleep(50);
}
RS485.WriteData(sendBuff, SENDFRAMELEN);
cout << sendBuff << endl;
RS485.setupPacketConfig(receiveData, RECFRAMELEN , 0xA5, 0x5A);
if (true != RS485.OpenListenThread())
{
cout << "Open rs485 failed or serial port has already opened!Please check your setting!" << endl;
return -1;
}
while (1)
{
if (RS485.dataReady == TRUE)
{
RS485.dataReady = FALSE;
//AnalysisData(receiveData);
cout << receiveData << endl;
}
}
};
请大神帮忙看下,万分感谢!!