シリアルポートに 0xFF を送信します
結果として 0x3F が表示されます。
他のすべてのバイトは適切です。
状況はこんな感じ…
外部ボックスはこれらのバイトを PC に送信します...
0xFF、0x0D、0x00、0x30、0x31、0x53、0x55、0x43、0x43、0x45、0x53、0x53
C#はバッファでこれを生成します...
0x3F、0x0D、0x00、0x30、0x31、0x53、0x55、0x43、0x43、0x45、0x53、0x53
最初のバイトの最初の 2 ビットが欠落しています。誰もこれを見たことがありますか?
何が起こっているのか、さらに重要な理由、そして最も重要な修正方法を説明する投稿がここにある場合は、それを指摘してください。ありがとうございました。
コードは次のとおりです。StackOverFlow システムについて理解できたと思います。このコミュニティと私の 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;
}
}
}