Thread: Read RS232 with interrupt

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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