Sending hex from matlab to longer pump device

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

Str = 'E9 01 06 57 4A 00 0A 01 01 10';
That does not match the image in the second and the last places.
Hello, thank you for your help
Yes my bad sorry! but even if I write
Str = 'E9 1F 06 57 4A 00 0A 01 01 0E';
or Str = 'E91F06574A000A01010E';
It does not work :/
You did not mention, why you assume that there is a mistake and what "does not work" exactly means. Please share this important information.
Mathieu
Mathieu on 3 Mar 2023
Edited: Mathieu on 3 Mar 2023
Yes sorry for the lack of information,
Matlab does not give me any error message, the pin ( I call pin Str = E9 1F 06 57 4A 00 0A 01 01 0E) is sent since I can see the led on my USB-RS485 converter lights up in the same way that when I sent the pin with the software.
It makes me think that the format of the pin is not understood by the pump.
I tried with the Serial Explorer app from matlab, I've seen that
Both Str = 'E9 1F 06 57 4A 00 0A 01 01 0E' or Str = 'E91F06574A000A01010E' give the same results, i.e the pin is sent but the pump is not activated
I notice that several parameters were available at the right and at the top of the windows, but I'm not able to choose the right parameters
Thank you again for your help
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').'
D = 1×10
233 1 6 87 74 0 10 1 1 16
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?
The only two available format in Serial explorer app are Binary and ASCII-Terminated string (see images below)
I have tried every "Data Type" proposed in the list, and when I swith from Binary to ASCII-Terminated, only one "Data Type" is proposed. In all cases, none of them work. The byte is sent but the pump remains inert
Yes the baudrate was initialized as 9600 by default setting, I did not notice it. Thank you
When I implement the following code in Matlab
Str = 'E9 01 06 57 4A 00 0A 01 01 10';
D = sscanf(Str, '%2x').'
then to send it to the pump, I have to use the fwrite function
fwrite(s, D, 'uint8')
Are you saying that uint8 is not the good format ? By which one should I change it?
Edit: I assumed that the object I had to send was 'E9 01 06 57 4A 00 0A 01 01 10' because it is displayed in TXD windows (in the first image). Maybe it is not a good assumption?
Please find in attachment the communication protocole of my device. I would be very grateful if someone could enlighten me
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
^^ ^^
Hello Sir,
Thank you very much for your answer.
I was not clear in my previous answer, I have also tried the following code:
clear all
instrreset
s = serialport('COM6',1200);
fopen (s)
Str = 'E9 1F 06 57 4A 00 0A 01 01 0E';
D = sscanf(Str, '%2x').'
fwrite(s, D, 'int8')
However, pump remains inert. Do you know if in this type of device, there is an additional conversion step of the sent object? Maybe the object "E9 1F 06 57 4A 00 0A 01 01 0E" needs to be changed into something else?
Well, let us do a test:
Str = 'E9 1F 06 57 4A 00 0A 01 01 0E';
D = sscanf(Str, '%2x').'
D = 1×10
233 31 6 87 74 0 10 1 1 14
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)
127 31 6 87 74 0 10 1 1 14
%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)
233 31 6 87 74 0 10 1 1 14
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
Hello Sir, first of all thank you very much for your answer and sorry for the slowness of mine
If I understand what you said, I need to use typecast() function in order to convert my str command ('E9 1F 06 57 4A 00 0A 01 01 0E') to HEX format because fwrite does not work as I thought. Then, I should send the converted command through my serial port?
EDIT:
I tried this one:
%% configuration and opening of the serial port
s = serialport('COM8',1200,'Parity','Even','DataBits',8,'StopBits',1); %open the serial port
fopen(s);
%% conversion step of the command format
Str = 'E9 1F 06 57 4A 00 0A 01 01 0E'; %command to be sent
D = sscanf(Str, '%2x').';
filename = tempname + ".txt";
fid = fopen(filename, 'w');
fwrite(fid, D, 'int8');
fclose(fid);
fid = fopen(filename, 'r');
stored = fread(fid, [1 inf], 'uint8');
fclose(fid);
disp(stored)
%% Here, the converted string command is sent to the seriaporl
fwrite(s, stored, 'int8')
display("the command has been sent")
clear all
But it does not work, because you said that fwrite was not compatible.. What should I replace the fwrite function with? I don't see how to use typecast function.

Sign in to comment.

 Accepted Answer

Hi,
this should work
s = serialport('COM8',1200,'Parity','Even','DataBits',8,'StopBits',1); %open the serial port
Str = 'E9 1F 06 57 4A 00 0A 01 01 0E';
D = sscanf(Str, '%2x').';
write(s,D,"uint8");

More Answers (2)

If its possible, can I know which software you used in picture 1?

5 Comments

Hello,
The software shown in picture 1 was sent to me by Longer Company, I don't know if it is available online.
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?
Hello,
I currently use the program described below. I just run the code normally, but Serial Explorer works too. Don't forget to implement the right parameters ('COM8',1200,'Parity','Even','DataBits',8,'StopBits',1)
For the connection between PC and pump, I use:
  • a converter connected to the computer : Waveshare Industrial USB to RS485 Converter with Original FT232RL 300-921600bps Baudrate
  • simple electrical cables
  • a DB15 to rs485 converter connected to the pump (in the document attached, the 2nd one from the right "RS485 05.49.31.E"), it was sold with the pump
I hope it will help you
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.
Hello
I've added you on Linked In, we can chat there if you don't mind.

Sign in to comment.

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

Mathieu
Mathieu on 11 Jul 2023
Edited: Mathieu on 11 Jul 2023
Hello Florian,
here is a wetransfer link to download the software:
I use it to generate the appropriate pin (for example: 'E9 1F 06 57 4A 00 B3 01 00 B6') to send to the pump. Then I copy and paste this pin into my matlab code described above. Make sure you select the correct COM port (ctrl+X -> device manager, ports (COM and LPT)).
If you have any questions, please don't hesitate to contact me.
THANK YOU SO MUCH! Will give this a try tomorrow!
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.
Great news! Yes, I agree, the documentation isn't clear, but I have to admit that the person I contacted at longer pump company was very helpful. Anyway, congratulations!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!