文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>串口通信comport.c

串口通信comport.c

时间:2010-06-07  来源:长江货郎

#include "comport.h" unsigned char           g_ucProcToken, g_ucCtrlZ; void syslog (int mode, char* msg, ...)
{
        va_list argptr;
        va_start (argptr, msg);
        vprintf (msg, argptr);
        va_end (argptr);
        printf ("\n");
}
void Usage ()
{
  syslog (0, "Usage: comport -d <device> [-b <baudrate>][-s <settings>] [-i<cmd>,<arg>][-h][-v]");
  syslog (0, "  -d   device name");
  syslog (0, "  -b   device baudrate (115200, 57600, 19200, 9600), default is 115200");
  syslog (0, "  -s   device settings (data bits, parity, stop bits, flow control), defaut is 8N1N");
  syslog (0, "       data bits: 8, 7");
  syslog (0, "       parity: N=None, O=Odd, E=Even, S=Space");
  syslog (0, "       stop bits: 1, 0");
  syslog (0, "       flow control: N=None, H=Hardware, S=Software, B=Both");
  syslog (0, "  -i   ioctl system call (cmd & arg only support int), used as \"-i 2,3\"");
  syslog (0, "  -h   this help info");
  syslog (0, "  -v   version number");
  exit(1);
}
void signal_handler (int i_sig)
{
 if (SIGTERM == i_sig || SIGINT == i_sig)
 {
  g_ucProcToken = 0x00;
 }
 else if (20 == i_sig)
 {
  g_ucCtrlZ = 0x01;
 }
}
int main(int argc, char **argv)
{
 int    oc = 0;
 COM_PORT   comport; 
 unsigned char   ucRet = 0x00;
    unsigned char   uc_getdev = 0x00;
 unsigned char   buf[512] = {0};
 unsigned char   dis_mode = 0x00; // Default as %c
 int    recv_bytes = 0, i=0;
 struct sigaction      sigact;
 
 if(argc < 3)
    {
  Usage();
    }
 memset(&comport, 0, sizeof(COM_PORT));
 init_comport(&comport);
 while( (oc=getopt(argc, argv, "d:b:s:i:hv")) != -1)
 {
  switch(oc)
  {
     case 'd':
   strncpy(comport.dev_name, optarg, BUF_64);
   dbg_print("Get device name:\t\t\"%s\"\n", comport.dev_name);
   uc_getdev = 0x01;
   break;
     case 'b':
   comport.baudrate = atol(optarg);
   dbg_print("Set BaudRate as:\t\t\"%ld\"\n", comport.baudrate);
   break;
     case 's':
   set_setting(optarg, &comport);
   break;
     case 'h':
   Usage();
   break;
     case 'i':
     case 'v':
     default:
   break;
  }
 }
 if(0x00 == uc_getdev)
  Usage();
 g_ucProcToken = 0x01;
 g_ucCtrlZ = 0x00;
 disp_setting(&comport); // Only enable "DEBUG" will outprint it.
 ucRet = comport_open(&comport);
 if( 0x00 != ucRet )
 {
  syslog (0, "failed to open %s. RetCode [0x%02X]", comport.dev_name, ucRet); 
  goto CleanUp;
 }
 dbg_print("\"%s\" opened.\n", comport.dev_name);
 // Process level signal handler
 sigemptyset (&sigact.sa_mask);
 sigact.sa_flags = 0;
 sigact.sa_handler = signal_handler;
 
 sigaction (SIGTERM, &sigact, NULL); // catch terminate signal
 sigaction (SIGINT, &sigact, NULL);  // catch interrupt signal
 sigaction (SIGSEGV, &sigact, NULL); // catch segmentation faults
 sigaction (SIGTSTP, &sigact, NULL);     // catch ctrl+Z
 sigaction (SIGSTOP, &sigact, NULL); // catch ctrl+Z
 nonblock();
 while (0x01 == g_ucProcToken)
 {
  ucRet = comport_recv (&comport, buf, sizeof (buf)-1, &recv_bytes, 10); 
  if (0x00 == ucRet && 0 < recv_bytes)
  {
   for(i=0; i<recv_bytes; i++) 
   {
       if (0x01 == dis_mode)  
        printf ("%02X ", buf[i]);
       else
        printf ("%c", buf[i]);
   }
   fflush(stdout);
  }
  if(0 != kbhit ())
  {
   recv_bytes = fgetc (stdin);
   if (0x0a == recv_bytes) // NL
    buf[0] = 0x0d; // CR
   else
    buf[0] = recv_bytes;
   comport_send (&comport, buf, 1);
  }
  else if (0x00 != g_ucCtrlZ)
  {
   g_ucCtrlZ = 0x00; 
   buf [0] = 0x1A;
   comport_send (&comport, buf, 1);
  }
 }
 printf("Close comport and exit now.\n");
 comport_close (&comport);
 
CleanUp:
 comport_term (&comport);
 return 0;
}
相关阅读 更多 +
排行榜 更多 +
鸡生化精英安卓版

鸡生化精英安卓版

飞行射击 下载
光头火柴人安卓版

光头火柴人安卓版

飞行射击 下载
轨道射击安卓版

轨道射击安卓版

飞行射击 下载