MATLAB Receive serial data

Source: Internet
Author: User
Tags current time fread

MATLAB receives the TXT document data sent by the serial assistant and saves it as a TXT document named at the current time. During the commissioning process, there are several major problems

1. How file names saved as TXT are automatically created

Finally, "Clock" is selected to get the system current time format xxxx-xx-xx-xx-xx-xx

2. Read the serial function to choose fread or fscanf problem;

Fread is read data, fscanf is read character, because I choose to use another computer through the serial debugging assistant to send TXT document content inside, so to matalb inside format for character;

So choose fscanf.

Fread and fscanf can read the device and read the file, so here choose to read the device, that is, before the top of a serial port object;

3. Problems with callback functions;

callback function Count I chose byte mode, and the system defaults to one character count at a time, so if you send a value of "12306" the number of accounting 5 times;

The same way the serial buffer and counting should also pay attention to.


List of programs [HTML]  View Plain  copy%%  Author: Chen Guodong  2015 February 14   %%  The main reference network for MATLAB on the help of the serial port of Chinese translation;   %%  initialization of the serial port.    clear   clc   serial_obj=serial (' COM1 ');   serial_obj. baudrate=9600;   Serial_obj. databits=8;   Serial_obj. Parity= ' None ';   serial_obj. stopbits=1;  % serial_obj. terminator=0;   Serial_obj. inputbuffersize=1024;  % serial_obj.timeout=20;  %%  Create a txt document.    ch=clock;   ch_1=int2str (CH (1));   ch_2=int2str (CH (2));   Ch_3=int2str ( CH (3));   ch_4=int2str (CH (4));   CH_5=INT2STR (CH (5));   CH_6=INT2STR (CH (6));    ch_7= '. txt ';   filename=[ch_1, '-', ch_2, '-', ch_3, '-', ch_4, '-', ch_5, '-',ch_6,ch_7];   Fileid=fopen (FileName, ' A + ');  %%  triggers the callback function.   % serial_obj. bytesavailablefcncount=256;         % serial_obj. Bytesavailablefcnmode= ' byte ';  % serial_obj. bytesavailable= @serial_cbk;  %%  Open the serial port, read and write serial content.    fopen (serial_obj);   temp_1 = fscanf (serial_obj, '%d ',;   )       % read data from device, and format as  text  % temp_1 = fscanf (FileID, '%d ',;      )      % Read data from text file  % temp_1  = fread (serial_obj,256, ' uint ');      % read binary  data from device  % temp_1 = fread (fileid,256, ' uint8 ');          % Read data from binary file  %%  Curve display.    temp_2=temp_1 ';   plot (temp_2);  %%  Save numberAccording to TXT.    fprintf (FileID, '%d ', temp_2);                   % Write data to text file  %  fprintf (serial_obj, '%d ', temp_2);             % write text to device                                    % fwrite (fileid,temp_2, ' uint8 ');               % Write data to binary file  %  fwrite (serial_obj,temp_2, ' uint8 ');          %  write binary data to device        %%  Close the serial port, Clear Buffers    FCLOSE (serial_obj);   Delete (serial_obj);   clear serial_obj  %%     Reproduced in http://blog.csdn.net/neill_ch/article/details/43815761

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.