串行通信使用传入的ASCII DC1字符

时间:2012-12-30 05:41:50

标签: binary embedded serial-port

我正在实现一个串行接口,使用/ dev / ttyS0与外部硬件进行通信。我已配置RAW输入和输出,因为数据流采用数据包格式,并使用消息开头,长度字节和二进制数据进行封装。传入数据流中的任何DC1或DC3字符(0x11或0x13)都被Linux操作系统(最新的Ubuntu)吃掉。

我有一台Windows PC和Realterm监控通信,并在显示器上看到0x11和0x13字符。据我所知,没有其他缺失的角色。

这是我的串行设置代码(注意IXON,IXOFF,IXANY被禁用):

void Ser_Open_Port( unsigned char *port )
{
   tcgetattr(ser_handle, &ser_old_settings);
   tcgetattr(ser_handle, &ser_new_settings);

   // Set Baud Rate
   cfsetospeed(&ser_new_settings, baud); 

   // ~BRKINT - No Flush on Break
   // ~PARMRK, ~IGNPAR - Parity Error result in \0 char
   // ~ISTRIP - No 8th bit Strip
   // ~INLCR - No NL to CR Translate
   // ~IGNCR No Ignore CR
   // ~ICRNL No CR to NL Translate
   // ~IXON - No Software Flow Control on Output
   // ~IXOFF - No Software Flow Control on Output
   // ~IXANY - No Software Flow Control on Output
   // ~INPCK - Ignore Parity Checking
   ser_new_settings.c_iflag &= ~(BRKINT|PARMRK|IGNPAR|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|INPCK);
   // IGNBRK - Ignore Break
   ser_new_settings.c_iflag |= (IGNBRK); 

   // ~OPOST - Implementation Defined Output Processing Off - Raw Output
   // All Other c_oflag bits are ignored
   ser_new_settings.c_oflag &= ~(OPOST);

   // ~ICANON - Not Canonical Input Mode
   // ~ECHO - No Echo
   // ~ECHOE - No Echo Erase
   // ~ECHONL - No Echo NL
   // ~ISIG - No Signals
   // ~IEXTEN - Implementation Defined Input Processing Off - Raw Input
   ser_new_settings.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHONL|ISIG|IEXTEN);

   // ~PARENB - No Parity
   // ~CSTOPB - 1 stop bit 
   // ~CSIZE - Clear Character Size Bits
   ser_new_settings.c_cflag &= ~(PARENB|CSTOPB|CSIZE);
   // CS8 - Character Size 8 bits
   // CLOCAL -  Ignore Modem Control Lines
   ser_new_settings.c_cflag |= (CS8|CLOCAL); 

   // apply the settings
   tcsetattr(ser_handle, TCSANOW, &ser_new_settings);
   tcflush(ser_handle, TCOFLUSH);

   // Open Serial Port for Read/Write, and Non Blocking
   ser_handle = open(port, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
}

这是我从串行端口缓冲区读取的地方:

   ser_charin_count = read( ser_handle, ser_charin, 30);
   if( ser_charin_count > 0 && ser_charin_count != 0xFFFFFFFF)
   {
   // Code to Process Message Bytes Here
   }

感谢您的协助。

1 个答案:

答案 0 :(得分:2)

错误在于没有首先打开端口以使ser_handle初始化为正确的端口attr结构。我将open打开在子例程的顶部,然后使用tcgetattr和tcsetattr函数来正确修改端口值。