【问题标题】:Problem with receiving data form serial port in c#?在 C# 中从串口接收数据的问题?
【发布时间】:2010-06-07 08:21:08
【问题描述】:

你好,我在 c# 中从串行端口接收数据时遇到问题,我在数据缓冲区的末尾插入了一个换行符。然后我在串口上发送这个数据缓冲区,之后我的 c# GUI 接收器将通过Readline() 函数获取这些数据,但它总是给我原始数据而不是实际数据如何解决这个问题。

//configuring the serial port this code in c# with problem
                serialPort.PortName = "COM1";
                serialPort.BaudRate = 9600;
                serialPort.DataBits = 8;
                serialPort.Parity = Parity.None;
                serialPort.StopBits = StopBits.One;

                //opening the serial port
                if(!serialPort.IsOpen)
                    serialPort.Open();

                //read 2byte data for msG code from serial port


                string strReadData=serialPort.ReadLine();
                char[] temp=new char[350];
                //strReadData.CopyTo(1, temp, 0, strReadData.Length - 2);

                //strReadData = temp.ToString();

                //string strReadData = serialPort.ReadExisting();

                //strReadData.Replace(' ', '\0');

                //strReadData.Replace(' ', '');

                byte[] RecievedData = Encoding.ASCII.GetBytes(strReadData);

                RecievedDataDecoder(RecievedData);


 //close the port
                if(serialPort.IsOpen)
                    serialPort.Close();

但是我的 c++ 接收器工作正常我不知道这里的问题是工作 c++ 代码

// variables used with the com port
BOOL     m_bPortReady;
HANDLE   m_hCom;
DCB      m_dcb;
COMMTIMEOUTS m_CommTimeouts;
BOOL     bWriteRC;
BOOL     bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;
DWORD dwCommEvent;
DWORD dwRead;

char       sBuffer[128];

m_hCom = CreateFile("Com1", 
        GENERIC_READ | GENERIC_WRITE,
        0, // exclusive access
        NULL, // no security
        OPEN_EXISTING,
        0, // no overlapped I/O
        NULL); // null template 

m_bPortReady = SetupComm(m_hCom, 128, 128); // set buffer sizes

m_bPortReady = GetCommState(m_hCom, &m_dcb);
m_dcb.BaudRate = 9600;
m_dcb.ByteSize = 8;
m_dcb.Parity = NOPARITY;
m_dcb.StopBits = ONESTOPBIT;
m_dcb.fAbortOnError = TRUE;

m_bPortReady = SetCommState(m_hCom, &m_dcb);

m_bPortReady = GetCommTimeouts (m_hCom, &m_CommTimeouts);

m_CommTimeouts.ReadIntervalTimeout = 50;
m_CommTimeouts.ReadTotalTimeoutConstant = 50;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 10;
m_CommTimeouts.WriteTotalTimeoutConstant = 50;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 10;

m_bPortReady = SetCommTimeouts (m_hCom, &m_CommTimeouts);


    if (!SetCommMask(m_hCom, EV_RXCHAR))
    {
        printf("Error in set comm mask");
    }

while(1)
{

   if (WaitCommEvent(m_hCom, &dwCommEvent, NULL)) 
   {
      if (ReadFile(m_hCom, &sBuffer, 128, &iBytesRead, NULL))
            printf("");

      else
      {
         printf("Error in reading");
         break;
      }
   }
   else
   {
    printf("Error in Waiting");
       break;
   }


    printf("%s",sBuffer);   
    strcpy(sBuffer,"");

}


CloseHandle(m_hCom);
getch();
exit(0);

【问题讨论】:

  • “原始”数据而不是“实际”数据是什么意思?
  • 我无法按照我在串口中写入的顺序接收数据,它是块而不是数据缓冲区的形式

标签: c# c++ c windows


【解决方案1】:

您的问题有点含糊,但在 Windows 上默认情况下,当端口接收到回车和换行字节组合时,ReadLine() 方法将返回。或者 \r\n,或者 0x0d 0x0a,如果你愿意的话。

如果您首先发送的“缓冲区”包含由 \r\n 分隔的多个消息,则 ReadLine() 将仅返回第一个,然后端口将在您发布的 C# 代码中关闭。

也许你的代码在循环中,但没有显示出来。

除此之外,在您收到任何数据之后,您会将其转换回由 ASCII 编码确定的字节数组。您是否首先发送ASCII?如果没有,您可能会丢失信息。

此外,如果您所做的只是将接收到的字符串转换为字节,那么您可以首先将数据作为字节接收。

我认为您需要更详细地解释一下您正在发送的缓冲区中的内容以及您收到的内容。

【讨论】:

  • 很高兴它有帮助。出了什么问题?
【解决方案2】:

我的大多数串行端口错误都是由错误的波特率设置引起的。这也可能是你的问题。 (您可以在 SerialPort 类的some constructors 中设置波特率,也可以在BaudRate property 中设置波特率)

【讨论】:

  • 两者具有相同的波特率和数据字节大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 2018-04-17
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 2021-12-27
相关资源
最近更新 更多