Sending hex from matlab to longer pump device
Show older comments
Hello everybody
First of all, I am a beginner in communication protocols
I recently started to try to control LongerPump BT1002J with matlab. I'm able to control it with a special software (pictures 1), everything works

I would like to use Matlab to be able to send the same thing to my device, but I think the format of the pin in not correct and the pump seems to not understand what I am sending to it.
I used the folowing code:
clear all
instrreset
s = serialport('COM6',1200);
fopen (s)
Str = 'E9 01 06 57 4A 00 0A 01 01 10';
D = sscanf(Str, '%2x');
fwrite(s, D, 'uint8')
I tried to remove blanks between E9 01 etc, but it is not effective. The line is sent ( I can see it with my RS485 converter, the led lights up.
Can anyone could correct the mistake in this code please?
Thank you for your help
10 Comments
Walter Roberson
on 3 Mar 2023
Str = 'E9 01 06 57 4A 00 0A 01 01 10';
That does not match the image in the second and the last places.
Mathieu
on 3 Mar 2023
Jan
on 3 Mar 2023
You did not mention, why you assume that there is a mistake and what "does not work" exactly means. Please share this important information.
You see, that in the grey box "Write" the ASCII values of the char vector are sent as UINT8, not the decimal values of the conversion from hex.
Str = 'E9 01 06 57 4A 00 0A 01 01 10';
D = sscanf(Str, '%2x').'
These are the bytes, which should be sent. Can you set the "Data format" to hex in the Serial Explorer app?
In you code you set the speed to 1200, in the SerialExplorer to 9600. Does this matter?
Walter Roberson
on 3 Mar 2023
The image you show first records the conversation as
E9 1F 06 57 4A 00 0A 01 01 0E
You are sending
E9 01 06 57 4A 00 0A 01 01 10
^^ ^^
Well, let us do a test:
Str = 'E9 1F 06 57 4A 00 0A 01 01 0E';
D = sscanf(Str, '%2x').'
filename = tempname + ".txt";
%write process you are using now
fid = fopen(filename, 'w');
fwrite(fid, D, 'int8');
fclose(fid);
fid = fopen(filename, 'r');
stored = fread(fid, [1 inf], 'uint8');
fclose(fid);
disp(stored)
%write process with greater certainty
D2 = typecast(uint8(D), 'int8');
fid = fopen(filename, 'w');
fwrite(fid, D2, 'int8');
fclose(fid);
fid = fopen(filename, 'r');
stored = fread(fid, [1 inf], 'uint8');
fclose(fid);
disp(stored)
This suggests that your use of write() with 'int8' is equivalent to first doing int8(D) and writing the result -- which is not going to work because some of your values are greater than 127. The type specification you use in write() or fwrite() does not do a typecast() of the data, it does a cast() of the data
Accepted Answer
More Answers (2)
JOUDI ARMOUCH
on 19 May 2023
1 vote
If its possible, can I know which software you used in picture 1?
5 Comments
Mathieu
on 19 May 2023
JOUDI ARMOUCH
on 24 May 2023
Thanks for your answer, I am just wondering when you were able to control the pump did you use Serial Explorer app from matlab or just run the code normally? also, what type of cables or adatpers did you use to connect PC and the pump?
Mathieu
on 24 May 2023
Edited: Walter Roberson
on 24 May 2023
JOUDI ARMOUCH
on 15 Jun 2023
Thank you for your answer and time, really appreciated..
I have a project related to this type of longerPump, if you do not mind can I have a way to communicate with you and ask some questions.
Mathieu
on 15 Jun 2023
Florian Mueller
on 11 Jul 2023
0 votes
Dear Mathieu,
I also try to use a Longer pump ... and failed.
Would you mind sharing what software you used (and maybe you have a better user manual than I found).
Thanks a lot already!
5 Comments
Florian Mueller
on 11 Jul 2023
THANK YOU SO MUCH! Will give this a try tomorrow!
Mathieu
on 11 Jul 2023
Florian Mueller
on 18 Jul 2023
Mathieu, THANK YOU.
Thanks to this thread and all the replies, I finally managed to get this to work ... Longer pumps are nice and cheap, but the documentation is not very clear.
Mathieu
on 19 Jul 2023
Categories
Find more on MATLAB Support for MinGW-w64 C/C++ Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

