help with Matlab and serial communication
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:
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
' {$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 MainMAtLab
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; endCommand 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.
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.
Open mode