简体   繁体   English

0xFF变为0x3F

[英]0xFF becomes 0x3F

I send 0xFF to the serial port 我将0xFF发送到串口

I see 0x3F as a result. 结果我看到了0x3F。

All other bytes are proper. 所有其他字节都是正确的。

The situation is like this... 情况就是这样......

External box sends these bytes to the PC... 外部框将这些字节发送到PC ...

0xFF, 0x0D, 0x00, 0x30, 0x31, 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53 0xFF,0x0D,0x00,0x30,0x31,0x53,0x55,0x43,0x43,0x45,0x53,0x53

C# produces this in the buffers... C#在缓冲区中生成这个...

0x3F, 0x0D, 0x00, 0x30, 0x31, 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53 0x3F,0x0D,0x00,0x30,0x31,0x53,0x55,0x43,0x43,0x45,0x53,0x53

The first byte is missing the first two bits. 第一个字节缺少前两位。 Has anyone seen this before ? 谁看过这个吗 ?

If there is a post here which explains what's going on, and more importantly why, and even most importantly, how to fix it, please point me to it. 如果这里有一篇文章解释了发生了什么,更重要的是为什么,甚至最重要的是如何解决它,请指出我。 Thank you. 谢谢。

Here's the code; 这是代码; I hope that I've figured out the StackOverFlow system; 我希望我已经找到了StackOverFlow系统; still a new comer to both this community and my C# knowledge is about 1 or 2 months old. 对于这个社区和我的C#知识来说仍然是一个新来者,大概是1或2个月大。

 public static void OurBackGroundSerialPortReceiver(object sender, SerialDataReceivedEventArgs e )
    {
                                                                //// Item 1, tell the world, particularly the
                                                                //// chief dispatch routin, that we are receiving

        aUartSemaphoreThatTells.WhatTheUartBackgroundRxIsDoing = (int)aValueWhichIndicatesThat.theUARTisReceivingData;

        SerialPort CurrentPort = (SerialPort)sender;            //// Int routine gave is this in the arguments

        int LastByteInUartBuffer = CurrentPort.ReadBufferSize;
        int TheLastByteTheBoxSent = CurrentPort.BytesToRead;
        string inputData = CurrentPort.ReadExisting();          //// This is a C# property method of ports

        int Dest;
        Dest = UartPlaceHolders.RxBufferLeader;                 //// Will index into buffer for Chief dispatch

        int Source;                                             //// Will index into Uart buffer to fish it out
        Source = 0;                                             //// therefore, we start at zero

        int TopEdge;                                            //// We'll calculate this here once instead of in the loops below
        TopEdge = (int)TheSizeOf.OneSecondsWorthOfData;         //// This will tell us when to wrap around


        if (Dest < UartPlaceHolders.RxBufferTrailer)            //// Half the time we'll have wrap-around
        {                                                       //// If that's the case, then the trailer > the leader
            while (
                  (Dest < UartPlaceHolders.RxBufferTrailer)     //// If we are wrapped, make sure we don't
                   &&                                           //// overtake either the trailer or
                   (Dest < TopEdge)                             //// go over the top edge
                   &&                                           //// At the same time, make sure that
                   (Source <= LastByteInUartBuffer)             //// we don't fish out more than is there
                   )
            {
                UartData.TheImmediateSecondOfData[Dest] = (byte)inputData[Source];      //// Move bytes into buff for chief
                Dest = Dest + 1;
                Source = Source + 1;
            }

            if (Source >= LastByteInUartBuffer)                 //// Have we done all the bytes for this event ?            
            {                                                   //// Yes, therefore we will update the leader
                UartPlaceHolders.RxBufferLeader = Dest;         //// This tells us where to start next time

                aUartSemaphoreThatTells.WhatTheUartBackgroundRxIsDoing = (int)aValueWhichIndicatesThat.WeHaveReceivedSomeData;

                return;                                         //// and we are done
            }
                                                                //// //  //   Else no, more bytes so...
            else if (Dest >= TopEdge)                           //// Did we wrap around ?
            {                                                   //// Yes, so
                Dest = 0;                                       //// wrap around to the start
                while (                                         //// Now we do the same thing again
                      Dest < UartPlaceHolders.RxBufferTrailer   //// C# and windows keep buffers at 4K max, 
                      &&                                        //// so we will wrap only once
                      Source < LastByteInUartBuffer             //// May not even need that other test
                      )                                         //// This will finish the rest of the bytes
                {
                    UartData.TheImmediateSecondOfData[Dest] = (byte)inputData[Source];      //// There they go
                    Dest = Dest + 1;
                    Source = Source + 1;
                }

                UartPlaceHolders.RxBufferLeader = Dest;         //// This tells us where to start next time
                return;
            }

            else                                             //// Dest is neither <, >, nor =, we have logic error
            {
                ErrorFlags.SerialPortErrorDescription = (int)AnError.ExistsInTheSerialPortBufferPointers;
                return;
            }

        }

        if (Dest >= UartPlaceHolders.RxBufferTrailer)           //// Now, if the Trailer is ahead of the leader, here we go
        {                                                       //// If that's the case, then the trailer > the leader
            while (
                //(Dest < UartPlaceHolders.RxBufferTrailer)     //// This is the first major difference twixt this time & previous...
                // &&                                           //// ...because This condition is defacto guarateed to be false now
                   (Dest < TopEdge)                             //// We still want to stop before we hit the top edge
                   &&                                           //// At the same time, make sure that we go past...
                   (Source < LastByteInUartBuffer)              //// ...the last byte the Uart gave us
                   &&
                   (Source < TheLastByteTheBoxSent)
                   )
            {
                UartData.TheImmediateSecondOfData[Dest] = (byte)inputData[Source];      //// Move bytes into buff for chief
                Dest = Dest + 1;
                Source = Source + 1;
            }

            if (Source >= LastByteInUartBuffer)                 //// Have we done all the bytes for this event ?
            {                                                   //// Yes, therefore we will update the leader
                UartPlaceHolders.RxBufferLeader = Dest;         //// This tells us where to start next time
                return;                                         //// and we are done
            }                                                   //// //   Else no, we have more bytes to move, so...
            else if (Dest >= TopEdge)                           //// Did we wrap around ?
            {                                                   //// Yes, so...
                Dest = 0;                                       //// wrap around to the start
                while (                                         //// Now we do the same thing again
                      Dest < UartPlaceHolders.RxBufferTrailer   //// C# and windows keep buffers at 4K max, 
                      &&                                        //// so we will wrap only once
                      Source < LastByteInUartBuffer
                      )
                {
                    UartData.TheImmediateSecondOfData[Dest] = (byte)inputData[Source];
                    Dest = Dest + 1;
                    Source = Source + 1;
                }

                UartPlaceHolders.RxBufferLeader = Dest;         //// This tells us where to start next time

                aUartSemaphoreThatTells.WhatTheUartBackgroundRxIsDoing = (int)aValueWhichIndicatesThat.WeHaveReceivedSomeData;

                return;
            }

            else                                                //// Dest is neither <, >, nor =, we have logic error
            {
                ErrorFlags.SerialPortErrorDescription = (int)AnError.ExistsInTheSerialPortBufferPointers;
                return;
            }

        }

    }

Here's where the answer was found... 这是找到答案的地方......

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx

Scroll down to "Read" and it will detail the method used to do this, found here... 向下滚动到“阅读”,它将详细说明用于执行此操作的方法,此处可找到...

http://msdn.microsoft.com/en-us/library/ms143549.aspx http://msdn.microsoft.com/en-us/library/ms143549.aspx

This is the code that fixed this problem for the moment (maybe for good) 这是解决这个问题的代码(也许是好的)

        SerialPort CurrentPort = (SerialPort)sender;                                //// caller gave us this in the arguments

        int TheNumberOfBytes = CurrentPort.BytesToRead;                             // The system will tell us how large our array should be

        byte[] inputData = new byte[TheNumberOfBytes];                              // Our array is not that large

        int WeReadThisMany = CurrentPort.Read(inputData, 0, TheNumberOfBytes);      //// This is a C# property method of ports

Hope I'm doing the proper thing by answering here. 希望我在这里回答你做的事情。

Anyway, the end result of this is that this method reads the real bytes, as bytes, that are sent to the Serial port by whatever was on the other side. 无论如何,最终的结果是这个方法读取实际字节,作为字节,通过另一方的任何东西发送到串行端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM