上传文件至 /
This commit is contained in:
		
							parent
							
								
									b74b26e74f
								
							
						
					
					
						commit
						5142c4ad6d
					
				
							
								
								
									
										78
									
								
								GetFrame 2.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								GetFrame 2.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,78 @@ | |||||||
|  | #include <iostream> | ||||||
|  | #include <string> | ||||||
|  | #include <iomanip> | ||||||
|  | #include <sstream> | ||||||
|  | 
 | ||||||
|  | //校验函数
 | ||||||
|  | unsigned int sumHexNumbersSkippingFirstTwo(const std::string& hexString) { | ||||||
|  |     std::istringstream iss(hexString); | ||||||
|  |     std::string hexNum; | ||||||
|  |     unsigned int sum = 0; | ||||||
|  | 
 | ||||||
|  |     // 跳过前两个十六进制数(包括它们之间的空格,尽管在这个例子中可能不需要)
 | ||||||
|  |     iss >> std::hex >> hexNum; // 读取第一个FF
 | ||||||
|  |     iss >> hexNum; // 读取第二个FF(或空格后的下一个十六进制数)
 | ||||||
|  | 
 | ||||||
|  |     // 现在开始累加剩下的十六进制数
 | ||||||
|  |     while (iss >> std::hex >> hexNum) { | ||||||
|  |         sum += std::stoul(hexNum, nullptr, 16); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return -1-sum; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // 函数:在16进制字符串中每两位插入分隔符
 | ||||||
|  | std::string insertSeparators(const std::string& src, char separator) { | ||||||
|  |     std::string result; | ||||||
|  |     for (size_t i = 0; i < src.length(); i += 2) { | ||||||
|  |         if (i > 0) result += separator; // 在非第一个字符前插入分隔符
 | ||||||
|  |         result += src.substr(i, 2); // 添加两位16进制数
 | ||||||
|  |     } | ||||||
|  |     return result; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | std::string GetFrame() { | ||||||
|  |     int time, angle; | ||||||
|  |     std::string hexTime, hexAngle, finalString, hexCheck, hexCheck2; | ||||||
|  |     const std::string prefix = "FF FF 01 07 03 2A "; | ||||||
|  |     const std::string suffix = " 00"; | ||||||
|  | 
 | ||||||
|  |     // 获取用户输入
 | ||||||
|  |     std::cout << "请输入时间值: "; | ||||||
|  |     std::cin >> time; | ||||||
|  |     std::cout << "请输入角度值: "; | ||||||
|  |     std::cin >> angle; | ||||||
|  | 
 | ||||||
|  |     // 计算角度的转换公式
 | ||||||
|  |     int convertedAngle = 4096 * (angle + 180) / 360;  | ||||||
|  |     std::cout<<convertedAngle<<std::endl; | ||||||
|  |     // 将转换后的角度值和时间值转换为16进制字符串
 | ||||||
|  |     std::stringstream ss; | ||||||
|  |     ss << std::hex << std::uppercase;  | ||||||
|  |     ss << std::setw(4) << std::setfill('0') << convertedAngle; | ||||||
|  |     std::cout<< "角度值转换:" <<ss.str()<<std::endl; | ||||||
|  |     hexAngle = ss.str(); | ||||||
|  |     ss.str(""); // 清空stringstream以重用
 | ||||||
|  |     ss << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << time; // 时间值始终为两位
 | ||||||
|  |     hexTime = ss.str(); | ||||||
|  |     std::cout<< "时间值转换:" <<hexTime<<std::endl; | ||||||
|  |     // 在hexAngle中每两位插入空格(如果需要)
 | ||||||
|  |     hexAngle = insertSeparators(hexAngle, ' '); | ||||||
|  | 
 | ||||||
|  |     // 拼接字符串
 | ||||||
|  |     finalString = prefix + hexAngle + " " + hexTime + suffix; | ||||||
|  |     | ||||||
|  |     //校验和
 | ||||||
|  |     unsigned int sumResult = sumHexNumbersSkippingFirstTwo(finalString); | ||||||
|  |     std::cout << "校验和: " << sumResult << std::endl; | ||||||
|  |     ss.str(""); // 清空stringstream以重用
 | ||||||
|  |     ss << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << sumResult;//校验和始终为两位
 | ||||||
|  |     hexCheck = ss.str(); | ||||||
|  |     hexCheck2 = hexCheck.substr(hexCheck.length() - 2, 2); | ||||||
|  |     finalString = finalString + " " + hexCheck2; | ||||||
|  | 
 | ||||||
|  |     // 输出结果
 | ||||||
|  |     std::cout << "发送的数据帧为: " << finalString << std::endl; | ||||||
|  | 
 | ||||||
|  |     return finalString; | ||||||
|  | } | ||||||
							
								
								
									
										85
									
								
								main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								main.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,85 @@ | |||||||
|  | #include <iostream> | ||||||
|  | #include <fcntl.h> // 包含open函数
 | ||||||
|  | #include <unistd.h> // 包含close函数
 | ||||||
|  | #include <termios.h> // 包含termios结构体
 | ||||||
|  | #include <cstring> // 包含memset函数
 | ||||||
|  | #include <string> // 包含std::string
 | ||||||
|  | #include "GetFrame.h" | ||||||
|  | 
 | ||||||
|  | // 串口配置参数
 | ||||||
|  | const char* SERVO_PORT_NAME = "/dev/ttyS3"; // 串口设备文件,根据实际情况修改
 | ||||||
|  | const speed_t BAUDRATE = B115200; // 波特率
 | ||||||
|  | 
 | ||||||
|  | bool setupSerialPort(int& fd, const char* portName, speed_t baudRate); | ||||||
|  | bool sendSerialData(int fd, const std::string& data); | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  |     int uart_fd; // 串口文件描述符
 | ||||||
|  | 
 | ||||||
|  |     // 开启并配置串口
 | ||||||
|  |     if (!setupSerialPort(uart_fd, SERVO_PORT_NAME, BAUDRATE)) { | ||||||
|  |         std::cerr << "Failed to setup serial port" << std::endl; | ||||||
|  |         return 1; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // 要发送的数据帧
 | ||||||
|  |     //std::string data_frame = "Hello, servo!"; // 字符串数据
 | ||||||
|  |     std::string data_frame = GetFrame(); | ||||||
|  |      | ||||||
|  |     // 通过串口发送数据
 | ||||||
|  |     if (!sendSerialData(uart_fd, data_frame)) { | ||||||
|  |         std::cerr << "Failed to send data" << std::endl; | ||||||
|  |         close(uart_fd); // 关闭串口
 | ||||||
|  |         return 1; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     std::cout << "Data frame sent successfully" << std::endl; | ||||||
|  | 
 | ||||||
|  |     close(uart_fd); // 关闭串口
 | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool setupSerialPort(int& fd, const char* portName, speed_t baudRate) { | ||||||
|  |     fd = open(portName, O_RDWR | O_NOCTTY | O_NDELAY); | ||||||
|  |     if (fd == -1) { | ||||||
|  |         std::cerr << "Error opening serial port: " << portName << std::endl; | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     struct termios options; | ||||||
|  |     tcgetattr(fd, &options); // 获取当前串口配置
 | ||||||
|  | 
 | ||||||
|  |     // 设置输入输出波特率
 | ||||||
|  |     cfsetispeed(&options, baudRate); | ||||||
|  |     cfsetospeed(&options, baudRate); | ||||||
|  | 
 | ||||||
|  |     // 本地模式 | 接收使能
 | ||||||
|  |     options.c_cflag |= (CLOCAL | CREAD); | ||||||
|  |     // 设置数据位数
 | ||||||
|  |     options.c_cflag &= ~CSIZE; // 清空数据位设置
 | ||||||
|  |     options.c_cflag |= CS8; // 8位数据位
 | ||||||
|  |     // 设置无奇偶校验
 | ||||||
|  |     options.c_cflag &= ~PARENB; | ||||||
|  |     // 设置停止位
 | ||||||
|  |     options.c_cflag &= ~CSTOPB; | ||||||
|  |     // 设置原始输入输出模式
 | ||||||
|  |     options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); | ||||||
|  |     options.c_iflag &= ~(IXON | IXOFF | IXANY); | ||||||
|  |     options.c_oflag &= ~OPOST; | ||||||
|  | 
 | ||||||
|  |     // 应用配置
 | ||||||
|  |     if (tcsetattr(fd, TCSANOW, &options) != 0) { | ||||||
|  |         std::cerr << "Error applying serial port settings" << std::endl; | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return true; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool sendSerialData(int fd, const std::string& data) { | ||||||
|  |     if (write(fd, data.c_str(), data.size()) < 0) { | ||||||
|  |         std::cerr << "Error writing to serial port" << std::endl; | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  |     return true; | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user