加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CSerialPortDemoNoGui.cpp 5.34 KB
一键复制 编辑 原始数据 按行查看 历史
louis_sun 提交于 2021-12-11 14:38 . serial_and_plot
#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;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化