2 Star 14 Fork 3

createskyblue/Serial-Screen 单片机串口投屏工具 上位机程序

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
start.cpp 11.59 KB
一键复制 编辑 原始数据 按行查看 历史
createskyblue 提交于 2021-08-17 17:19 . 放大画面
/*
* @Descripttion: 单片机串口投屏 主程序
* @version: 1.0.0
* @Author: Createskyblue
* @Date: 2021-05-06 08:03:27
* @LastEditors: Createskyblue
* @LastEditTime: 2021-05-07 13:06:16
*/
#define DATA_LENGTH 255
#include <stdio.h>
#include <stdint.h>
#include <windows.h>
#include<math.h>
#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
//uint8_t image[1024] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFE, 0xFF, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x07, 0x07, 0x07, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0x07, 0x07, 0x07, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0xFF, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0x80, 0x80, 0x80, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x80, 0x80, 0x80, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x7F, 0x81, 0x81, 0x81, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x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 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x3F, 0x00, 0x20, 0x22, 0x22, 0x3F, 0x22, 0x22, 0x20, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x04, 0x02, 0x1F, 0x20, 0x20, 0x20, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
uint8_t image[1024];
#define setbit(x,y) x|=(1<<y)
#define clrbit(x,y) x&=~(1<<y)
#define reversebit(x,y) x^=(1<<y)
#define getbit(x,y) ((x) >> (y)&1)
void print_error(const char* context)
{
DWORD error_code = GetLastError();
char buffer[256];
DWORD size = FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
buffer, sizeof(buffer), NULL);
if (size == 0) { buffer[0] = 0; }
fprintf(stderr, "%s: %s\n", context, buffer);
}
// Opens the specified serial port, configures its timeouts, and sets its
// baud rate. Returns a handle on success, or INVALID_HANDLE_VALUE on failure.
HANDLE open_serial_port(const char* device, uint32_t baud_rate)
{
HANDLE port = CreateFileA(device, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (port == INVALID_HANDLE_VALUE)
{
print_error(device);
return INVALID_HANDLE_VALUE;
}
// Flush away any bytes previously read or written.
BOOL success = FlushFileBuffers(port);
if (!success)
{
print_error("Failed to flush serial port");
Sleep(1000);
CloseHandle(port);
return INVALID_HANDLE_VALUE;
}
// Configure read and write operations to time out after 100 ms.
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 0;
success = SetCommTimeouts(port, &timeouts);
if (!success)
{
print_error("Failed to set serial timeouts");
Sleep(1000);
CloseHandle(port);
return INVALID_HANDLE_VALUE;
}
// Set the baud rate and other options.
DCB state = { 0 };
state.DCBlength = sizeof(DCB);
state.BaudRate = baud_rate;
state.ByteSize = 8;
state.Parity = NOPARITY;
state.StopBits = ONESTOPBIT;
success = SetCommState(port, &state);
if (!success)
{
print_error("Failed to set serial settings");
Sleep(1000);
CloseHandle(port);
return INVALID_HANDLE_VALUE;
}
return port;
}
// Writes bytes to the serial port, returning 0 on success and -1 on failure.
int write_port(HANDLE port, uint8_t* buffer, size_t size)
{
DWORD written;
BOOL success = WriteFile(port, buffer, size, &written, NULL);
if (!success)
{
print_error("Failed to write to port");
Sleep(1000);
return -1;
}
if (written != size)
{
print_error("Failed to write all bytes to port");
Sleep(1000);
return -1;
}
return 0;
}
// Reads bytes from the serial port.
// Returns after all the desired bytes have been read, or if there is a
// timeout or other error.
// Returns the number of bytes successfully read into the buffer, or -1 if
// there was an error reading.
SSIZE_T read_port(HANDLE port, uint8_t* buffer, size_t size)
{
DWORD received;
BOOL success = ReadFile(port, buffer, size, &received, NULL);
if (!success)
{
print_error("Failed to read from port");
Sleep(1000);
return -1;
}
return received;
}
int main()
{
Mat img(64, 128, CV_8UC3);
Mat ResizeImg = Mat::zeros(640, 1280, CV_8UC3);
img = CV_RGB(0, 0, 0);
char device[50] = { '\\','\\', '.','\\', };
printf("输入要打开的串口 如:\"COM3\"\n");
scanf("%s", device + 4);
// Choose the baud rate (bits per second). This does not matter if you are
// connecting to the Jrk over USB. If you are connecting via the TX and RX
// lines, this should match the baud rate in the Jrk's serial settings.
uint32_t baud_rate = 115200;
printf("波特率 如:\"115200\"\n");
scanf("%ld", &baud_rate);
HANDLE port = open_serial_port(device, baud_rate);
if (port == INVALID_HANDLE_VALUE) { return 1; }
char buffer[2048];
while (1) {
uint8_t CMD[] = "OLED_ScreenshotInit\r\n";
write_port(port, CMD, strlen((char*)CMD));
read_port(port, (uint8_t*)buffer, strlen((char*)CMD)); //提取usmart-shell返回值
if (memcmp(buffer, CMD, 19)) { //判断usmart-shell执行返回值
read_port(port, (uint8_t*)buffer, 2048); //清除缓存区
read_port(port, (uint8_t*)buffer, 2048); //清除缓存区
continue;
}
read_port(port, image, 1024); //正文
img = CV_RGB(0, 0, 0);
//计算位图字节宽度
int bytew1 = (128 + 7) / 8;
uint8_t* p = image;
for (int yi = 0; yi < 8; yi++) {
for (int x = 0; x < 128; x++) {
for (int y = 0; y < 8; y++) {
//绘制图形
if (getbit(*p, y)) circle(img, Point(x, yi * 8 + y), 0, CV_RGB(255, 255, 255), -1);
else circle(img, Point(x, yi * 8 + y), 0, CV_RGB(0, 0, 0), -1);
}
p++;
}
}
resize(img, ResizeImg, ResizeImg.size(),0,0,0);
imshow("Stm32F401CCU6", ResizeImg);
//imshow("Stm32F401CCU6", ResizeImg);
waitKey(15);
}
CloseHandle(port);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/createskyblue/Serial-Screen.git
git@gitee.com:createskyblue/Serial-Screen.git
createskyblue
Serial-Screen
Serial-Screen 单片机串口投屏工具 上位机程序
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385