Shop OBEX P1 Docs P2 Docs Learn Events
help with Matlab and serial communication — Parallax Forums

help with Matlab and serial communication

erick_2987erick_2987 Posts: 11
edited 2011-03-11 19:00 in BASIC Stamp
hi all,
i am trying to establish a serial communication with my bs2p and matlab. Here is my bs code:
*************************************************
' Variables
test VAR Word

'**********************
' Main Program

test = 6

Main:
SEROUT 16,2400,[DEC test, CR]

PAUSE 250
GOTO Main

END
***********************************************************

and my matlab code:
***************************************************************************************************
s = serial('COM3','BaudRate',2400,'DataBits',8,'Parity', 'none', 'FlowControl', 'none');
s.terminator = 'CR';
s.ReadAsyncMode = 'manual';
fopen(s)

s_scan = fscanf(s,'%d\n');
s_get = fgetl(s);
s_read = fread(s);
fclose(s);
********************************************************************************************************

when i execute the program, the output from the matlab is:
Warning: A timeout occurred before the read operation completed.
Warning: A timeout occurred before the read operation completed.
Warning: The specified amount of data was not returned within the Timeout period.

meanwhile the debug window in bs2 gives me:

Comments

  • PublisonPublison Posts: 12,366
    edited 2011-03-09 04:35
    You have to use the BAUDMODE number for the baud rate on BS2'. Look in the SEROUT section of the Basic Stamp manual or the Help in the Basic Stamp Editor for the table of acceptable BAUDMODE numbers. Be sure and pick the table for the BS2P.
    erick_2987 wrote: »
    hi all,
    i am trying to establish a serial communication with my bs2p and matlab. Here is my bs code:
    *************************************************
    ' Variables
    test VAR Word

    '**********************
    ' Main Program

    test = 6

    Main:
    SEROUT 16,2400,[DEC test, CR]

    PAUSE 250
    GOTO Main

    END
    ***********************************************************

    and my matlab code:
    ***************************************************************************************************
    s = serial('COM3','BaudRate',2400,'DataBits',8,'Parity', 'none', 'FlowControl', 'none');
    s.terminator = 'CR';
    s.ReadAsyncMode = 'manual';
    fopen(s)

    s_scan = fscanf(s,'%d\n');
    s_get = fgetl(s);
    s_read = fread(s);
    fclose(s);
    ********************************************************************************************************

    when i execute the program, the output from the matlab is:
    Warning: A timeout occurred before the read operation completed.
    Warning: A timeout occurred before the read operation completed.
    Warning: The specified amount of data was not returned within the Timeout period.

    meanwhile the debug window in bs2 gives me:
  • erick_2987erick_2987 Posts: 11
    edited 2011-03-09 08:40
    hi, sorry for the above mistake.I have set the baudrate at 9600 but still unable to get the answer.
  • PublisonPublison Posts: 12,366
    edited 2011-03-09 11:15
    What value did you use for BAUDMODE?
    erick_2987 wrote: »
    hi, sorry for the above mistake.I have set the baudrate at 9600 but still unable to get the answer.
  • Mike GMike G Posts: 2,702
    edited 2011-03-11 18:14
    Stamp code
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    
    #SELECT $STAMP
      #CASE BS2SX, BS2P
        N2400        CON     1021+$8000
      #CASE BS2PX
        N2400        CON     1646+$8000
      #CASE #ELSE
        N2400        CON     396+$8000
    #ENDSELECT
    
    test    VAR      Byte
    test = 6
    
    Main:
      SEROUT 16,N2400,[DEC test, CR]
      PAUSE 250
    GOTO Main
    

    MAtLab
    function SerialTest()
    
        % Set the port paramenter
        s=serial('COM16', 'BaudRate', 2400, 'Parity', 'none', 'DataBits', 8, 'StopBits', 1);
        s.terminator = 'CR';
        % open the port
        fopen(s);
        
        % display the com port resources
        com = instrfind;
        disp(com);
      
        % Expecting 1 byte
        out=fread(s,1);
        
        % Display the byte
        disp(out);
        
        % Clean up
        fclose(s);
        delete(s);
        clear s;
    end
    

    Command Window
       Serial Port Object : Serial-COM16
    
       Communication Settings 
          Port:               COM16
          BaudRate:           2400
          Terminator:         'CR'
    
       Communication State 
          Status:             open
          RecordStatus:       off
    
       Read/Write State  
          TransferStatus:     idle
          BytesAvailable:     0
          ValuesReceived:     0
          ValuesSent:         0
     
        54
    
    >> 
    

    The result is 54 or 0x36 which is ASCII for 6.
  • erick_2987erick_2987 Posts: 11
    edited 2011-03-11 18:29
    my baudmode is 240
  • Mike GMike G Posts: 2,702
    edited 2011-03-11 19:00
    my baudmode is 240

    Not following you.
    Did you try the code and it didn't work? Make sure you update the COMxx port in MatLab.
    What is 240? 2400 baud?

    1021 equals 2400 baud in the BS2p SEROUT command. See basic stamp help.
    SEROUT 16, 1021, [DEC test, CR]
    

    Open mode
    SEROUT 16, 1021 + $8000, [DEC test, CR]
    
Sign In or Register to comment.