к;давайте попробуем исправить это;проблема в том, что данные не всегда поступают аккуратно и аккуратно.В вашем случае вы хотите обрабатывать 4 байта за раз, поэтому: давайте попробуем это:
byte[] buffer = new byte[4];
int bytesBuffered = 0;
bool inProgress = false;
private void timer1_Tick(object sender, EventArgs e)
{
if (!inProgress) {
// presumably this means "send me the current data?"
inProgress = true;
port.Write("A");
}
// read whatever we still need to make 4, if available
int bytes = Math.Min(port.BytesToRead, 4 - bytesBuffered);
if (bytes <= 0) return; // nothing to do right now
// read the next few bytes, noting the offset
bytes = port.Read(buffer, bytesBuffered, bytes);
// TODO: check if bytes is <= 0 - if so, the port may have disconnected
bytesBuffered += bytes;
// check whether we have enough to update the UI
if (bytesBuffered == 4)
{
// we now have 4 bytes; update the UI, and reset
int potensio = BitConverter.ToInt32(buffer, 0);
string potString = Convert.ToString(potensio);
label1.Text = potString;
// and issue a new A next time
bytesBuffered = 0;
inProgress = false;
}
}
Примечание: если вы получите странные результаты, возможно, вы не учли "порядковый номер байтов"- 4-байтовое целое число может быть закодировано как «big endian» или «little endian», а ваш процессор может быть «big endian» или «little endian» - эти два значения должны совпадать.Если результаты выглядят безумно большими (или безумно отрицательными), попробуйте добавить Array.Reverse(buffer)
перед вызовом ToInt32
, предпочтительно после проверки BitConverter.IsLittleEndian
;то есть
// we now have 4 bytes; update the UI, and reset
if (BitConverter.IsLittleEndian) Array.Reverse(buffer); // fix endianness
int potensio = BitConverter.ToInt32(buffer, 0);
, и если это дает неправильный результат: поменяйте местами!
// we now have 4 bytes; update the UI, and reset
if (!BitConverter.IsLittleEndian) Array.Reverse(buffer); // fix endianness
int potensio = BitConverter.ToInt32(buffer, 0);