I have this code, what I want to do is constantly obtain the weight of a scale, this through the C# serial port, but it gives me an error when I use a serial to usb converter, the error it gives me is from a device connected to the system that It doesn't work well and when I try without the converter it connects to the serial port but the weight of the scale doesn't attract me.
private SerialPort serialPort;
public MainForm()
{
InitializeComponent();
serialPort = new SerialPort();
}
private void MainForm_Load(object sender, EventArgs e)
{
serialPort.PortName = "COM1";
serialPort.BaudRate = 9600;
serialPort.DataBits = 8;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.DataReceived += SerialPort_DataReceived;
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = serialPort.ReadLine();
decimal weight = decimal.Parse(data);
numericUpDownWeight.Invoke((MethodInvoker)(() => numericUpDownWeight.Value = weight));
}
private void btnStart_Click(object sender, EventArgs e)
{
if (!serialPort.IsOpen)
serialPort.Open();
}
private void btnStop_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen)
serialPort.Close();
}