0
我試圖測試matlab和arduino之間的通信以及一些奇怪的事情。 我嘗試數字1到10來接收他們。它適用於2,3,4 .... 10但是我得到:Matlab和Arduino
警告:不成功的讀取:在超時期限內沒有返回指定的數據量。
如果我再次運行它,我會得到相同的結果。爲1我得到錯誤,我得到2到10.我不重置運行之間的Arduino。那是奇怪的部分。
我arudino代碼:
uint32_t next_number;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
next_number = 0;
}
void loop() {
if (Serial.available() >= sizeof(next_number)) {
uint8_t data_read = Serial.readBytes((uint8_t*)&next_number, sizeof(next_number));
Serial.write((uint8_t*)&next_number, 4);
digitalWrite(13, HIGH);
}
}
我Matlab代碼:
function send_song_to_arduino()
delete(instrfind);
s = serial('COM3');
disp(['s input buffer size ', num2str(s.InputBufferSize)]);
set(s,'Terminator','LF'); % define the terminator for println
set(s,'Databits',8);
set(s,'Stopbits',1);
set(s,'Parity','none');
set(s, 'BaudRate', 9600);
fopen(s);
disp(['available at start ', num2str(s.bytesavailable)]);
disp(['left to send at start ', num2str(s.BytesToOutput)]);
if (s.bytesavailable > 0)
fread(s, s.bytesavailable, 'uint8')
end
disp('starting');
for i=1:10
fwrite(s, i, 'uint32');
pause(1);
disp(['available ', num2str(s.bytesavailable)]);
received = fread(s, 1, 'uint32');
disp(['recv ', mat2str(received)]);
end
disp(['done available ', num2str(s.bytesavailable)]);
disp(['total sent ', num2str(s.ValuesSent)]);
disp(['left to send at end ', num2str(s.BytesToOutput)]);
fclose(s);
delete(s);
end
結果:
>> send_song_to_arduino
available at start 0
left to send at start 0
starting
available 0
Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period.
recv zeros(1,0)
available 4
recv 2
available 4
recv 3
available 4
recv 4
available 4
recv 5
available 4
recv 6
available 4
recv 7
available 4
recv 8
available 4
recv 9
available 4
recv 10
done available 0
total sent 10
left to send at end 0