Answer the question
In order to leave comments, you need to log in
How to solve serial port data conversion error?
I receive data from the COM port. The value comes from 0 to 100.
When I try to convert to int, an error pops up (screen is attached).
The code itself:
string inpSerial = serialPort1.ReadExisting();
label1.Text = inpSerial;
int numm = int.Parse(inpSerial);
progressBar1.Value = numm;
serialPort1.ReadLine();
, and not through serialPort1.ReadExisting();
, then the error does not appear, but this option does not suit me because performance drops.
Answer the question
In order to leave comments, you need to log in
What happens inpSerial
when an error occurs? Obviously not a number.
bool successfullyParsed = int.TryParse(inpSerial, out num);
if (successfullyParsed){
// ...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question