11 changed files with 491 additions and 2 deletions
-
1.gitignore
-
3.vscode/settings.json
-
47CMakeLists.txt
-
16README.md
-
20demo/dc_capture/README.md
-
43demo/uart/test_uart232_1.cpp
-
44demo/uart/test_uart232_2.cpp
-
44demo/uart/test_uart485.cpp
-
44demo/uart/test_usbuart.cpp
-
169demo/uart/uart.cpp
-
62demo/uart/uart.hpp
@ -0,0 +1,20 @@ |
|||
|
|||
``` |
|||
## 显示可供使用的声卡 |
|||
arecord -l |
|||
**** List of CAPTURE Hardware Devices **** |
|||
XDG_RUNTIME_DIR (/run/user/1000) is not owned by us (uid 0), but by uid 1000! (This could e.g. happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.) |
|||
card 0: rockchipes8388 [rockchip-es8388], device 0: dailink-multicodecs ES8323 HiFi-0 [dailink-multicodecs ES8323 HiFi-0] |
|||
Subdevices: 1/1 |
|||
Subdevice #0: subdevice #0 |
|||
card 1: rockchiphdmiin [rockchip,hdmiin], device 0: fddf8000.i2s-dummy_codec hdmiin-dc-0 [fddf8000.i2s-dummy_codec hdmiin-dc-0] |
|||
Subdevices: 1/1 |
|||
Subdevice #0: subdevice #0 |
|||
|
|||
DC对应的声卡是 card 0: rockchipes8388 [rockchip-es8388] |
|||
|
|||
|
|||
## 录音指令 |
|||
arecord -Dhw:0,0 -d 10 -f S24_LE -r 48000 -c 2 -t wav test.wav |
|||
|
|||
``` |
@ -0,0 +1,43 @@ |
|||
|
|||
#include <string.h>
|
|||
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <thread>
|
|||
#include <vector>
|
|||
|
|||
#include "uart.hpp"
|
|||
using namespace std; |
|||
using namespace iflytop; |
|||
|
|||
#define UART_NAME "/dev/ttyS0"
|
|||
|
|||
int main(int argc, char const* argv[]) { |
|||
Uart uart; |
|||
uart.open(UART_NAME, "115200"); |
|||
|
|||
thread th([&]() { |
|||
//
|
|||
while (true) { |
|||
char buf[1024]; |
|||
memset(buf, 0, 1024); |
|||
int size = uart.receive(buf, 1024, 10); |
|||
if (size > 0) { |
|||
cout << "receive:" << buf << endl; |
|||
} |
|||
} |
|||
}); |
|||
|
|||
while (true) { |
|||
printf("tx hello\n"); |
|||
uart.send("hello", 5); |
|||
sleep(1); |
|||
} |
|||
} |
@ -0,0 +1,44 @@ |
|||
|
|||
#include <string.h>
|
|||
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <thread>
|
|||
#include <vector>
|
|||
|
|||
#include "uart.hpp"
|
|||
using namespace std; |
|||
using namespace iflytop; |
|||
|
|||
#define UART_NAME "/dev/ttyS5"
|
|||
|
|||
int main(int argc, char const* argv[]) { |
|||
Uart uart; |
|||
uart.open(UART_NAME, "115200"); |
|||
|
|||
thread th([&]() { |
|||
//
|
|||
|
|||
while (true) { |
|||
char buf[1024]; |
|||
memset(buf, 0, 1024); |
|||
int size = uart.receive(buf, 1024, 10); |
|||
if (size > 0) { |
|||
cout << "receive:" << buf << endl; |
|||
} |
|||
} |
|||
}); |
|||
|
|||
while (true) { |
|||
printf("tx hello\n"); |
|||
uart.send("hello", 5); |
|||
sleep(1); |
|||
} |
|||
} |
@ -0,0 +1,44 @@ |
|||
|
|||
#include <string.h>
|
|||
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <thread>
|
|||
#include <vector>
|
|||
|
|||
#include "uart.hpp"
|
|||
using namespace std; |
|||
using namespace iflytop; |
|||
|
|||
#define UART_NAME "/dev/ttyS1"
|
|||
|
|||
int main(int argc, char const* argv[]) { |
|||
Uart uart; |
|||
uart.open(UART_NAME, "115200"); |
|||
|
|||
thread th([&]() { |
|||
//
|
|||
while (true) { |
|||
char buf[1024]; |
|||
memset(buf, 0, 1024); |
|||
int size = uart.receive(buf, 1024, 10); |
|||
if (size > 0) { |
|||
cout << "receive:" << buf << endl; |
|||
} |
|||
} |
|||
}); |
|||
|
|||
while (true) { |
|||
const char* txstr = "message from 485\n"; |
|||
printf("tx message\n"); |
|||
uart.send((char*)txstr, strlen(txstr)); |
|||
sleep(1); |
|||
} |
|||
} |
@ -0,0 +1,44 @@ |
|||
|
|||
#include <string.h>
|
|||
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <thread>
|
|||
#include <vector>
|
|||
|
|||
#include "uart.hpp"
|
|||
using namespace std; |
|||
using namespace iflytop; |
|||
|
|||
#define UART_NAME "/dev/ttyUSB0"
|
|||
|
|||
int main(int argc, char const* argv[]) { |
|||
Uart uart; |
|||
uart.open(UART_NAME, "115200"); |
|||
|
|||
thread th([&]() { |
|||
//
|
|||
|
|||
while (true) { |
|||
char buf[1024]; |
|||
memset(buf, 0, 1024); |
|||
int size = uart.receive(buf, 1024, 10); |
|||
if (size > 0) { |
|||
cout << "receive:" << buf << endl; |
|||
} |
|||
} |
|||
}); |
|||
|
|||
while (true) { |
|||
printf("tx hello\n"); |
|||
uart.send("hello", 5); |
|||
sleep(1); |
|||
} |
|||
} |
@ -0,0 +1,169 @@ |
|||
|
|||
/*
|
|||
* uart.c |
|||
* |
|||
* Created on: Aug 5, 2019 |
|||
* Author: Cristian Fatu |
|||
* Implements basic UART functionality, over Uart Lite linux driver, using termios. |
|||
* After booting linux, a device like "/dev/ttyUL1" must be present. |
|||
* These functions work in both canonic and not canonic modes. |
|||
* In the canonic communication mode, the received chars can be retrieved by read only after \n is detected. |
|||
* In the non canonic communication mode, the received chars can be retrieved by read as they are received. |
|||
*/ |
|||
#include "uart.hpp"
|
|||
|
|||
#include <fcntl.h>
|
|||
#include <stdio.h>
|
|||
#include <stdlib.h>
|
|||
#include <string.h>
|
|||
#include <termios.h>
|
|||
|
|||
#include <chrono>
|
|||
#include <fstream>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
|
|||
using namespace iflytop; |
|||
using namespace chrono; |
|||
|
|||
static int64_t getnowms() { return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); } |
|||
|
|||
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}, |
|||
}; |
|||
|
|||
Uart::Uart() {} |
|||
Uart::~Uart() {} |
|||
|
|||
int Uart::open(string path, string ratestr) { |
|||
int rc; |
|||
|
|||
m_name = path; |
|||
uint32_t rate = 0; |
|||
|
|||
m_fd = ::open(path.c_str(), O_RDWR | O_NOCTTY); |
|||
if (m_fd < 0) { |
|||
// m_error = fmt::format("open {} failed,{}", path, strerror(errno));
|
|||
return -1; |
|||
} |
|||
|
|||
if (s_baundmap.find(ratestr) == s_baundmap.end()) { |
|||
// m_error = fmt::format("baund {} not support", ratestr);
|
|||
return -1; |
|||
} |
|||
rate = s_baundmap[ratestr]; |
|||
m_rate = ratestr; |
|||
memset(&m_tty, 0, sizeof(m_tty)); |
|||
// memset(tty, 0, sizeof(struct termios));
|
|||
/*
|
|||
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. |
|||
CRTSCTS : output hardware flow control (only used if the cable has |
|||
all necessary lines. See sect. 7 of Serial-HOWTO) |
|||
CS8 : 8n1 (8bit,no parity,1 stopbit) |
|||
CLOCAL : local connection, no modem contol |
|||
CREAD : enable receiving characters |
|||
*/ |
|||
// tty->c_cflag = dev->rate | CRTSCTS | CS8 | CLOCAL | CREAD;
|
|||
m_tty.c_cflag = rate | CS8 | CLOCAL | CREAD; |
|||
|
|||
// not canonic
|
|||
/*
|
|||
IGNPAR : ignore bytes with parity errorsc_cc[VTIME] |
|||
*/ |
|||
m_tty.c_iflag = IGNPAR; |
|||
/* set input mode (non-canonical, no echo,...) */ |
|||
m_tty.c_lflag = 0; |
|||
/* Do not wait for data */ |
|||
m_tty.c_cc[VTIME] = 10; /* inter-character timer unused */ |
|||
m_tty.c_cc[VMIN] = 0; /* blocking read until 5 chars received */ |
|||
|
|||
/*
|
|||
Raw output. |
|||
*/ |
|||
m_tty.c_oflag = 0; |
|||
|
|||
/* Flush port */ |
|||
tcflush(m_fd, TCIFLUSH); |
|||
|
|||
/* Apply attributes */ |
|||
rc = tcsetattr(m_fd, TCSANOW, &m_tty); |
|||
if (rc) { |
|||
// m_error = fmt::format("tcsetattr {} failed,{}", path, strerror(errno));
|
|||
return -1; |
|||
} |
|||
return 0; |
|||
} |
|||
int Uart::send(char *data, int size) { |
|||
// if (logger->level() <= level::debug) {
|
|||
// logger->debug("{} send: {}", m_name, StringUtils().bytesToString((const uint8_t *)data, size));
|
|||
// }
|
|||
int sent = 0; |
|||
sent = write(m_fd, data, size); |
|||
if (sent < 0) { |
|||
// m_error = fmt::format("write {} failed,{}", m_name, strerror(errno));
|
|||
} |
|||
return sent; |
|||
} |
|||
int Uart::receive(char *data, int size_max) { |
|||
int received = 0; |
|||
received = read(m_fd, data, size_max); |
|||
if (received < 0) { |
|||
// m_error = fmt::format("read {} failed,{}", m_name, strerror(errno));
|
|||
} |
|||
// if (logger->level() <= level::debug) {
|
|||
// logger->debug("{} receive: {}", m_name, StringUtils().bytesToString((const uint8_t *)data, received));
|
|||
// }
|
|||
return received; |
|||
} |
|||
int Uart::receive(char *data, int size, int overtimems) { |
|||
if (m_fd < 0) return -1; |
|||
|
|||
int64_t now = getnowms(); |
|||
int64_t end = now + overtimems; |
|||
int rc = 0; |
|||
int total = 0; |
|||
while (getnowms() < end) { |
|||
rc = receive(data + total, size - total); |
|||
if (rc > 0) { |
|||
total += rc; |
|||
if (total >= size) break; |
|||
} |
|||
usleep(333); |
|||
} |
|||
return total; |
|||
} |
|||
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; |
|||
} |
|||
bool Uart::set_rx_overtime(int n100ms) { |
|||
if (m_fd < 0) return false; |
|||
m_tty.c_cc[VTIME] = n100ms; |
|||
int rc = tcsetattr(this->m_fd, TCSANOW, &m_tty); |
|||
return rc == 0; |
|||
} |
@ -0,0 +1,62 @@ |
|||
#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; |
|||
// ref:https://www.cnblogs.com/electron/p/3451114.html
|
|||
// ref:https://baike.baidu.com/item/tcflush/6869357?fr=aladdin
|
|||
class Uart { |
|||
private: |
|||
/* data */ |
|||
int m_fd = 0; |
|||
string m_name; |
|||
string m_rate; |
|||
struct termios m_tty; |
|||
string m_error; |
|||
|
|||
public: |
|||
Uart(); |
|||
~Uart(); |
|||
|
|||
/**
|
|||
* @brief 打开串口 |
|||
* |
|||
* @param path |
|||
* @param rate |
|||
* "0" "50" "75" "110" |
|||
* "134" "150" "200" "300" |
|||
* "600" "1200" "1800" "2400" |
|||
* "4800" "9600" "19200" "38400" |
|||
* "57600" "115200" "230400" "460800" |
|||
* "500000" "576000" "921600" "1000000" |
|||
* "1152000" "1500000" "2000000" "2500000" |
|||
* "3000000" "3500000" "4000000" |
|||
* |
|||
* @return int |
|||
*/ |
|||
int open(string path, string rate); |
|||
int send(char *data, int size); |
|||
int receive(char *data, int size_max); |
|||
int receive(char *data, int size, int overtimems); |
|||
int close(); |
|||
|
|||
string get_error() { return m_error; } |
|||
|
|||
bool flush_rx(); |
|||
bool flush_tx(); |
|||
bool set_rx_overtime(int n100ms); |
|||
}; |
|||
|
|||
} // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue