pos机上的串口配置

来源:互联网 发布:贾雷德·戴蒙德 知乎 编辑:程序博客网 时间:2024/05/09 01:28

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#endifextern 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

 

原创粉丝点击