Like Tree30Likes

Wirte and read serial port

This is a discussion on Wirte and read serial port within the C Programming forums, part of the General Programming Boards category; Originally Posted by ZuK Code: result2 = write(mainfd,get,8); Your program will only send the first 8 characters of that string ...

  1. #76
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by ZuK View Post
    Code:
    result2 = write(mainfd,get,8);
    Your program will only send the first 8 characters of that string and this is
    \xF5\x00
    you need to find a way to convert the string \xF5\x01\x00\x00\x00\x00\x01\xF5
    into an array of unsigned characters that contains
    Code:
    unsigned char buff[] = { 245, 1, 0, 0,  0, 0, 1, 245 };
    And note. there isn't anything like hex data. There exists only hex representation of data
    Kurt
    This is what i did:

    Code:
    unsigned myCommand[100] = { 0xf5, 0x01, 0x00, 0x01, 0x03, 0x00, 0x03, 0xf5,};
    
    result2 = write(mainfd, myCommand, sizeof(myCommand));
    now my .txt file content is like this:

    0xf5, 0x00, 0x00, 0x00, 0x18, 0x0c, 0x23, 0xe1, 0x61, 0x1c, 0xae, 0x4c, 0x21, 0x23, 0xa6, 0xcc, 0x21, 0x24, 0xb7, 0xe4, 0x01, 0x32, 0x22, 0x8c, 0x01, 0x41, 0x17, 0x46, 0xe1, 0x4d, 0x25, 0x2c, 0xc1, 0x4d, 0x34, 0x4e, 0x81, 0x4d, 0x91, 0x9b, 0x41, 0x54, 0x8e, 0x46, 0x21, 0x5a, 0x1a, 0x43, 0xc1, 0x5e, 0x10, 0x9a, 0x41, 0x5e, 0x1f, 0x57, 0x61, 0x61, 0xa4, 0xec, 0xe1, 0x63, 0x19, 0x82, 0xe1, 0x66, 0x26, 0x94, 0xc1, 0x88, 0x1f, 0xc0, 0x81, 0x21, 0x10, 0xc9, 0x22, 0x31, 0x1f, 0x0a, 0x82, 0x35, 0x24, 0x4e, 0x02, 0x3f, 0x34, 0xce, 0xe2, 0x43, 0xb6, 0x65, 0xe2, 0x4f, 0x35, 0xa7, 0x22, 0x80, 0xaf, 0xa9, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0xf5,

    How can i add this into the myCommand array?

  2. #77
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Marvin Gorres View Post
    How can i add this into the myCommand array?
    You need to parse the file and convert the values represented as text to an actual numeric value.

    Depending on the format of your file you could simply use fscanf() with the %x format specifier. Another option is to use a combination of fgets() and sscanf() or a combination of fgets(), strtok() and strtoul() or writing your own parser or ...

    Bye, Andreas

  3. #78
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by AndiPersti View Post
    You need to parse the file and convert the values represented as text to an actual numeric value.

    Depending on the format of your file you could simply use fscanf() with the %x format specifier. Another option is to use a combination of fgets() and sscanf() or a combination of fgets(), strtok() and strtoul() or writing your own parser or ...

    Bye, Andreas
    convert? is there any other method without altering the original data?

  4. #79
    Registered User
    Join Date
    May 2010
    Posts
    2,680
    Where did this information come from?
    0xf5, 0x00, 0x00, 0x00, 0x18, 0x0c, 0x23, 0xe1, 0x61, 0x1c, 0xae, 0x4c, 0x21, 0x23, 0xa6, 0xcc, 0x21, 0x24, 0xb7, 0xe4, 0x01, 0x32, 0x22, 0x8c, 0x01, 0x41, 0x17, 0x46, 0xe1, 0x4d, 0x25, 0x2c, 0xc1, 0x4d, 0x34, 0x4e, 0x81, 0x4d, 0x91, 0x9b, 0x41, 0x54, 0x8e, 0x46, 0x21, 0x5a, 0x1a, 0x43, 0xc1, 0x5e, 0x10, 0x9a, 0x41, 0x5e, 0x1f, 0x57, 0x61, 0x61, 0xa4, 0xec, 0xe1, 0x63, 0x19, 0x82, 0xe1, 0x66, 0x26, 0x94, 0xc1, 0x88, 0x1f, 0xc0, 0x81, 0x21, 0x10, 0xc9, 0x22, 0x31, 0x1f, 0x0a, 0x82, 0x35, 0x24, 0x4e, 0x02, 0x3f, 0x34, 0xce, 0xe2, 0x43, 0xb6, 0x65, 0xe2, 0x4f, 0x35, 0xa7, 0x22, 0x80, 0xaf, 0xa9, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0xf5,
    Are you generating a file that contains this information or is some external program/device generating this information?

    is there any other method without altering the original data?
    You seem to be altering the data, it used to look something like "\xF5\x01\x00\x01\x03\x00\x03\xF5".

    What you want to do is put the above data into an array of unsigned character then loop thru this array and send each element of the array one element at a time (a loop).

    Jim

  5. #80
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Where did this information come from?


    Are you generating a file that contains this information or is some external program/device generating this information?



    You seem to be altering the data, it used to look something like "\xF5\x01\x00\x01\x03\x00\x03\xF5".

    What you want to do is put the above data into an array of unsigned character then loop thru this array and send each element of the array one element at a time (a loop).

    Jim
    This is what i've done:

    Code:
    unsigned char myCommand[810] = { 0xf5,0x01,0x00,0x01};
    unsigned char two[50];
    
    two[1]= "0x03";
    two[2]= "0x00";
    two[3]= "0x03";
    two[4]= "0xf5";
    
    for(j=1,j_2=4;j<5,j_2<8;j++,j_2++)
    {
        myCommand[j_2] = two[j];
    }
    after that i write it to the port:

    Code:
    result2 = write(mainfd, myCommand, sizeof(myCommand)); //return number of byte written to the port
         if ( result2 == -1 ) { perror("error: write"); }
         if ( result2 != 8 ) { fprintf(stderr,"error in write, result = %d\n", result2); }
    i get error: error in write, result = 42
    Last edited by Marvin Gorres; 04-19-2013 at 10:06 PM.

  6. #81
    Registered User
    Join Date
    May 2009
    Posts
    2,487
    My Compiler complains about this code with warnings.

    Code:
    unsigned char two[50];
    
    two[1]= "0x03";
    two[2]= "0x00";
    two[3]= "0x03";
    two[4]= "0xf5";
    Correct way is likely this.

    Code:
       unsigned char two[50];
    
        two[1] = 0x03;
        two[2] = 0x00;
        two[3] = 0x03;
        two[4] = 0xf5;
    You need to get a better compiler or turn up the warnings!!

    Tim S.
    Marvin Gorres likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #82
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by stahta01 View Post
    My Compiler complains about this code with warnings.

    Code:
    unsigned char two[50];
    
    two[1]= "0x03";
    two[2]= "0x00";
    two[3]= "0x03";
    two[4]= "0xf5";
    Correct way is likely this.

    Code:
       unsigned char two[50];
    
        two[1] = 0x03;
        two[2] = 0x00;
        two[3] = 0x03;
        two[4] = 0xf5;
    You need to get a better compiler or turn up the warnings!!

    Tim S.
    Tried already, my device seems to respond but the error is still there. 42 byte were sent.

  8. #83
    Registered User
    Join Date
    May 2009
    Posts
    2,487
    Quote Originally Posted by Marvin Gorres View Post
    Tried already, my device seems to respond but the error is still there. 42 byte were sent.
    So, you told it to send over 800 bytes; why do you think 42 is wrong?
    write (C System Call) - Code Wiki

    Hint: what value does sizeof(myCommand) have?
    What meaning does the third parameter of write have?

    Tim S.
    Marvin Gorres likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  9. #84
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    Tried already, my device seems to respond but the error is still there. 42 byte were sent.
    Well, I haven't been following this thread that much, but I must ask: how are you calculating the received/sent bytes?

    Soma

  10. #85
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by stahta01 View Post
    So, you told it to send over 800 bytes; why do you think 42 is wrong?
    write (C System Call) - Code Wiki

    Tim S.
    Owh sorry! i forgot to change the 801 to 8..my mistake. now it works properly already. thanks for pointing it out.

    Now what i have to do is how to transfer an array which have the following value into something like this
    Code:
     two[1] = 0x03;
    
     two[2] = 0x00;
     two[3] = 0x03;
     two[4] = 0xf5;
    array value:
    Result = 0xf5, 0x00, 0x00, 0x00, 0x0a,
    Result = 0x32, 0xaa, 0x97, 0x61, 0x48
    Result = , 0x28, 0x17, 0x01, 0x4a, 0x9
    Result = 9, 0x0f, 0x61, 0x4d, 0xb5, 0x
    Result = 86, 0x21, 0x66, 0x29, 0x63, 0
    Result = x81, 0x67, 0x2c, 0x22, 0x01,
    Result = 0x72, 0x24, 0x22, 0xa1, 0x76,
    Result = 0x9c, 0x63, 0x01, 0x8a, 0x25
    Result = , 0xcc, 0x81, 0x2a, 0xa9, 0x2
    Result = b, 0x82, 0x36, 0x2f, 0x82, 0x
    Result = 02, 0x51, 0x18, 0x66, 0x02, 0
    Result = x80, 0x15, 0xe4, 0x62, 0x00,
    Result = 0x00, 0x00, 0x00, 0x00, 0x00,
    Result = 0x01, 0x00, 0x00, 0x00, 0x00
    Result = , 0x00, 0x00, 0x00, 0x00, 0x0
    Result = 1, 0x00, 0x00, 0x00, 0x00, 0x
    Result = 00, 0x00, 0x00, 0x00, 0x01, 0
    Result = x00, 0x00, 0x00, 0x00, 0x00,
    Result = 0x00, 0x00, 0x00, 0x01, 0x00,
    Result = 0x00, 0x00, 0x00, 0x00, 0x00
    Result = , 0x00, 0x00, 0x00, 0x01, 0x0
    Result = 0, 0x00, 0x00, 0x00, 0x00, 0x
    Result = 00, 0x00, 0x00, 0x00, 0x00, 0
    Result = x00, 0x00, 0x00, 0x00, 0x00,
    Result = 0x00, 0x00, 0x00, 0x00, 0x00,
    Result = 0x00, 0x00, 0x00, 0x00, 0x00
    Result = , 0x00, 0x00, 0x00, 0x00, 0x0
    Result = 0, 0x00, 0x00, 0x00, 0x00, 0x
    Result = 00, 0x00, 0x00, 0x00, 0x00, 0
    Result = x00, 0x00, 0x00, 0x00, 0x00,
    Result = 0x00, 0x00, 0x00, 0x00, 0x00,
    Result = 0x00, 0x00, 0x00, 0x00, 0x00
    Result = , 0x00, 0x00, 0x00, 0x00, 0x0
    Result = 0, 0x00, 0x00, 0x00, 0x00, 0x
    Result = 00, 0x00, 0x00, 0x00, 0x00, 0
    Result = x00, 0x00, 0x00, 0x00, 0x00,
    Result = 0x00, 0x00, 0x00, 0x00, 0x00,
    Result = 0x00, 0x00, 0x00, 0x00, 0x00
    Result = , 0x00, 0x00, 0x00, 0x00, 0x0
    Result = 0, 0x00, 0x00, 0x00, 0x00, 0x
    Result = 00, 0x00, 0x00, 0x00, 0x01, 0
    Result = xf5,
    any idea?

  11. #86
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You have already been told, that you need to convert the textual representation of your hex values to the actual values. There is no shortcut if all you have is just text.

    Do you know the difference between text and binary data/bytes?

    Bye, Andreas

  12. #87
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by phantomotap View Post
    Well, I haven't been following this thread that much, but I must ask: how are you calculating the received/sent bytes?

    Soma
    its in write() library, the write() will return you the number of byte as long as you set the correct parameter for it.

  13. #88
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by AndiPersti View Post
    You have already been told, that you need to convert the textual representation of your hex values to the actual values. There is no shortcut if all you have is just text.

    Do you know the difference between text and binary data/bytes?

    Bye, Andreas
    alright thanks for the suggestion. Too many thing now and i keep forgetting things.

  14. #89
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by AndiPersti View Post
    You have already been told, that you need to convert the textual representation of your hex values to the actual values. There is no shortcut if all you have is just text.

    Do you know the difference between text and binary data/bytes?

    Bye, Andreas
    I wonder if i'm doing this correctly. Please correct me. This is what i did:

    Code:
    fscanf(fp, "%s %s %s %s", str1, str2, str3, str4);
    
    two[1]= str1;
    two[2]= str2;
    two[3]= str3;
    two[4]= str4;
    
    for(j=1,j_2=4;j<5,j_2<8;j++,j_2++)
    {
        myCommand[j_2] = two[j];
    }
    fp call a .txt file which have content of "0x03 0x00 0x03 0xf5". When i write it to the port it has segmentation fault error.

  15. #90
    Registered User
    Join Date
    May 2009
    Posts
    2,487
    PLEASE turn on the Compiler Warnings!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!

    You CAN NOT fit a C-String [zero terminated char array] into a single char!

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Page 6 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read in from serial port; getchar()
    By s.dodd in forum C Programming
    Replies: 9
    Last Post: 10-23-2011, 10:50 AM
  2. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05:56 PM
  3. serial port read
    By mackrackit in forum C++ Programming
    Replies: 6
    Last Post: 03-22-2006, 12:40 AM
  4. How to read from a serial port
    By WDT in forum C++ Programming
    Replies: 6
    Last Post: 05-10-2004, 06:31 AM
  5. Serial port read..can someone tell me..
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 08:21 AM

Tags for this Thread


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