How to do MATLAB for serial port

shahrul
@shahrul-Ex12xz โ€ข Oct 25, 2024

Hi,
I want to interface MATLAB with PIC, to do SCADA. MATLAB communicate with USB port, then I have USB-to-UART converter to connect to PIC.
Does anybody have source to do this? It's is doing in GUI or m file?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Ashraf HZ

    @Ash • Dec 4, 2010

    Hey shahrul,

    I'm assuming the USB-UART converter is a serial port emulator. In Matlab, you just need to create a serial port object, open it, then read the data. At the most simple, the code will be:

    s = serial('COM1'); % Create serial port object s
    set(s,'BaudRate',9600)
    fopen(s); %open port
    x = fscanf(s); % read data into variable x
    fclose(s) % close port
    delete(s)
    clear s

    The above code assumes you are using COM1 port for the USB serial emulator. You should change accordingly. Also, 8N1 parameter is used as default. Of course, you may need to code more according to your project, refer to the Matlab reference below:

    #-Link-Snipped-#

  • shahrul
    shahrul OP

    @shahrul-Ex12xz • Dec 5, 2010

    Thanks, I will try.