communication between matlab and microcontroller (problem with the C code)

This is a discussion on communication between matlab and microcontroller (problem with the C code) within the C Programming forums, part of the General Programming Boards category; Hi there ! I'm trying to send 4 string signals as one array from Matlab to dspic30F4013. Each signal is ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    communication between matlab and microcontroller (problem with the C code)

    Hi there !

    I'm trying to send 4 string signals as one array from Matlab to dspic30F4013. Each signal is 13 bytes. I use MPLAB X IDE. In the Microcontroller code, I reconstruct the 4 signals back from the received array. Then add the first two signals together and the same with the signals 3 and 4 so I just send 2 signals back to the MATLab, again as an array that have "a" after the first signal and the terminator "/r" after the second. This is done, so it will be easy to recognize them by the Matlab code, by reconstructing the bytes of the first signal until "a" is reached and then start building the other signal. However I keep facing problems since I don't receive anything in the matlab. Here is the code and I hope somebody can help me.. thanks


    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    
    #include <p30F4013.h> 
    
    _FOSC (FRC_PLL16 & CSW_FSCM_OFF); 
    
    #define FOSC 7370000 
    #define PLL 16 
    #define FCY ((FOSC * PLL) / 4.0) 
    
    void uart_init (unsigned short uart_register); 
    char uart_get_byte (void); 
    void uart_send_byte (char byte); 
    void uart_send_string (char *string, int size); 
    void uart_read_string (char *string, int size, char delimeter); 
    
    int main(int argc, char** argv) 
    { 
    char recieved_bytes[54], sent_bytes[28]; 
    char x1[13], x2[13], x3[13], x4[13]; 
    float value1, result1, value2, result2, value3, value4 ; 
    int i; 
    
    
    
    uart_init (47); 
    
    while (1) 
    { 
    /* Read the number as a string from the serial port*/ 
    uart_read_string (recieved_bytes, 54, '\r'); 
    
    for (i=0;i<13;i++) 
    { 
    x1=recieved_bytes; 
    x2=recieved_bytes[i+13]; 
    x3=recieved_bytes[i+26]; 
    x4=recieved_bytes[i+39]; 
    } 
    
    /* Cast the recieved bytes into a double percision number */ 
    value1 = atof(x1) ; 
    value2 = atof(x2) ; 
    value3 = atof(x3) ; 
    value4 = atof(x4) ; 
    
    /* Perform Control Algorithm */ 
    result1 = value1 * value2; 
    result2 = value3 * value4; 
    
    sprintf(sent_bytes,"%fa%f", result1,result2); 
    
    
    
    /* Sent the converted float */ 
    uart_send_string (sent_bytes, 28); 
    
    
    } 
    
    return EXIT_SUCCESS; 
    } 
    
    void uart_init (unsigned short uart_register) 
    { 
    /* Set the baudrate register to correct value */ 
    U1BRG = uart_register; 
    
    /* Enable UART, TX and RX */ 
    U1MODEbits.UARTEN = 1; 
    U1STAbits.UTXEN = 1; 
    } 
    
    char uart_get_byte (void) 
    { 
    char Recieved; 
    
    while (U1STAbits.URXDA == 0); 
    Recieved = U1RXREG; 
    
    return Recieved; 
    } 
    
    void uart_send_byte (char byte) 
    { 
    while (U1STAbits.UTXBF == 1); 
    U1TXREG = byte; 
    } 
    
    void uart_send_string (char *string, int size) 
    { 
    int i; 
    
    for (i = 0; i < size; i++) 
    { 
    if (string == '\0') 
    break; 
    uart_send_byte (string); 
    } 
    } 
    
    void uart_read_string (char *string, int size, char delimeter) 
    { 
    int i; 
    
    for (i = 0; i < size; i++) 
    { 
    string = uart_get_byte (); 
    
    if (string == delimeter) 
    break; 
    } 
    
    if (i >= size) 
    string[size] = '\0'; 
    else 
    string[i + 1] = '\0'; 
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    Can you receive the information using a communications program, like Hyperterm or what ever terminal program available?

    Jim

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    x1=recieved_bytes;
    x2=recieved_bytes[i+13];
    x3=recieved_bytes[i+26];
    x4=recieved_bytes[i+39];
    There are a lot of things missing array [subscript] notation here.

    Does it even compile?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    I didn't try with teraterm because what should I receive is a combination of some singles without a terminator between them

    yea it compile without any problem, even I used the same way before with MikroC to programm a PIC and it works

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    So do you understand that you can't assign arrays in C?

    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:33: error: incompatible types when assigning to type ‘char[13]’ from type ‘char *’
    bar.c:34: error: incompatible types when assigning to type ‘char[13]’ from type ‘char’
    bar.c:35: error: incompatible types when assigning to type ‘char[13]’ from type ‘char’
    bar.c:36: error: incompatible types when assigning to type ‘char[13]’ from type ‘char’
    Never equate "it compiles" with "it works and is bug-free".

    Getting something to compile is dead easy by comparison to getting it to do what you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running C++ Code from Matlab GUI
    By cpp_novice in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2011, 10:42 AM
  2. glue C and matlab code
    By shaoshao in forum C Programming
    Replies: 1
    Last Post: 06-10-2011, 09:13 AM
  3. Problem with C codes embeded in Matlab
    By KelvinSiu in forum C Programming
    Replies: 4
    Last Post: 03-16-2011, 12:13 PM
  4. Serial Communication Code for Reading
    By coolrox86 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2010, 05:30 AM
  5. Replies: 27
    Last Post: 10-25-2007, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21