|
Hi I have a custom made datalogger that I can access using Excel with VBA code using a USB-serial converter.
I am trying to use matlab instead but am having difficulties as fopen(s) doesn't return anything.
When running the serial('COM10') function the number of bytes available is 0. Should that be the case? My settings for the port are correct apart from that I am unsure what the terminator is required to be. Do you think that would be causing the problem?
The working VBA code is beneath the matlab code. sorry for the long post.
function data_log()
% Create a serial port object
instrreset
delete(instrfindall)
s=serial('COM10','BaudRate',115200,'Parity','none','DataBits',8,'StopBits',1,'Terminator','LF/CR','TimeOut',5); % create serial object (port dependent)
% Connect to the device
fopen(s) % open the serial port for reading/writing
% Set logging options
no_samples=30;
no_channels=1;
current_sample=zeros(1,no_samples); % preallocation
tic
% Write and read data
% 1) read data from the device and store in the input buffer
for i=1:no_samples-1
i
current_sample(i)=i;
fprintf(s,'%d',1); % send a decimal 1 (not ascii code 1), prompts the logger to start sending data
if i==1
buffer=[];
end
[in,count,msg]=fread(s);
buffer=buffer+in;
while length(buffer)<8 %checks when the number of bits in buffer =8
j=1;
for n=1:no_channels
hi=buffer(j,j:j+1);
lo=buffer(j+1,j+1:j+2);
% work out whether positive or negative number,
% if greater than 128 then its positive
if hi<128
adc_value=hi*256+lo;
else
adc_value=(65536-(hi*256+lo))*-1;
end
j=j+2;
% outputs as voltage
voltage_output(i)=round(adc_value/3276.8)
end
end
clear buffer
sample_time=toc;
end
% Disconnect and clean up
fclose(s)
delete(s)
VBA Code that works:
Sub Button10_Click()
Dim buffer As String
Dim rs As New MSComm
Dim adc(4) As Variant
Call Macro1 'clear worksheet
Cells(2, 1) = "scan" 'create title
Cells(2, 2) = "Time" 'create title
For n = 3 To 6
Cells(2, n) = "ADC" & Str(n - 2) 'create title
Next n
rs.CommPort = 10 'port device is attached to
rs.Settings = "115200,n,8,1" ' 1) Baud rate, 2) parity none, 3)no bits, 4)number of stop bits
rs.InputLen = 0 'allows the buffer to continuously fill
rs.PortOpen = True 'open the port
t = Timer
x$ = rs.Input 'x is a string, rs.Input is reading the serial COM port, effectively clearing the buffer
For samps = 1 To Cells(1, 2) 'for samples 1 to maximum specified in cell(1,2) of sheet
rs.Output = Chr$(1) 'outputs the number '1' to the datalogger as trigger
buffer = "" 'clears the string buffer
Do
DoEvents
buffer = buffer + rs.Input
Loop While Len(buffer) < 8 'checks when the no-bits =8
Cells(samps + 2, 1) = samps 'current sample no
Cells(samps + 2, 2) = Timer - t 'current time
j = 1
For n = 1 To 4 'for all 4 channels
hi = Asc(Mid(buffer, j, 1)) 'finding the jth digit +next digit
lo = Asc(Mid(buffer, j + 1, 1))
If hi < 128 Then 'works out whether positive or negative number, if high is greater than 128 then its positive
adc_value = hi * 256 + lo
Else
adc_value = (65536 - (hi * 256 + lo)) * -1
End If
j = j + 2
Cells(samps + 2, n + 2) = Round(adc_value / 3276.8, 4) 'outputs voltage for chan 1 to third column in sheet
Next n 'carry on for next channel
Next samps 'carry on for next sample
rs.PortOpen = False 'close port
End Sub
|