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);
}