SEROUT to Matlab
Archiver
Posts: 46,084
This is the last step for me in getting the pipes connected for a
feedback control loop. Thanks for all the advice so far guys.
The problem I'm experiencing now is - I am able to get the data sent
by SEROUT using the board's debugger, but I can't seem to get this
data from matlab.
I thought that there is some serial buffer that matlab and the
debugger reads from. In matlab, I am able to get the echo of the
command I sent via this buffer, but when I try to read the buffer
again for the SEROUT data there is nothing there and it just sits
there and waits infinitely.
Any ideas why is this happening? Do I need to do synchronous
communication or something like that?
matlab code:
**********************************************************
>>s1 = serial('COM1', 'Baudrate', 9600, 'DataTerminalReady', 'off')
Serial Port Object : Serial-COM1
Communication Settings
Port: COM1
BaudRate: 9600
Terminator: LF
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
>> fopen(s1)
>> fprintf(s1, 'cmd:')
>> fprintf(s1, '1')
appropriate LEDs turn on
>> fscanf(s1)
ans =
cmd:
>>fscanf(s1)
ans =
1
>> fscanf(s1)
It then waits infinitely.
*************************************************
STAMP CODE:
' {$STAMP BS2 }
PIN_NUM VAR NIB
PIN_CMD VAR BIT
PIN_NUM = 0
DataIn:
TOGGLE 14
SERIN 16, 16468, [noparse][[/noparse]WAIT("cmd:")]
SERIN 16, 16468, [noparse][[/noparse]DEC PIN_CMD]
TOGGLE 14
IF PIN_CMD = 0 THEN TurnOff
IF PIN_CMD = 1 THEN TurnOn
GOTO DataIn
TurnOn:
TOGGLE 12
HIGH PIN_NUM
DEBUG CR, BIN1 IN0
Pause 1000
TOGGLE 12
GOTO DataOut
TurnOff:
TOGGLE 10
LOW PIN_NUM
DEBUG CR, BIN1 IN0
Pause 1000
TOGGLE 10
GOTO DataOut
DataOut:
TOGGLE 6
SEROUT 16, 16468, [noparse][[/noparse]DEC IN0]
Pause 1000
TOGGLE 6
GOTO DataIn
feedback control loop. Thanks for all the advice so far guys.
The problem I'm experiencing now is - I am able to get the data sent
by SEROUT using the board's debugger, but I can't seem to get this
data from matlab.
I thought that there is some serial buffer that matlab and the
debugger reads from. In matlab, I am able to get the echo of the
command I sent via this buffer, but when I try to read the buffer
again for the SEROUT data there is nothing there and it just sits
there and waits infinitely.
Any ideas why is this happening? Do I need to do synchronous
communication or something like that?
matlab code:
**********************************************************
>>s1 = serial('COM1', 'Baudrate', 9600, 'DataTerminalReady', 'off')
Serial Port Object : Serial-COM1
Communication Settings
Port: COM1
BaudRate: 9600
Terminator: LF
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
>> fopen(s1)
>> fprintf(s1, 'cmd:')
>> fprintf(s1, '1')
appropriate LEDs turn on
>> fscanf(s1)
ans =
cmd:
>>fscanf(s1)
ans =
1
>> fscanf(s1)
It then waits infinitely.
*************************************************
STAMP CODE:
' {$STAMP BS2 }
PIN_NUM VAR NIB
PIN_CMD VAR BIT
PIN_NUM = 0
DataIn:
TOGGLE 14
SERIN 16, 16468, [noparse][[/noparse]WAIT("cmd:")]
SERIN 16, 16468, [noparse][[/noparse]DEC PIN_CMD]
TOGGLE 14
IF PIN_CMD = 0 THEN TurnOff
IF PIN_CMD = 1 THEN TurnOn
GOTO DataIn
TurnOn:
TOGGLE 12
HIGH PIN_NUM
DEBUG CR, BIN1 IN0
Pause 1000
TOGGLE 12
GOTO DataOut
TurnOff:
TOGGLE 10
LOW PIN_NUM
DEBUG CR, BIN1 IN0
Pause 1000
TOGGLE 10
GOTO DataOut
DataOut:
TOGGLE 6
SEROUT 16, 16468, [noparse][[/noparse]DEC IN0]
Pause 1000
TOGGLE 6
GOTO DataIn
Comments
First thing I'd do is remove or comment out the DEBUG statements.
The DEBUG output is almost certainly confusing matlab, since it
can't differentiate between data and debug info.
Second, here's a WAG--perhaps matlab is awaiting a line feed to
terminate the incoming data. Your setup parameters sort of suggest
this.
Try adding a line feed character to terminate the DataOut stream:
DataOut:
TOGGLE 6
SEROUT 16, 16468, [noparse][[/noparse]DEC IN0, $0A]
Pause 1000
TOGGLE 6
GOTO DataIn
Or maybe $0D, $0A.
Regards,
Steve