0

I set a serial port in matlab to receive an image via UART. After receiving the image, I notice that the image sometimes shifts and sometimes not shift. As shown in figure, left part is the correct receiving result and right part shows the shift of the image.

The following is my code.Is there any suggestion to improve my figure?

clear all
close all
s = serial('COM3','BaudRate',115200, 'Parity','none','DataBits',8,'StopBits',1);
s.InputBufferSize=200000;
s.timeout=20;
fopen(s);
flushinput(s);
D=fread(s,[200,200]);
E=fread(s,[200,200]);
F=fread(s,[200,200]);
image_RGB(:,:,1)=D;
image_RGB(:,:,2)=E;
image_RGB(:,:,3)=F;
imwrite(image_RGB/256,'myimage.jpg','jpg');
imshow(image_RGB/256);
fclose(s);
fclose all;
Morten Zilmer
  • 15,586
  • 3
  • 30
  • 49
Mr.Z
  • 1
  • can you increase `InputBufferSize` to `400000`? – Yvon Jun 09 '16 at 16:38
  • Thx Yvon, the total bit of the image are 960000 bits and I have tried many inputbuffersizes such as 960000 or 400000 etc but it does not work. Due to my observation, I guess either the inputbuffer or the image merge process play the key. – Mr.Z Jun 10 '16 at 01:03
  • it might be interesting if you use some other tools to capture the serial data stream. i think something is wrong with the transfer, not the matlab stuff. because as you said it's not always shifted, and from the picture i see a yellow band, it's like the transmitter side is giving wrong signal. – Yvon Jun 10 '16 at 04:32
  • Thx Yvon, as you said, I had considered the idea. The transmitter is an FPGA board. In order to keep the transmission data from FPGA memory to matlab correct, I initialize the memory data as same as the image. But it also does not work. I am confused why the result is sometimes wrong but sometimes correct..... – Mr.Z Jun 10 '16 at 06:38
  • so maybe you can extend the question to fpga + matlab? – Yvon Jun 10 '16 at 10:36
  • umm.... if the behavior of FPGA is wrong, it is impossible to get the correct data. Thus I focus on the matlab code – Mr.Z Jun 10 '16 at 15:29
  • but it's possible the tx buffer pointer is shifted, right? – Yvon Jun 10 '16 at 18:20
  • maybe you can verify the fpga sequence by sending a known bit stream, like `ABCDEFG` in a sequence? If matlab can receive in the right order - always - then the problem is `imshow` – Yvon Jun 10 '16 at 18:21
  • I do not noticed whether there is the tx buffer setting in FPGA. Maybe that is the reason why the image sometimes failed. I will take the advice into consideration. thx Yvon !! – Mr.Z Jun 11 '16 at 05:53

0 Answers0