How to generate a signal of fixed length of 6 bytes. The massage are transfered in 15ms cycle. Baud rate 38400, data bits 8, parity none, stop bits 1, CRC 16.
Example signal : AA 20 AA 02 IC IE.
Serial Data Format

 Réponse acceptée

Benjamin Thompson
Benjamin Thompson le 9 Mar 2022

0 votes

See if one of the examples on the File Exchange will work for you:
Then if you run into problems ask a specific question and post your work.

8 commentaires

Ibnul Feraji Alam
Ibnul Feraji Alam le 10 Mar 2022
I want to send and receive data from a serial device which is not listed on the file exchange. Like Matlab computer as Master and the drive as slave. I used serialport(), serialportobj() to send the data using write() command. But the data doesn't send as it should be. Example: write(serialportObj,101010101010101010101010101010101010101010101010,"uint32");
data1 = read(serialportObj,6,"uint8")
Like the signal I was trying to send is (AA AA AA AA AA AA) which I have coonverted to binary for write() command. But the signal I am sending is not the correct one.
The correct signal shuold be like:
The transmitted data is encapsulated into a data frame.
The data bytes are convert into ASCII format in order to ensure they lie outside the range of control characters. This method offers the bonus of an additional plausibility check to pick up transmission errors not detected by calculating the checksum (e.g. 2 bits falsified).
Example signal : AA 20 AA 02 IC IE
Benjamin Thompson
Benjamin Thompson le 10 Mar 2022
Modifié(e) : Benjamin Thompson le 10 Mar 2022
If your serial device does not present itself as a COM port in Windows, then you need to identify the device by manufacturer and part number. Otherwise we do not know what it is. Do you have some C or C++ code that you have written yourself or that you receive with the hardware in a developer kit? Usually the approach to use this in Simulink is to wrap the code in an S-Function.
Ibnul Feraji Alam
Ibnul Feraji Alam le 11 Mar 2022
Yes the device present itself as a COM port. I can connect with the device using serialport(), serialportObj() and also in simulink.
Example:
s = serialport("COM4",38400,"DataBits",8, "Parity", "none", "StopBits", 1, "FlowControl", "none", "ByteOrder", "big-endian", "Timeout", 0.005)
write(s,101010101010101010101010101010101010101010101010,"uint64");
data1 = read(s,6,"uint8")
if I want to send a signal (like AAAAAAAAAAAA) I convert it into binary (like 101010101010101010101010101010101010101010101010). I can send the signal with write(). But the problem is the signal should be sent like
The transmitted data is encapsulated into a data frame.
Also I can connect the device with simulink using instrument control toolbox. But I don't know how to send the signal (like AA 00 20 IC ID IE) with simulink.
Thank you
Benjamin Thompson
Benjamin Thompson le 11 Mar 2022
If your device needs software flow control, the Instrument Control Toolbox functions do not support that. But if STX and ETX can just be at the beginning and end of your byte string then just send 8 bytes every time instead of 6. You can combine bytes together into a vector using the mux or vector concatenate blocks per my sample diagram. Use the serial configuration block to set up your baud rate and other parameters. You want your model, or at least the serial send block, to have a step size of 0.015 seconds, and run the model in Real Time mode (use the Simulation Pacing option under the Run button in Simulink). Your CRC algorithm could probably be coded up in a MATLAB Function Block for Simulink, or maybe try the "General CRC Calculator" block in the instrument control toolbox library.
Remember that Windows is only a soft real time OS at best, so for timing you are probably only going to be in the +/- 10-15 ms range on the timing accuracy.
I am not sure what you are meaning by convert all byte to ASCII, but the ASCII to String block in the Simulink String library may help with something like that.
Ibnul Feraji Alam
Ibnul Feraji Alam le 14 Mar 2022
To send the bits or byte to the communication port or the serial device which simulink block can I use.
Like if I want to send 10110001 to the serial device what function block I have to use?
Benjamin Thompson
Benjamin Thompson le 14 Mar 2022
Seeing the example model I posted as an image, you use the Serial Configuration block and the Serial Send block. You can start your data vector with the STX character and end your data vector with the ETX character. The Serial Send block accepts input as a vector of bytes as I have shown. So there is no need for you to convert bytes into a binary string representation.
If you want to send the binary 10110001 to the serial port, then convert to decimal 177. Then pass 177 as the single uint8 input to the Serial Send block. Or you can enter bin2dec('10110001') as the constant value on a const block, and configure the output data type of the block to uint8.
If you have an oscilloscope, hook the scope to pins 3 (transmit) and 5 (ground) of your 9-pin serial port in order to see the result of this.
Ibnul Feraji Alam
Ibnul Feraji Alam le 15 Mar 2022
I am having trouble over sending the signal over serial send block.
Which generator signal I need to use. Like if I want to send hex "AA" or decimal "170" or binary "10101010" which "create and connect generator" I need to use. Also If you could tell me the parameters that would be helpful.
Is there anything needed to be changed for the header and terminator?
Thank you.
Benjamin Thompson
Benjamin Thompson le 15 Mar 2022
I don't know what the "create and connect generator" does. Connect a constant source block to your Serial Send Block input. Give the constant block a value of 170 decimal, and configure its output data type to uint8.
As for the other parameters, it is your device. The header and terminator could be your STX and ETX characters. Blocking mode, you may have to try it with and without that checked to see what works best for timing.
Then when you are sure the serial port is sending out three bytes (STX + 170 + ETX), then make the middle part the full six bytes long using the mux block like I showed in my example.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by