源代碼如下:
#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;
}
}
};
請大神幫忙看下,萬分感謝!!
uj5u.com熱心網友回復:
為啥不先打開接收執行緒,你發完了再打開,萬一來不及呢。uj5u.com熱心網友回復:
執行緒在接收資料前已經打開了啊uj5u.com熱心網友回復:
不要沉啊,急求幫助啊uj5u.com熱心網友回復:
plc是啥。。。uj5u.com熱心網友回復:
PLC 不用 MODBUS 協議?自由協議?uj5u.com熱心網友回復:
用串口精力之類的串口工具 先手動發送 看看是否正常?轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/131309.html
標籤:基礎類
上一篇:2008資料庫備份權限怎么開
