I have a jpeg image which I read into Matlab (into a matrix), then I would like to send each element of the matrix to the arduino on serial port with an USB cable, so I convert each element of the matrix (these are decimal numbers) to 8bit binary data (because later on I will need it binary). I wired together the Serial pins 0-1, because I want to send everything back to the PC from this port.
The communication between the serial ports and the PC is working well, if I write something like '10101010' from Matlab I can read it back, but for some reason if I try to send the image (which is represented by an 4006003 matrix so big data) it is just taking too much time. I set the baud rate in Matlab and on the arduino as well to 115200 for both serial ports. Any ideas to make it work faster?
Matlab code (before I converted everything to 8bit binary):
arduinoCom = serial('COM13', 'BaudRate', 115200);
arduinoCom.Terminator = "";
fopen(arduinoCom)
for i=1:row
for j=1:col
for k=1:dim %image matrix dimensions
s = A{i,j,k};
fwrite(arduinoCom, s);
r = fread(arduinoCom, 8);
r = char(r)' %converting back to binary
B{i,j,k} = r; %matriw for received data
end
end
end
how long is the data transfer taking?
if you are using 115200baud you can transmit approximately 11000 bytes/second
you 4006003 matrix is 540000bytes so would take about 50 seconds to transfer (assuming you don't require delays to process the data)
I don't know how much would it take because I shut down the transfer after a few minutes since it was still busy...so it takes much ore than 50 seconds...
sylarhero:
I don't know how much would it take because I shut down the transfer after a few minutes since it was still busy...so it takes much ore than 50 seconds...
I suspect that the program is crashing leading it to hang - you are probably overwriting memory
what do you do with the data in the arduino? write it to a LCD display?
if you are intending to store it as a 4006003 matrix in RAM I suggest you use a raspberry pi or similar device with suitable memory
I am trying to read it back to the PC from the serial port immediately so I do not want to store it. It is meaningless for now, but I need to make it work like this now.
if you are in a loop reading a byte at a time and transmitting it back to the PC the Arduino should handle it OK
you may have problems with buffering on the PC at 115200baud though
try a slower speed and if it works increase it
are you using a hardware serial port or software serial?
I read data from the PC with Serial.read(), then I send it to Serial1 with Serial1.write() and from there I send it back to the PC with Serial.write().
sylarhero:
After like 5 minutes when I shut it down, until 196003 was built.
196003 is only 34200 bytes - is the data received the same as that transmitted or is it corrupted?
have you hardware or software handshaking enabled?
Robin2 asks how is the serial connected to the PC?
Serial1 is not connected to the PC, but I am reading from Serial. Serial1 is just the 0-1 pins wired together, if something comes from the PC I write it to Serial1 and from there back to Serial.
The reieved data is the same as the transmitted, so its is good. I don't know where can I set up these handshaking methods ?
sylarhero:
Serial1 is not connected to the PC, but I am reading from Serial. Serial1 is just the 0-1 pins wired together, if something comes from the PC I write it to Serial1 and from there back to Serial.
What good is that doing?
Sounds about as useful as tying your left shoelace to your right shoelace - and probably has the same effect.
To test the transfer of data from Simulink to the Arduino, and back, just use Serial. Write what you read.
When you have some idea how long that takes, you can use Serial1 for whatever else it is you need to do. What that is is not at all clear.
r = char(r)' %converting back to binary
That comment is nonsense. The binary data in the memory location designated by the typeless variable r does NOT need converting to binary data. Depending on whether char is signed or unsigned, using char() may actually be causing more harm than good.