6 changed files with 245 additions and 152 deletions
-
57.vscode/settings.json
-
81c/main.c
-
BINcpp/a.out
-
116cpp/main.cpp
-
103cpp/uart.cpp
-
40cpp/uart.hpp
@ -0,0 +1,103 @@ |
|||
#include "uart.hpp"
|
|||
|
|||
#include <fcntl.h>
|
|||
#include <stdio.h>
|
|||
#include <stdlib.h>
|
|||
#include <string.h>
|
|||
|
|||
#include <iostream>
|
|||
|
|||
using namespace iflytop; |
|||
using namespace std; |
|||
|
|||
Uart::Uart() {} |
|||
Uart::~Uart() {} |
|||
|
|||
static map<string, uint32_t> s_baundmap = { |
|||
{"0", 0000000}, {"50", 0000001}, {"75", 0000002}, {"110", 0000003}, //
|
|||
{"134", 0000004}, {"150", 0000005}, {"200", 0000006}, {"300", 0000007}, //
|
|||
{"600", 0000010}, {"1200", 0000011}, {"1800", 0000012}, {"2400", 0000013}, //
|
|||
{"4800", 0000014}, {"9600", 0000015}, {"19200", 0000016}, {"38400", 0000017}, //
|
|||
{"57600", 0010001}, {"115200", 0010002}, {"230400", 0010003}, {"460800", 0010004}, //
|
|||
{"500000", 0010005}, {"576000", 0010006}, {"921600", 0010007}, {"1000000", 0010010}, //
|
|||
{"1152000", 0010011}, {"1500000", 0010012}, {"2000000", 0010013}, {"2500000", 0010014}, //
|
|||
{"3000000", 0010015}, {"3500000", 0010016}, {"4000000", 0010017}, |
|||
}; |
|||
|
|||
int Uart::open(string path) { |
|||
int rc; |
|||
|
|||
m_name = path; |
|||
|
|||
m_fd = ::open(path.c_str(), O_RDWR | O_NOCTTY); |
|||
if (m_fd < 0) { |
|||
cout << "open " << path << " failed" << endl; |
|||
return -1; |
|||
} |
|||
|
|||
memset(&m_tty, 0, sizeof(m_tty)); |
|||
|
|||
m_tty.c_cflag |= CLOCAL | CREAD; // 激活本地连接与接受使能
|
|||
|
|||
m_tty.c_cflag &= ~CSIZE; // 失能数据位屏蔽
|
|||
m_tty.c_cflag |= CS8; // 8位数据位
|
|||
|
|||
m_tty.c_cflag &= ~CSTOPB; // 1位停止位
|
|||
|
|||
m_tty.c_cflag &= ~PARENB; // 无校验位
|
|||
|
|||
m_tty.c_cc[VTIME] = 0; |
|||
m_tty.c_cc[VMIN] = 0; |
|||
|
|||
cfsetispeed(&m_tty, B115200); // 设置输入波特率
|
|||
cfsetospeed(&m_tty, B115200); // 设置输出波特率
|
|||
|
|||
tcflush(m_fd, TCIFLUSH); // 刷清未处理的输入和/或输出
|
|||
|
|||
/* Apply attributes */ |
|||
rc = tcsetattr(m_fd, TCSANOW, &m_tty); |
|||
if (rc) { |
|||
cout << "tcsetattr failed" << endl; |
|||
return -3; |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
int Uart::send(char *data, int size) { |
|||
int sent = 0; |
|||
sent = write(m_fd, data, size); |
|||
if (sent > 0) { |
|||
printf("send success, data is %s\r\n", data); |
|||
} else { |
|||
printf("send error!\r\n"); |
|||
} |
|||
return sent; |
|||
} |
|||
|
|||
int Uart::receive(char *data, int size_max) { |
|||
int received = 0; |
|||
received = read(m_fd, data, size_max); |
|||
if (received > 0) { |
|||
printf("recv success,recv size is %d,data is %s\r\n", received, data); |
|||
Uart::send(data, received); |
|||
} |
|||
return received; |
|||
} |
|||
|
|||
int Uart::close() { |
|||
::close(m_fd); |
|||
m_fd = -1; |
|||
return 0; |
|||
} |
|||
|
|||
bool Uart::flush_rx() { |
|||
if (m_fd < 0) return false; |
|||
int rc = tcflush(m_fd, TCIFLUSH); |
|||
return rc == 0; |
|||
} |
|||
|
|||
bool Uart::flush_tx() { |
|||
if (m_fd < 0) return false; |
|||
int rc = tcflush(m_fd, TCOFLUSH); |
|||
return rc == 0; |
|||
} |
@ -0,0 +1,40 @@ |
|||
#pragma once
|
|||
#include <termios.h>
|
|||
#include <unistd.h>
|
|||
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
class Uart { |
|||
private: |
|||
int m_fd = 0; |
|||
string m_name; |
|||
int m_rate = B115200; |
|||
struct termios m_tty; |
|||
|
|||
public: |
|||
Uart(); |
|||
~Uart(); |
|||
|
|||
int open(string path); |
|||
int send(char *data, int size); |
|||
int receive(char *data, int size_max); |
|||
int close(); |
|||
|
|||
bool flush_rx(); |
|||
bool flush_tx(); |
|||
|
|||
int get_m_fd(void) { return m_fd; }; |
|||
}; |
|||
|
|||
} // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue