How to accept data from a USB to RS232 converter to MATLAB and storing the data to a variable

6 views (last 30 days)
Hello everyone, I'm using an ATMega8 Microcontroller ADC to convert the output of a photodiode to digital form. This digital data I've stored in a register of ATMega8. My requirement is to transfer the data from the ATMega8 register to a MATLAB variable.
I'm using a Laptop with two USB ports but no serial/parallel port. I cannot affort to use a DAQ or some custom solution for this purpose. Thus I think my only option is to use a USB to RS232 converter. Data is output from the microcontroller in serial format.
Here's what I understand:
  • Data is stored in one of the registers of ATMega8. This is in hexadecimal.
  • I place the data to be communicated in the UDR (UART Data Register) register of ATMega8.
  • Data is transmitted through the Tx pin of the microcontroller in serial format.
  • My USB to RS232 converter converts the serial data from the microcontroller to USB format.
  • The USB end of the converter is connected to my PC.
  • Data received at the USB port will be in a different format from the serially communicated data.
How can I get this data from that converter? I need to store the Hexadecimal value of the received data to a variable in MATLAB.
Would it be easier to use C/C++ to receive data and import it to MATLAB?
Once again, I cannot use a DAQ. The USB to RS232 converter isn't a supported brand. Its lika a homemade converter.
So is there a MATLAB solution for this? What should I do?

Accepted Answer

Walter Roberson
Walter Roberson on 23 Sep 2013
The data is probably stored in binary on the ATMega8 rather than hexadecimal.
The USB to RS232 convertor is probably buffering bytes and sending them several at a time, rather than sending them as soon as they are received.
Data received at the USB port will be in the same format as what was placed on the UDR, unless you used unusual configuration options such as 9 bits per byte. However, the data will probably be buffered so the timing would differ.
You need a USB driver to get data from the USB port. You can probably use a generic one if the manufacturer does not supply one.
You will need to use instrfind() to locate the name of the USB port. Then use serial() to create a serial object referring to the port, configure it appropriately, and then fopen() it. Once it is fopen(), fread() or fscanf() or fgetl() from the serial object.
  2 Comments
Abhay Mohan
Abhay Mohan on 9 Oct 2013
Edited: Abhay Mohan on 9 Oct 2013
Thank you for the answer. Could you please see what I have to add to this?
x = 0;
i = 1;
DataIn = instrfind;
set(DataIn,'BaudRate',4800,'StopBits', 1);
%What kind of terminator would I put here? I want to read this one byte at a time for an indefenite amount of time.
while(1)
fopen(DataIn);
% Is IDN command required? I do not understand its necessity.
Data = fread(DataIn,1);
x(i) = Data;
%Operations with x(i)
i = i+1;
fclose(Datain);
end
I saw something about possible timeout error. Would this code work fine? I want to use the Data to convert the received ADC value to the voltage value.
Walter Roberson
Walter Roberson on 10 Oct 2013
Edited: Walter Roberson on 10 Oct 2013
Try
set(DataIn, 'terminator', 'byte', 'InputBufferSize', 1)
The IDN command is necessary to prompt the remote device to start sending data.
Do not fopen() and fclose() DataIn for every iteration. Open it once before the loop. And you should configure some way to leave the loop smoothly, even if only an onCleanup() to fclose(Datain) when you get a control-C

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!