什么是c++中串口数据读取的回调函数?下面是Matlab中的一个例子
what is Callback function for serial port data reading in c++? Here is an example in Matlab
有c++的例子或参考吗?我们将如何在c++中基于什么目的进行串口编程?这是一个Matlab代码示例。如果我们将Matlab代码转换成c++,在c++编程中我们应该考虑什么?
function DUT_callback(obj, event, DUT_port)
persistent stored_data;
if isempty(stored_data)
stored_data = [];
end
if ~strcmp(DUT_port.status,'open')
return;
end
if ~DUT_port.BytesAvailable
return;
end
try
new_data = fread(DUT_port,DUT_port.BytesAvailable);
catch exception
fprintf('ERROR: Failed to read from DUT port. Shutting down.n');
cleanup();
return;
end
data_array = [stored_data;new_data];
packet.NewPacket = 1;
while packet.NewPacket == 1
[packet,data_array] = parse_serial_data(data_array);
% Report bad checksum if appropriate
if packet.BadChecksum
end
% If packet was received, do stuff with it
if packet.NewPacket == 1
% HANDLE RAW GYRO DATA PACKET
if packet.Address == 86
% Extract the gyro data
gyro_x = typecast( flipud(uint8(packet.data(1:2))), 'int16' );
gyro_y = typecast( flipud(uint8(packet.data(3:4))), 'int16' );
gyro_z = typecast( flipud(uint8(packet.data(5:6))), 'int16' );
got_gyro_data = 1;
% fprintf('%dt%dt%dn',gyro_x,gyro_y,gyro_z);
end
% HANDLE TEMPERATURE DATA PACKET
if packet.Address == 118
temperature = typecast( flipud(uint8(packet.data(1:4))), 'single' );
got_temperature_data = 1;
% fprintf('%3.2fn',temperature);
end
% If we received gyro and temperature data, log it to
% the workspace if logging is enabled.
if got_temperature_data && got_gyro_data
got_temperature_data = 0;
got_gyro_data = 0;
if GDATA.logging_data == 1 && GDATA.selected_DUT > 0
if GDATA.DUT_samples_collected < GDATA.MAX_SAMPLES
GDATA.DUT_samples_collected = GDATA.DUT_samples_collected + 1;
end end end end
这取决于你要在哪个操作系统上运行你的程序。
微软在串行接口上有很好的文档。
http://msdn.microsoft.com/en-us/library/ff802693.aspxLinux或其他你会更感兴趣的操作系统:
http://www.gnu.org/software/libc/manual/html_mono/libc.html Low_002dLevel-Terminal-Interface
您可以在两组文档中找到示例。
相关文章:
- 显示 matlab 程序的输出,调用另一个 C++ 程序
- Matlab 如何知道有一个 .mex64 文件并避免无穷大"compiling"循环
- 在 matlab 中编写一个非常基本的 mex 函数
- 使用MatLab dll时出现C++错误:找到一个或多个多重定义的符号
- 在c++中的一个函数中返回几个数组和变量(如MATLAB)
- 如何将实部和虚部放入一个变量中,就像在matlab中一样
- 在我的c++代码之间向matlab传递一个字符串变量
- 使用mex将一个大矩阵从Matlab传递到C:Matlab崩溃
- 带有外部C++函数的Matlab:coder.ceval将一个结构传递给一个函数
- 如何在 Matlab 中分析一个 MEX 函数
- 将一个C++ DLL 加载到 matlab 中,该 matlab 调用另一个 DLL 中的函数
- 使用MATLAB mex将矢量分配给另一个矢量时出现分割错误
- 在Matlab中编写一个固定长度的字节数组到二进制文件
- 实现A(:,k)=b;一个c++矩阵库中的类似matlab的语法
- Matlab 计算与'C/C++'计算..哪一个是有效的?
- 从matlab代码创建一个静态库
- OpenCV C++ 如何编写一个与 Matlab 的 bwareaopen 函数相同的函数?
- 一个等效于MATLAB的c++函数示例
- 防止一个MEX文件在MATLAB中崩溃
- 如果瓶颈是一个大的矩阵乘法,我可以使用mex文件加速我的MATLAB代码吗?