Sending hex from matlab to longer pump device

조회 수: 6 (최근 30일)
Mathieu
Mathieu 2023년 3월 3일
댓글: Mathieu 2023년 7월 19일
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
Walter Roberson
Walter Roberson 2023년 3월 7일
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
Mathieu
Mathieu 2023년 3월 21일
편집: Mathieu 2023년 3월 21일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Daniele Sportillo
Daniele Sportillo 2023년 3월 21일
이동: Walter Roberson 2023년 3월 30일
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");
  댓글 수: 1
Mathieu
Mathieu 2023년 3월 21일
이동: Walter Roberson 2023년 3월 30일
It works, thank you very much !

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

JOUDI ARMOUCH
JOUDI ARMOUCH 2023년 5월 19일
If its possible, can I know which software you used in picture 1?
  댓글 수: 5
JOUDI ARMOUCH
JOUDI ARMOUCH 2023년 6월 15일
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
Mathieu 2023년 6월 15일
Hello
I've added you on Linked In, we can chat there if you don't mind.

댓글을 달려면 로그인하십시오.


Florian Mueller
Florian Mueller 2023년 7월 11일
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
Florian Mueller
Florian Mueller 2023년 7월 18일
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
Mathieu 2023년 7월 19일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 COM Component Integration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by