代码拉取完成,页面将自动刷新
#include <iostream>
#include "RingBuffer.h"
#include "FileSave.h"
#include "CSerialPort/SerialPort.h"
#include "CSerialPort/SerialPortInfo.h"
#include <string>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#define imsleep(microsecond) Sleep(microsecond) // ms
string portnamebase = "COM";
#else
#include <unistd.h>
string portnamebase = "/dev/ttyS";
#define imsleep(microsecond) usleep(1000 * microsecond) // ms
#endif
#include <vector>
using namespace itas109;
using namespace std;
int countRead = 0;
int flag_stop = 0;
string namebase = "./data_serial/";
#define FRAME_SIZE 16
FileSave FS;
class mySlot : public has_slots<>
{
public:
mySlot(CSerialPort *sp)
{
recLen = -1;
p_sp = sp;
};
bool receive_check(char *_buffer)
{
unsigned char *buffer = (unsigned char *)_buffer;
if (buffer[0] == 170)
{
if (buffer[FRAME_SIZE - 1] == 47)
{
unsigned char checksum = 0;
for (int i = 1; i < 14; i++)
{
checksum += buffer[i];
}
if (checksum == buffer[FRAME_SIZE - 2])
{
return true;
}
else
{
cout << (int)checksum << endl;
cout << (int)buffer[FRAME_SIZE - 2] << endl;
cout << "checksum error" << endl;
return false;
}
}
else
{
cout << "tail error" << endl;
return false;
}
}
else
{
cout << "head error" << endl;
return false;
}
}
void OnSendMessage()
{
recLen = p_sp->readData(str, 16);
if (recLen > 0)
{
countRead++;
str[recLen] = '\0';
if (receive_check(str))
{
FS.save_three_num(*((float *)&str[2]), *((float *)&str[6]), *((float *)&str[10]));
}
if (flag_stop)
{
std::cout << "receive count : " << countRead << std::endl;
p_sp->close();
}
else
{
}
}
};
private:
mySlot(){};
private:
CSerialPort *p_sp;
char str[1024];
int recLen;
};
int read_port_name(void)
{
}
int main(int argc, char **argv)
{
int index = -1;
if (argc == 1)
{
index = 1;
cout << "no input index = 1" << endl;
}
else
{
index = atoi(argv[1]);
}
std::string portName;
vector<SerialPortInfo> m_availablePortsList;
CSerialPort sp;
std::cout << "Version : " << sp.getVersion() << std::endl
<< std::endl;
mySlot receive(&sp);
m_availablePortsList = CSerialPortInfo::availablePortInfos();
std::cout << "availableFriendlyPorts : " << std::endl;
for (int i = 0; i < m_availablePortsList.size(); i++)
{
std::cout << i << " - " << m_availablePortsList[i].portName << " " << m_availablePortsList[i].description << std::endl;
}
if (m_availablePortsList.size() == 0)
{
std::cout << "No valid port" << std::endl;
}
else
{
std::cout << std::endl;
// do
// {
// std::cout << "Please input index of the port(0 - " << (m_availablePortsList.size() - 1) << " ) : " << std::endl;
// std::cin >> index;
// if (index >= 0 && index < m_availablePortsList.size())
// {
// break;
// }
// } while (true);
string portNameFull = portnamebase + to_string(index);
bool flag_exist_port = false;
for (int i = 0; i < m_availablePortsList.size(); i++)
{
// std::cout << i << " - " << m_availablePortsList[i].portName << " " << m_availablePortsList[i].description << std::endl;
if (portNameFull == m_availablePortsList[i].portName)
{
flag_exist_port = true;
break;
}
}
if (!flag_exist_port)
{
cout << portNameFull << " isn't exist" << endl;
return 0;
}
portName = portNameFull;
std::cout << "select port name : " << portName << std::endl;
sp.init(portName, // windows:COM1 Linux:/dev/ttyS0
itas109::BaudRate115200, itas109::ParityNone, itas109::DataBits8, itas109::StopOne);
sp.open();
if (sp.isOpened())
{
std::cout << "open " << portName << " success" << std::endl;
}
else
{
std::cout << "open " << portName << " failed" << std::endl;
}
// connect for read
sp.readReady.connect(&receive, &mySlot::OnSendMessage);
// write
sp.writeData("itas109", 7);
for (;;)
{
char c;
cin >> c;
cout << "int put" << endl;
if (c == 'c')
{
flag_stop = 1;
sp.readReady.disconnect(&receive);
sp.close();
std::cout << "receive count : " << countRead << std::endl;
break;
}
imsleep(1);
}
}
string cmd = ".\\test.bat";
cmd += " " + to_string(FS.get_this_num());
WinExec(cmd.c_str(), SW_SHOW);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。