1

I want to obtain the weight of a scale through the serial port but I have this error

Error: System.IO.IOException: Uno de los dispositivos conectados al sistema no funciona.
en System.IO.Ports.InternalResources WintErrorent32 errorCode, String str
en System.IO.Ports InternalResources WinIOError
en System.IO.Ports SerialStream.InitializeDCB(Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Boolean discardNull
en System.IO.Ports. SeriatStream.ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull. Byte parityReplace) en System.IO.Ports.SerialPort Open
en puertoSerial.Form1.btnAccionPuerto Click(Object sender, EventArgs e) en
C\Users\Huawei Desktop puertoSerial puertoSerial Form1.cs:linea 43
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace puertoSerial {
    public partial class Form1 : Form {
        private bool conected = false;
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            string[] puertosDisponibles = SerialPort.GetPortNames();
            cbPuertos.Items.Clear();
            foreach (string puerto in puertosDisponibles) {
                cbPuertos.Items.Add(puerto);
            }
        }

        private void btnAccionPuerto_Click(object sender, EventArgs e) {
            if (!conected) {
                spPuertos.PortName = cbPuertos.Text;
                spPuertos.BaudRate = Int32.Parse(cbBaudRate.Text);
                spPuertos.DataBits = 8;
                spPuertos.Parity = Parity.None;
                spPuertos.StopBits = StopBits.One;
                spPuertos.Handshake = Handshake.None;
                try {
                    spPuertos.Open();
                    conected = true;
                    btnAccionPuerto.Text = "DESCONECTAR";
                } catch(Exception er) {
                    MessageBox.Show("Error: "+ er);
                }
            }
            else {
                spPuertos.Close();
                conected= false;
                btnAccionPuerto.Text = "CONECTAR";
            }
        }

        private void spPuertos_DataReceived(object sender, SerialDataReceivedEventArgs e) {
            string data_in = spPuertos.ReadExisting();
            MessageBox.Show(data_in);
        }
    }
}
karel
  • 5,489
  • 46
  • 45
  • 50
Dam Dev
  • 11
  • 1
  • 1
    _I want to obtain the weight of a scale through the serial port but I have this error_: Unfortunately,, this is too vague. You need to start by reading the documentation for your device. Then, ensure that you've properly configured the device for serial port communication including installing any necessary drivers. Then read all of the documentation for the [SerialPort Class](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport?view=dotnet-plat-ext-7.0) and ensure that you set the property values to match those on the device. – Tu deschizi eu inchid Jun 03 '23 at 03:01
  • There are some PowerShell commands towards the bottom of this [post](https://stackoverflow.com/a/65971845/10024425) that may be helpful. – Tu deschizi eu inchid Jun 03 '23 at 16:12
  • Is your problem solved? – Ihdina Jun 04 '23 at 14:14

1 Answers1

0

Make sure you have installed the driver for the serial port device and the seial port device is visible in the device manager when you plug that into your computer.

Try like this:

try
{

    if (spPuertos == null) {
        // Reinstance SerialPort if null reference
        spPuertos = new SerialPort();
    }

    spPuertos.PortName = cbPuertos.Text;
    spPuertos.BaudRate = Int32.Parse(cbBaudRate.Text);
    spPuertos.DataBits = 8;
    spPuertos.Parity = Parity.None;
    spPuertos.StopBits = StopBits.One;
    spPuertos.Handshake = Handshake.None;


    // Close SerialPort if opened
    if(spPuertos.IsOpen) 
    {
        spPuertos.Close();
    }
    
    // Open SerialPort
    spPuertos.Open();

} catch(Exception er)
{
    // Dispose SerialPort
    spPuertos.Dispose();
    
    // Null reference
    spPuertos = null
    
    MessageBox.Show("Error: " + er.Message);
}
Ihdina
  • 950
  • 6
  • 21