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


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?

Best Answer

  • 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.
  • Related Question