Thread: Read RS232 with interrupt

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    5

    Question Read RS232 with interrupt

    I use this library and I want to read from rs232 with interrupt but it doesn't work
    How can I fix it?

    Code:
    #include<assert.h>
    #include<fcntl.h>
    #include<signal.h>
    #include<stdio.h>
    #include<string.h>
    #include<termios.h> 
    
    #ifdef__linux__ 
    #include<unistd.h> 
    #endif 
    
    #ifdef_WIN32 
    #include<windows.h>
     #endif 
    
    #ifdef NDEBUG
     #error This test program must work with macro "ASSERT" enabled! 
    #endif 
    
    #include"rs232.h" 
    
    void sleep_awhile(void){ 
    
    #ifdefined(__linux__) 
    usleep(1*1000*1000); 
    #elif defined(_WIN32)
     Sleep(1*1000); 
    #else 
    #error No implementation on this platform!
     #endif 
    } 
    
    void serial_callback(int status); 
    
    rs232_t comm; 
    struct termios termAttr; 
    struct sigaction saio; 
    
    int main(void){ 
    
    #ifdefined(__linux__) 
    staticconstchar*const device="/dev/ttyS0"; 
    #elif defined(_WIN32) 
    staticconstchar*constdevice="COM1"; 
    #endif 
    
    printf("== Before test process : ==\n");
    printf("1. Please check this device "%s" is available.\n",device); printf("2. Be sure that you had connect the RS-232 port to a loop back " "connector.\n");
     printf("\n"); 
    
    rs232_init(&comm); 
    
    bool open_result=rs232_open(&comm,device); 
    printf("Open device "%s" : %s\n",device, (open_result)?("Succeed"):("Failed")); 
    if (!open_result)
     return 1; 
    
    saio.sa_handler=serial_callback;
    saio.sa_flags=0; 
    saio.sa_restorer=NULL;
    sigaction(SIGIO,&saio,NULL); 
    
    fcntl(open_result,F_SETFL,FNDELAY); 
    fcntl(open_result,F_SETOWN,getpid()); 
    fcntl(open_result,F_SETFL,O_ASYNC); 
    
    
    //-Flush
     sleep_awhile(); 
    assert(rs232_flush(&comm)); 
    printf("Flushed.\n"); 
    
    //-Sendmessage 
    static const char message[]="RS-232 test message"; 
    assert(sizeof(message)==rs232_send(&comm,message,sizeof(message))); 
    printf("Sent.\n"); 
    
    sleep_awhile(); 
    
    rs232_deinit(&comm); 
    return0;
     } 
    
    void serial_callback(int__attribute__((unused))status){
    char buffer[1024]={0};
     int  recsz=rs232_receive(&comm,buffer,sizeof(buffer));
     printf("Received size : %d\n",recsz);
    printf("Response message : [%s]\n",buffer);
    }
    Last edited by isan; 01-25-2022 at 06:30 AM.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    147
    What doesn't work?
    Do you have compile errors, linker errors, crashes....

    This trailing # looks suspicious
    #ifdef__linux__ #

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    5
    rs232 not receive data
    not I don't have compile errors, linker errors, crashes....

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,324
    The first thing you need to do is edit your post and then paste the code "text only".
    The horrible colour scheme from your IDE conflicts with the board and reduces your code to mush.

    Or just reply with another post with correctly formatted code and I'll fix it.
    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.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,623
    First thing are you sure that you are connected to "/dev/ttyYs0", "COM1"?

    Are you sure that the device connected to your com port is trying to contaminate at the same baud, parity, etc as your program expects?

    Have you tried to use a com program (minicom, putty) to connect to your remote device? Does it work?

  6. #6
    Registered User
    Join Date
    Feb 2018
    Posts
    5
    Quote Originally Posted by jimblumberg View Post
    First thing are you sure that you are connected to "/dev/ttyYs0", "COM1"?

    Are you sure that the device connected to your com port is trying to contaminate at the same baud, parity, etc as your program expects?

    Have you tried to use a com program (minicom, putty) to connect to your remote device? Does it work?

    Yes I'm sure / dev / ttys0 is connected
    It works when I send and receive data like the following code
    But it does not work when I add interrupt configuration and call back function



    Code:
    assert( sizeof(message) == rs232_send(&comm, message, sizeof(message)) );
    printf("Sent.\n");
     // - Receive message
    sleep_awhile();
    char buffer[1024] = {0};
    int  recsz = rs232_receive(&comm, buffer, sizeof(buffer));
    printf("Received size : %d\n", recsz);
    printf("Response message : [%s]\n", buffer);
     // - Check response
    bool echo_result = ( recsz == sizeof(message) )&&( !memcmp(buffer, message, recsz) );
    printf("Response check : %s\n", (echo_result ? "Succeed." : "FAILED!"));
    Last edited by Salem; 01-26-2022 at 06:00 AM. Reason: Removed crayola table tag horror

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    875
    Can you show the code that uses interrupts? What platform is that for where you can use interrupts directly anyway? From what I can see, this library is only for Linux and Windows, and in those systems you don't have direct access to interrupts (except in low-level driver code).

  8. #8
    Registered User
    Join Date
    Feb 2018
    Posts
    5
    Quote Originally Posted by christop View Post
    Can you show the code that uses interrupts? What platform is that for where you can use interrupts directly anyway? From what I can see, this library is only for Linux and Windows, and in those systems you don't have direct access to interrupts (except in low-level driver code).

    I got the interruption idea with sigaction from this page: Serial Programming HOWTO

  9. #9
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    Would you mind giving more details about the hardware in question, and about the use case? Most PCs since about 2005 no longer have an RS232 port to use, and similarly most peripherals today use USB for serial communication instead. Aside from that, the specific goal may be relevant in determining what the serial connection is actually doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with RS232
    By evverton in forum C Programming
    Replies: 4
    Last Post: 03-25-2009, 06:12 PM
  2. Read and display signel serial mouse RS232 in C
    By zebres in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 09:09 AM
  3. RS232/DB9 in C
    By minhdung_hoang in forum C Programming
    Replies: 4
    Last Post: 01-19-2009, 05:25 PM
  4. Rs232
    By lostinspace in forum C Programming
    Replies: 1
    Last Post: 06-12-2008, 05:13 PM
  5. rs232 with win api
    By alaturka in forum C Programming
    Replies: 14
    Last Post: 12-27-2005, 10:24 PM

Tags for this Thread