pos機上的串口配置

1.打開並初始化串口

int openRs232(int uibuad)
{
  struct Opn_Blk com_parm;
  int format;
  int iTermType;
  
  format = 0;
  
  closeRs232();
  iTermType = MmiUtil_GetTerminalType();
  
  if(iTermType == _VX670 || iTermType == _VX680 || iTermType == _VX680C)
  {
    if((RS232DEV = open("/dev/com6", O_RDWR)) < 0)
    {
      if((RS232DEV = open("/dev/com1", O_RDWR)) < 0)
      {
        disp_msg2("打開串口失敗");
        beeper_wait(500, 3);
        return(-1);
      }
    }
  }
  else if(iTermType == _VX805)
  {
    if((RS232DEV = open("/dev/com2", O_RDWR)) < 0)
    {
      disp_msg2("打開串口失敗");
      beeper_wait(500, 3);
      return(-1);
    }
  }
  else
  {
    if((RS232DEV = open("/dev/com1", O_RDWR)) < 0)
    {
      disp_msg2("打開串口失敗");
      beeper_wait(500, 3);
      return(-1);
    }
  }
  
  switch(uibuad)
  {
    case 1:
      com_parm.rate = Rt_1200;
      break;
    case 2:
      com_parm.rate = Rt_4800;
      break;
    case 3:
      com_parm.rate = Rt_9600;
      break;
    case 4:
      com_parm.rate = Rt_19200;
      break;
    case 5:
      com_parm.rate = Rt_115200;
      break;
    case 0:
    default:
      com_parm.rate = Rt_9600;
      break;
  }
  
  switch(format)
  {
    case 1:
      com_parm.format = Fmt_A7N1;
      break;
    case 2:
      com_parm.format = Fmt_A7E1;
      break;
    case 0:
    default:
      //com_parm.format = Fmt_A8N1| Fmt_auto | Fmt_RTS;
      com_parm.format = Fmt_A8N1;
      break;
  }
  
  com_parm.protocol = P_char_mode;
  com_parm.parameter = 0;
  set_opn_blk(RS232DEV, &com_parm);
  
  return(0);
}


2.發送數據

int Send232PosPacket(char *packet, int  len)
{
  char buf[6];
  int i;
  
  while(read(RS232DEV, buf, 6) > 0) ;
  
  buf[0] = STX;
  buf[1] = (unsigned char)(((((len / 1000) % 10) << 4) & 0xf0) | (((len / 100) % 10) & 0x0f));
  buf[2] = (unsigned char)(((((len / 10) % 10) << 4) & 0xf0) | ((len % 10) & 0x0f));
  buf[3] = ETX;
  buf[4] = ETX;
  buf[5] = '\r';
  
  for(i = 0; i < len; i++)
    buf[4] ^= packet[i];
    
  buf[4] ^= buf[1];
  buf[4] ^= buf[2];
  
  if(write(RS232DEV, buf, 3) != 3) return(-1);
  
  if(write(RS232DEV, packet, len) != len) return(-1);
  
  if(write(RS232DEV, buf + 3, 2) != 2) return(-1);
  
  return(len);
}


3.接收數據

int Rec232HostPacket(char *packet)
{
  int len, resp, k, delay_times = 100;
  long i, j;
  
  char buf[6];
  
  j = 10 * 50L;
  i = 0L;
  
 // while(1)
  {
  
    do
    {
      resp = read(RS232DEV, buf, 1);

	  if(resp == 0){
		continue;
	  }
      else if(resp == 1)
	  	break;
    }
    while((!(buf[0] == STX && resp == 1)));
    
    if(buf[0] != STX || resp != 1)
    {
      return(-1);
    }
    
    k = 0;
    
    do
    {
      resp = read(RS232DEV, &buf[1 + k], 2 - k);
      k += resp;
     }
    while((k < 2) && i++ < j);
    
    len = ((buf[1] & 0xf0) >> 4) * 1000 + (buf[1] & 0x0f) * 100
          + ((buf[2] & 0xf0) >> 4) * 10 + (buf[2] & 0x0f);
          
    if(k != 2 || len > TRANSMIT_BUFFER_LEN)
    {
      return(-1);
    }
    
    k = 0;
    
    do
    {
      resp = read(RS232DEV, packet + k, len - k);
      k += resp;
      
    }
    while((k < len));
    
    if(k != len)
    {
      return(-1);
    }
    
    k = 0;
    
    do
    {
      resp = read(RS232DEV, &buf[3 + k], 2 - k);
      k += resp;
    }
    while((k < 2));
    
    if(k != 2)
    {
      return(-1);
    }
    
    if(buf[3] != ETX)
    {
      return(-1);
    }
    
    buf[5] = ETX;
    
    for(resp = 0; resp < len; resp ++)
      buf[5] ^= packet[resp];
      
    buf[5] ^= buf[1];
    buf[5] ^= buf[2];
    
    if(buf[5] != buf[4])
    {
      return(-1);
    }
    
 //   if(packet[0] != 0x68)
     // break;
  }
  
  return(len);
}


4.關閉串口

void closeRs232()
{
  if(RS232DEV > 0)
  {
    close(RS232DEV);
    RS232DEV = 0;
  }
}


5.暫時不清楚做什麼的

int rs232Recv(char *rbuf, int size, int timeout)
{
  int sz, i, tries = 0;
  char c;
  long timeouts;
  
  do
  {
    timeouts = set_itimeout(-1, timeout, TM_SECONDS);
    i = 0;
    sz = size;  // max
    
    while(CHK_TIMEOUT(-1, timeouts) && i < sz)
    {
    
      if(act_kbd_pending_test(KEY_CANCEL))
        return -1;
        
      if(read(RS232DEV, &c, 1) == 1)    // got
      {
        if(i == 0)
        {
          if(c != STX && c != SI) continue;
        }
        
        rbuf[i++] = c;
        
        if(c == ETX || c == SO)
        {
          sz = i + 1; // real
        }
      }
    }
    
    if(i == 0 || i != sz)  // Wher LRC char is received, i equals to sz.
      return(-1);
      
    if(SVC_CRC_CALC(0, &rbuf[1], sz - 1) == 0)
      c = ACK;
    else
      c = NAK;  // NAK if LRC error
      
    write(RS232DEV, &c, 1);
    SVC_WAIT(200);
  }
  while(c == NAK && tries++ < 2);
  
  while(read(RS232DEV, &c, 1) > 0) SVC_WAIT(50);  // read off EOT if exist
  
  return(sz);
}


頭文件

#ifndef RS232_H
#define RS232_H

#define TRANSMIT_BUFFER_LEN   1000

#ifndef SI
#define SI      0xf
#endif

#ifndef SO
#define SO      0xe
#endif

#ifndef STX
#define STX     0x02
#endif

#ifndef ETX
#define ETX     0x03
#endif

extern int openRs232(int uibuad);
extern void closeRs232(void);
extern void cleanRs232(void);
extern int Send232PosPacket(char *packet, int  len);
extern int Rec232HostPacket(char *packet);
extern int rs232Recv(char *rbuf, int size, int timeout);
#endif

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章