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; UPDATE: Code: int myCommand[8] = { 245,1,0,0,0,0,0,0 }; int str1, str2, str3, str4; this is how i take data from ...

  1. #91
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    UPDATE:

    Code:
    int myCommand[8] = { 245,1,0,0,0,0,0,0 };
    
        int str1, str2, str3, str4;
    this is how i take data from .tct file and add up into the myCommand array:

    Code:
    fp = fopen ("file.txt", "r");
    rewind(fp);
    fscanf(fp, "%d %d %d %d", &str1, &str2, &str3, &str4);
    
    printf("%d",str1);
    
    myCommand[4] = str1;
    myCommand[5] = str2;
    myCommand[6] = str3;
    myCommand[7] = str4;
    note: the .txt file has this value ==> "0 0 1 245"

    i was supposed to send 8 byte of data which is "245,1,0,0,0,0,1,245" but i get error:

    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); }
    error ==> error in write, result = 32

  2. #92
    Registered User
    Join Date
    May 2010
    Posts
    2,711
    Please post your current code.

    Jim

  3. #93
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    int myCommand[8] = { 245,1,0,0,0,0,0,0 };
    result2 = write(mainfd, myCommand, sizeof(myCommand));
    This will not work. myCommand needs to be unsigned char
    That's why 32 bytes were sent ( 8 * sizeof int ).


    Kurt

  4. #94
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by ZuK View Post
    Code:
    int myCommand[8] = { 245,1,0,0,0,0,0,0 };
    result2 = write(mainfd, myCommand, sizeof(myCommand));
    This will not work. myCommand needs to be unsigned char
    That's why 32 bytes were sent ( 8 * sizeof int ).


    Kurt
    Thanks Kurt!!

  5. #95
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by ZuK View Post
    Code:
    int myCommand[8] = { 245,1,0,0,0,0,0,0 };
    result2 = write(mainfd, myCommand, sizeof(myCommand));
    This will not work. myCommand needs to be unsigned char
    That's why 32 bytes were sent ( 8 * sizeof int ).


    Kurt
    Now i'm trying to transfer 211 byte of data so i have this code below:

    Code:
    unsigned char myCommand[211] = { 245,65,0,196,0,0,133,245,245,0,1,1,245,0,0,0,15,29,40,43,193,34, 169,87,97,39,46,1,97,43,178,26,225,55,38,215,161,68,22,229,129,79,38,101,193,86,170,225,161,96,35,163,193,102,155,99,97,122,53,225,1,123,36, 204,33,137,39,75,97,59,22,79,162,69,51,29,226,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,3,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,75,245 };
    and send it using command below:

    Code:
    result2 = write(mainfd, myCommand, sizeof(myCommand)); //return number of byte written to the port
         if ( result2 == -1 ) { perror("error: write"); }
         if ( result2 != 211 ) { fprintf(stderr,"error in write, result = %d\n", result2); }
    But i get the error : error in write, result = -45

  6. #96
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,231
    O_o

    The `write' function can fail to write all bytes spuriously; you are just treating that relatively normal condition as an error.

    The question is, was there really an error. We don't know, and you code doesn't check.

    Soma

  7. #97
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    Quote Originally Posted by Marvin Gorres View Post
    But i get the error : error in write, result = -45
    -45 or positive 45?

    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

  8. #98
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by phantomotap View Post
    O_o

    The `write' function can fail to write all bytes spuriously; you are just treating that relatively normal condition as an error.

    The question is, was there really an error. We don't know, and you code doesn't check.

    Soma
    Yea you got a point there. Any suggestion how to check the error?

  9. #99
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by stahta01 View Post
    -45 or positive 45?

    Tim S.
    its negative.

  10. #100
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I would change this condition
    Code:
    if ( result2 == -1 ) {
    to
    Code:
    if ( result2 < 0 ) {
    because write returns a negative number in case of failure.
    Kurt
    stahta01 likes this.

  11. #101
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by ZuK View Post
    I would change this condition
    Code:
    if ( result2 == -1 ) {
    to
    Code:
    if ( result2 < 0 ) {
    because write returns a negative number in case of failure.
    Kurt
    Hello Zuk, i changed it already. Thanks for the information, now my error looks like this:

    error: write: Success
    error in write, result = -45

    but i just disable the second error and i get the correct result.
    Last edited by Marvin Gorres; 04-20-2013 at 10:05 PM.

  12. #102
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    even if i changed to this code

    Code:
    result2 = write(mainfd, myCommand, sizeof(myCommand)); //return number of byte written to the port
         if ( result2 < 0 ) { perror("error: write"); }
         if ( result2 != 207 ) { fprintf(stderr,"error in write, result = %d\n", result2); }
    still have error:

    error: write: Success
    error in write, result = -49

    suppose it retun number of byte of 207, but why -49??

  13. #103
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > suppose it retun number of byte of 207, but why -49??
    207 IS -49, if your result happens to be a signed char, rather than an integer.

    That was post #43 two weeks ago, and your response was "Ok thanks for the advice!"
    ZuK and AndiPersti like this.
    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.

Page 7 of 7 FirstFirst 1234567
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