我目前在阅读从Arduino到MatLab的串行发送的32位整数时遇到问题。
根据the documentation对于序列fscanf,假设我正确解释了格式 A = fscanf(obj,' format',size)读取size指定的值的数量。 所以,我有
flushinput(handles.s);
disp('Buffer was flushed');
fprintf(handles.s,'%s','M0');
disp('M0 was sent');
for j = 1:6
while (handles.s.BytesAvailable == 0)
end
disp('pausing');
pause(0.1);
disp('stopped pausing');
CurrentInput = fscanf(handles.s,'%c', 1 );
if CurrentInput == ' '
disp('pausing');
pause(0.1);
disp('stopped pausing');
databit = fscanf(handles.s,'%i',5);% reads four bytes and stores it as an integer
disp(databit);
disp(handles.s.BytesAvailable);
else
disp('Error communicating with arduino');
end
handles.coefficients(j) = databit; % stores the calibration bytes inside the handles object
guidata(hObject,handles);
disp(handles.coefficients(j));
end
end
哪个输出以下
Buffer was flushed
M0 was sent
pausing
Buffer was flushed
M0 was sent
pausing
stopped pausing
pausing
stopped pausing
45583
30
45583
pausing
stopped pausing
pausing
stopped pausing
39633
24
39633
pausing
stopped pausing
pausing
stopped pausing
28291
18
28291
pausing
stopped pausing
pausing
stopped pausing
26021
12
26021
pausing
stopped pausing
pausing
stopped pausing
32164
6
32164
pausing
stopped pausing
pausing
stopped pausing
28343
0
28343
我想要它做什么,但我不明白为什么。
现在,我最初使用
进行测试databit = fscanf(handles.s,'%i',4);% reads four bytes and stores it as an integer
因为我认为它会读取4个字节(32位)并将其转换为long int。但很快就意识到这个功能有自己的意志,正在读数字或其他东西......
然后如何从输入缓冲区读取指定数量的字节(4)并将它们转换为long int(32位)?
为了您的兴趣,这里是相关的arduino代码的snipet。基本上,我们通过I2C从2个传感器的寄存器中读取系数。然后将字节转换为通过串行发送到matlab的长整数。
//*********************************************************************************************************
// SERIAL
// Function to send an integer to MATLAB after converting it to a string and concatenating it with a character
// Parameters:
// >> data_label_character: A character defining the data that is to be sent after ()
// >> data_integer: Integer holding the data to be sent to MATLAB
// Return: None
////////Notes:
// This function is mainly used to send the capacitance, pressure, and temperature data to MATLAB
//*********************************************************************************************************
void send_MATLAB_integer_data(char data_label_character, uint32_t data_integer){
Serial.print(data_label_character); // Send the defining label to MATLAB
Serial.print(data_integer); // Follow the label by the corresponding data
}
//*********************************************************************************************************
// SERIAL
// Function to transmit the data to MATLAB while conducting a delay for a certain amount of time
// Parameters:
// >> total_wait_period: The period of the delay to be conducted
// >> maximum_data_conversion_wait: The maximum interval of time between two
// data points (a property of the devices on the bus and their ADCs)
// Return: None
////////Notes:
// The conversion mode for the AD7162 assumed in this function is the Single Conversion mode
// The general assumption in this function is that the conversion time chosen for the AD7152 is
// longer (60ms) than the conversion time of the temperature (10ms) and pressure (10ms) from the MS5803
// It is advised that the maximum_data_conversion_wait to be the actual conversion time plus several milliseconds for safety
//*********************************************************************************************************
void transmit_data_and_wait(unsigned long total_wait_period, unsigned long maximum_data_conversion_wait, int AD_channel_number, int OSR_temp, int OSR_press){
unsigned long start_time; // The time at which the wait and transmit algorithm is to start
// Keep looping until reaching the point in time where if you do the next conversion
// you will exceed the specified wait time
start_time = millis(); // Store the start time
while(total_wait_period - (millis() - start_time) > maximum_data_conversion_wait){
AD_request_capacitance(AD_channel_number); // Function to request data from the AD7152
send_MATLAB_integer_data('P', MS_read_pressure(OSR_press)); // Send MATLAB the pressure value you read
send_MATLAB_integer_data('T', MS_read_temperature(OSR_temp)); // Function to read the temperature from the AD7152
send_MATLAB_integer_data('C', AD_read_requested_capacitance(AD_channel_number)); // Send temperature data here
}
// If broke out of the loop before total_wait_period is complete, loop until it is complete
while(total_wait_period - (millis() - start_time) > 0){}
}