Serial communication with hex code

6 ビュー (過去 30 日間)
Deo
Deo 2017 年 5 月 17 日
回答済み: 상휘 김 2022 年 1 月 7 日
I have one device using an asynchronous HDLC framing. And my matlab versions are 2016a and 2015b.
This device is connected to my computer via USB, but this is recognized as COM port device due to USART driver.
Anyway, I need to send hex code "0x7E, 0x23, 0x02, 0x00, 0x7D, 0x31, 0x24, 0x01, 0x0C, 0x25, 0x01, 0x0E, 0x26, 0x01, 0x03, 0x3A, 0x7E" to this device and receive response. (Terminator is 0x7E which means "~" or "126").
Can you help to make this serial communication code?
For serial setthing, I set the code like below, but I am asking this question because my code with fopen/query or fwrite and fread did not work properly.
=======================================
clear;
instrreset();
s = serial ('COM3','baudrate',115200);
set(s,'parity','none');
set(s,'databit',8);
set(s,'stopbit',1);
set(s,'timeout',1);
===============================================

採用された回答

Jan
Jan 2017 年 5 月 17 日
編集済み: Jan 2017 年 5 月 17 日
Perhaps:
Str = '7E2302007D3124010C25010E2601033A7E'
D = sscanf(Str, '%2x');
fwrite(s, D, 'uint8')
This is a bold guess only. I'm still confused when somebody wants to send hex codes. Hex codes are actually a string, a vector of type char. I assume that the bytes should be sent, which are represented by these hex codes. Or do you really want to send the characters '7', 'E', ... ?
Note: I cannot test this code. Please try if it must be '%.2X' instead of '%2X'.
  2 件のコメント
Deo
Deo 2017 年 5 月 17 日
Jan, I solved the problem. Your code was correct, and I had problem in inverted checksum, such as "3A" at last 3rd and 4th digits of Str. Also, I added fread function like "data=fread(s);" Thanks for your help.
Jose Ignacio Mora Cordero
Jose Ignacio Mora Cordero 2021 年 5 月 24 日
Hi Deo, did you work with Xbee´s ?
Can you receive data and extract de data from de API frame?

サインインしてコメントする。

その他の回答 (1 件)

상휘 김
상휘 김 2022 年 1 月 7 日
@Jan, Thank you!

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by