MATLAB Answers

How to store continuous serial data from COM port

45 ビュー (過去 30 日間)
Clay Robinson
Clay Robinson 2019 年 8 月 2 日
Answered: Divya Yerraguntla 2019 年 8 月 8 日
Hey guys,
I'm trying to collect data from an engine dyno via serial port, and am having some issues. I have no problem obtaining one line of data using the code below, but I need a continous stream of data.
s = serial('COM3','BaudRate',19200,'DataBits',8, 'StopBits',1,'Terminator','CR/LF') %creates serial object for serial/USB adapter 'COM#', use command seriallist to find available ports
fopen(s); %open COM port connection
fprintf(s,'OD') %send "Output Data" command to controller
fgets(s) %view 1 line of output string from controller
So far my code will return one 1x15 char in the form 'S 7945T0.795R' (S= rpm, T=torque output, R/L=rotation direction. The problem is, using fgets(s), if I try and use it more than once, i get the following error:
Warning: A timeout occurred before the Terminator was reached.
'serial' unable to read any data. For more information on possible reasons, see Serial Read
This means that in order to continue to get data I have to repeat the 'Output Data' command to the dyno, and I only get 1 line of data before timeout. The dyno should output at 60Hz, any ideas on how to get a continous data stream and store it so I can parse out the variables with a different code?

  3 件のコメント

Walter Roberson
Walter Roberson 2019 年 8 月 2 日
That is how some devices are, only responding to explicit queries to avoid flooding the bus. Other devices offer commands that trigger returning back a specific number of responses.
Clay Robinson
Clay Robinson 2019 年 8 月 5 日
No, the serial port on the device puts outputs 15 bytes at a time at 60Hz, I just need some programming direction on how to read and store the data. I think the problem may be that every line is terminated with a CR-LF, and that is stopping the data read after it gathers one packet, but I'm not sure how to fix that.
Walter Roberson
Walter Roberson 2019 年 8 月 5 日
I recommend doing some testing using a program such as Teraterm

サインイン to comment.

件の回答 (1)

Divya Yerraguntla
Divya Yerraguntla 2019 年 8 月 8 日
In order to receive a continuous stream of data you can set the ‘IsContinuous’ property of the serial object (‘s’ in your case) to 1, this allows nonstop flow of data. Have a look at the link to use this property.

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。




Translated by