1,934 questions
Reading 256 byte data from serial port
Muzahidul Islam
0
Reputation points
hi there,
I am making a windows form to read 256 byte data from serial port. the data byte will be shown a combobox. if i sent maximum 32 byte data to serial port, it worked properly. but when it is more than 32 byte like 36 or 40 byte, error data is shown. I also declared serialPort1.ReadBufferSize = 1024;, but same problem happened. Please check the following function of data receiving:
please help to find out the issue. thank you.
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int bytesToRead = serialPort1.BytesToRead;
if (bytesToRead > 0)
{
byte[] buffer = new byte[bytesToRead];
serialPort1.Read(buffer, 0, bytesToRead);
// Clear the ComboBox before adding new data
if (cboboxs.InvokeRequired)
{
cboboxs.Invoke(new Action(() =>
{
cboboxs.Items.Clear();
}));
}
else
{
cboboxs.Items.Clear();
}
// Convert the buffer data to a string representation
string dataAsString = BitConverter.ToString(buffer);
// Add the string representation to the ComboBox
if (cboboxs.InvokeRequired)
{
cboboxs.Invoke(new Action(() =>
{
cboboxs.Items.Add(dataAsString);
cboboxs.SelectedIndex = cboboxs.Items.Count - 1; // Select the last item
}));
}
else
{
cboboxs.Items.Add(dataAsString);
cboboxs.SelectedIndex = cboboxs.Items.Count - 1; // Select the last item
}
}
}
```
Developer technologies | Windows Forms
Windows for business | Windows Client for IT Pros | User experience | Other
Microsoft Security | Microsoft Graph
13,726 questions
Developer technologies | C#
11,579 questions
Sign in to answer