You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
sending and receiving IMAGE using serial port(USB to serial)
21 views (last 30 days)
Show older comments
Hi friends,
Actually I have an image which need to be transferred via serial port. As my laptop dosent contain serial port i am using USB to serial convertor.I am sending image from one computer other using two USB to serials and null modem. I am doing this for testing purpose and later null modem will be replaced by 8051 controller. I wrote a code for transferring but i am unable to receive data proper control. Please check the below code.
For Transfering
if true
SendData=serial('COM1','BaudRate',256000);
fopen(SendData);
[rows,columns]=size(Encryptedimage);
for k=1:3
for i=1:rows
for j=1:columns
data=num2str(EncryptedImage(i,j,k));
fprintf(SendData, '%c' , '*');
fprintf(SendData, '%s',data);
end
end
end
fclose(SendData);
delete(SendData);
clear SendData;
end
For receiving
if true
recData=serial('COM2','BaudRate',256000);
fopen(recData);
while(1)
Input1=fread(receiveTemp)
if(isempty(Input1))
continue;
else
disp('Value received')
for k=1:3
for i=1:rows
for j=1:columns
Input1=char(Input1);
Input1=sscanf(Input1, '%d');
EncryptedImage1(i,j,k)=Input1;
Input1=fread(receiveTemp);
end
end
end
end
if(i==rows & j==columns & k==3)
break;
end
end
end
Now problem is while receiving Input1 is getting around 120 values at a time. please help me in rectifying this issue.
34 Comments
gullu
on 17 Apr 2014
Hey I am using combination of led and photo diode for transmitting an image through one computer and receive it using another.I tried the code as above using MATLAB as interface.I too am getting the error of dimension mismatch.Did you get where the problem lies?...?
gullu
on 19 Apr 2014
yes I tried as per the changes suggested by you.Still the input is receiving lot of pixel values at a time.I want it to receive one value at a time so that the image is recovered at the receiving end.
Walter Roberson
on 19 Apr 2014
As I indicated earlier, fread() will by default keep reading until the buffer is full. You can tell it otherwise by passing a parameter which is the number of elements to read. For example,
fread(fid, 1, 'uint8=>char')
However, if your protocol is '*' followed by a decimal number, then you should be using fscanf()
fscanf(fid, '*%d', 1)
hina khan
on 16 May 2014
hello, i am doing the same as gullu and want to transmit an image from the led transmitter and receive it on the photodiode receiver circuit from serial port, i some what changed this code but i am unable to receive any thing. i also want to know what is this "receivetemp" and what is the purpose of giving an if condition at the end? thanks in advance
dinh nguyen thanh
on 15 Apr 2016
can you me funtion EncryptedImage(i,j,k) and EncryptedImage1(i,j,k) thank you
dinh nguyen thanh
on 27 Apr 2016
Edited: Walter Roberson
on 27 Apr 2016
I want to transmit an image from the led transmitter and receive it on the photodiode receiver circuit from serial port but I have problem with Unexpected Error: The number of bytes written OutputBufferSize (must be less than or equal to OutputBufferSize-BytesToOutput.).
I set OutputBufferSize =1000000, but only send small Image. I want to send and receive one value at a time.
My code :
send:
s=serial('com2');
s.OutputBufferSize=10000000;
s.Timeout=500;
s.BaudRate=115200;
fopen(s);
I = imread('girl.jpg');
b = dec2bin(I);
du = bin2dec(b);
du=(du)';
du=num2str(du);
fprintf(s,du);
receive:
data1 = fscanf(s)
data1=str2num(data1);
data1=(data1)';
I = imread('5.jpg');
data1=reshape(data1,size(I));
imwrite(uint8(data1),'dinh.jpg','jpg');
please help me
Walter Roberson
on 27 Apr 2016
Sending one value at a time:
for K = 1 : numel(I)
fprintf(s, '%d\n', I(K));
end
this will be slow.
Priya Dharsini
on 28 Jun 2016
Will the above code work to recover the image at the reciever? Even am working with image transmission through LED.
Walter Roberson
on 29 Mar 2017
Shiv:
At that point in the code you should read an image that is the exact same size as the original image, but is not the original image.
The imread of 5.jpg there is used only to establish the size of the image. It would be better if the size were just transmitted first, and then the imread() at the destination would not be needed.
Walter Roberson
on 25 Apr 2017
size_bytes = [uint8(ndims(TheImage)), typecast( uint16(size(TheImage)), 'uint8')];
Now transmit the bytes in size_bytes.
Note: if any dimension of the image can be more than 65535 then you need to use uint32 instead of uint16
To reconstruct: read the first byte. That tells you how many dimensions follow. Read that many uint16 . That will give you the size information.
However, looking back I see that you are transmitting in character form, not as binary, so
size_bytes = [ndims(TheImage), size(TheImage)];
and transmit those such as with
fprintf(s, '%d\n', size_bytes)
Shiv
on 26 Apr 2017
Edited: Walter Roberson
on 26 Apr 2017
Sir i have used the following code for receiving image from serial port but im not able to reconstruct
receiver code:
s=serial('com1');
s.InputBufferSize=10000000;
s.Timeout=1500;
s.BaudRate=19200;
fopen(s);
data1 = fscanf(s)
data1=str2num(data1);
data1=(data1)';
data1=reshape(data1,[255,255,3]);
imwrite(uint8(data1),'shiv.jpg','jpg');
can you please help me with the code for receiving i need to submit my working project on next week.
Thank You...
Walter Roberson
on 26 Apr 2017
Edited: Walter Roberson
on 27 Apr 2017
In the transmission, before sending the content, use
img_size = size(TheImage);
fprintf(s, '%d ', img_size);
fprintf(s, '\n');
and then the code to transmit the content of the image:
fprintf(s, '%d ', TheImage);
fprintf(s, '\n');
Then in the receiver, after the fopen(s):
img_size = fscanf(s, '%d') .';
data1 = uint8( fscanf(s, '%d') );
data1 = reshape( data1, img_size );
imwrite( data1, 'shiv.jpg', 'jpg');
Shiv
on 27 Apr 2017
Edited: Shiv
on 2 May 2017
sir i have tried as you said but while transmitting i got error at
fprintf(s, '\n', img_size(end));
as: Matching failure in format.
so i removed img_size(end) and transmitted
and at receiver side at
data1 = reshape(data1, img_size );
getting error as: To RESHAPE the number of elements must not change.
so what can i do now.?
Walter Roberson
on 27 Apr 2017
Sorry, the img_size(end) was a typo, and you were right to remove it.
On the transmitting side, please show
img_size
and also please show how you send your image.
On the receiving end, please show
img_size
size(data1)
Shiv
on 28 Apr 2017
Edited: Walter Roberson
on 28 Apr 2017
Sir this is the code im using for transmission
s=serial('com4');
s.OutputBufferSize=10000000;
s.Timeout=600;
s.BaudRate=19200;
I = imread('images.jpg');
fopen(s);
img_size = size(I);
fprintf(s, '%d ', img_size);
fprintf(s, '\n');
b = dec2bin(I);
du = bin2dec(b);
du=(du)';
fprintf(s,'%d ', du);
fprintf(s, '\n');
fclose(s);
and the receiver code:
s=serial('com3');
s.InputBufferSize=10000000;
s.Timeout=600;
s.BaudRate=19200;
fopen(s);
img_size = fscanf(s, '%d') .';
data1 = uint8( fscanf(s, '%d') )
data1=(data1)';
data1 = reshape( data1, img_size );
imwrite(data1, 'shiv.jpg', 'jpg');
fclose(s);
Walter Roberson
on 28 Apr 2017
On the transmitting side, the lines
b = dec2bin(I);
du = bin2dec(b);
du=(du)';
fprintf(s,'%d ', du);
can be replaced by
fprintf(s, '%d ', I);
On the receiving side, you can leave out
data1=(data1)';
Both of these sets of changes are just for cleaner more efficient code, and do not affect what would be sent or received.
There is no obvious reason why the sent image size would not equal the received image size.
In the sending side, disp(img_size) and the same in the receiver, and tell us what shows up.
Shiv
on 30 Apr 2017
Edited: Walter Roberson
on 30 Apr 2017
Sir for disp(img_size) i got [115 115 3] in both transmission and receiver side. But im not getting any data at receiver i.e, data1 = [] there is no data in data1. I think the error is due to new line at
fprintf(s, '%d ', img_size);
fprintf(s, '\n');
the data is not reading by receiver after the new line.
and is there any other way to make serial data into matrix instead of using Reshape..?
Walter Roberson
on 30 Apr 2017
"is there any other way to make serial data into matrix instead of using Reshape..?"
Yes. Replace
data1 = uint8( fscanf(s, '%d') );
with
data1 = uint8( fscanf(s, '%d', img_size) )
Are you getting a message about time-outs?
You would not be able to successfully read the img_size as [115 115 3] if the newline was not there and functioning properly; we can now be sure that part is working.
The sending side should be
s = serial('com4');
s.OutputBufferSize = 10000000;
s.Timeout = 600;
s.BaudRate = 19200;
fopen(s);
I = imread('images.jpg');
img_size = size(I);
fprintf(s, '%d ', img_size);
fprintf(s, '\n');
fprintf(s, '%d ', I);
fprintf(s, '\n');
fclose(s);
Shiv
on 2 May 2017
There is no time-out error. And still im not able able to read the data after new line.
fprintf(s, '%d ', img_size); fprintf(s, '\n');
after execution the data1 = [] not getting any data.
and for fprintf(s, '%d ', I); it gives error as 'The third input argument must be a string.' so i have used I = dec2bin(I); I = bin2dec(I); before fprintf(s, '%d ', I)
Walter Roberson
on 2 May 2017
Change
fprintf(s, '%d ', I);
to
fprintf(s, '%d ', double(I(:)));
See https://www.mathworks.com/matlabcentral/answers/214786-serial-fprintf-of-non-string for more on that.
On the receiving side, try
while true
data1 = uint8( fscanf(s, '%d') );
if ~isempty(data1); break; end
end
data1 = reshape(dat1, img_size);
Though, if I were programming the code, I would not be trying to fprintf() everything at one time: I would presume that trying to do so would overflow the output buffer. I would transmit at most one column at a time.
Shiv
on 3 May 2017
Thank You sir finally i am able to reconstruct the image. But now the only problem is that after transmitting the image i need to wait till time-out to occur to get the output, so how can i get output before time-out that is immediately after transmission.
Walter Roberson
on 3 May 2017
If you are having to wait for timeout then the \n of the sending system is not being recognized. Did you accidentally leave out the fprintfs(, '\n') after the fprintf(s, '%d ', double(I(:))); ?
Shiv
on 8 May 2017
Im sending fprintf(s, '\n') after fprintf(s, '%d ', double(I(:))); but im losing few data while receiving, so what can i do to compensate the lost data?
Walter Roberson
on 8 May 2017
Do not send as much data at one time before the \n .
Make sure that both devices have hardware flow control turned on.
Priya Dharsini
on 16 Feb 2018
AM planning to send a biomedical image through LED.SO what image processing to be done for a biomedical image?
Walter Roberson
on 18 Feb 2018
Use several steps:
- read the biomedical image in, which is a step that is independent of any image processing you plan to do
- Do whatever image processing is appropriate for your biomedical image for whatever purpose you need, which is a step that is independent of reading the image and is independent of any plans to transmit the image
- if the processed image is not already uint8, use typecast() to make a byte stream out of it. This is a step that is independent of reading the image and is independent of whatever image processing you did.
- reshape your now uint8 image to a vector
- transmit the uint8 vector by LED technology -- which is a step that is independent of everything having to do with calculating which uint8 are going to be transmitted
Answers (1)
Walter Roberson
on 26 Apr 2013
Replace
[rows,columns]=size(Encryptedimage);
with
[rows,columns,bands]=size(Encryptedimage);
In the form of size() that you were using, "columns" would be set to the real columns times the number of bands.
2 Comments
srikanth
on 26 Apr 2013
Thanks for correcting me. But still this didnt rectified my problem of serial communication. When i am transferring values to hyper terminal i can see values continuously. But when using Matlab at reception side 'Input1' variable is showing lots of values at a time and matlab is giving error dimension mismatch. Maybe i feel there is some synchronization problem between transition and reception.How to rectify this issue?
Walter Roberson
on 26 Apr 2013
You know that fread() without a size parameter will try to read until the buffer is full ? http://www.mathworks.com/help/matlab/ref/serial.fread.html
You should also be considering using the 'precision' parameter of fread().
You might want to re-think why it is that you are using the formatted-data function fprintf() to send the data, but are using the binary-data function fread() to read it.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)