List for current settable baud rate on serialport c#

-2

I'm currently working on a weighbridge application that reads the weight in from a weight monitor. I can choose and change baud rates for the differences that might occur from monitor models and I save the weight read from the serial port into a database with relevant information. What I am wondering is if there is any way to programmatically pick or test the set baud rate of the currently connected monitor til it gives a meaningful response.

The case has presented itself since the user might not know the baud rate of the monitor and would cause me to go and test every possibility for all users.

Bellow is a snippet of code I am currently using to get supported baud rates of the device. which I got from here and it works great. Credit due there.

private int UpdateBaudRateCollection()
    {
        SerialPort mySerialPort = new SerialPort();
        mySerialPort.PortName = ddComPorts1.selectedValue.ToString();
        mySerialPort.Open();
        object p = mySerialPort.BaseStream.GetType().GetField("commProp", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(mySerialPort.BaseStream);
        int dwSettableBaud = (int)p.GetType().GetField("dwSettableBaud", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(p);
        mySerialPort.Close();
        //MessageBox.Show(dwSettableBaud.ToString("X"));
        return dwSettableBaud;
    }

    private void SettableBaudRateOfDevice(int settableBaudRate)
    {
        const int BAUD_075 = 0x00000001;
        const int BAUD_110 = 0x00000002;
        const int BAUD_150 = 0x00000008;
        const int BAUD_300 = 0x00000010;
        const int BAUD_600 = 0x00000020;
        const int BAUD_1200 = 0x00000040;
        const int BAUD_1800 = 0x00000080;
        const int BAUD_2400 = 0x00000100;
        const int BAUD_4800 = 0x00000200;
        const int BAUD_7200 = 0x00000400;
        const int BAUD_9600 = 0x00000800;
        const int BAUD_14400 = 0x00001000;
        const int BAUD_19200 = 0x00002000;
        const int BAUD_38400 = 0x00004000;
        const int BAUD_56K = 0x00008000;
        const int BAUD_57600 = 0x00040000;
        const int BAUD_115200 = 0x00020000;
        const int BAUD_128K = 0x00010000;
        const int BAUD_USER = 0x10000000;

        ddBaudRate1.Items.Clear();

        if ((settableBaudRate & BAUD_075) > 0)
            ddBaudRate1.Items.Add(75);
        if ((settableBaudRate & BAUD_110) > 0)
            ddBaudRate1.Items.Add(110);
        if ((settableBaudRate & BAUD_150) > 0)
            ddBaudRate1.Items.Add(150);
        if ((settableBaudRate & BAUD_300) > 0)
            ddBaudRate1.Items.Add(300);
        if ((settableBaudRate & BAUD_600) > 0)
            ddBaudRate1.Items.Add(600);
        if ((settableBaudRate & BAUD_1200) > 0)
            ddBaudRate1.Items.Add(1200);
        if ((settableBaudRate & BAUD_1800) > 0)
            ddBaudRate1.Items.Add(1800);
        if ((settableBaudRate & BAUD_2400) > 0)
            ddBaudRate1.Items.Add(2400);
        if ((settableBaudRate & BAUD_4800) > 0)
            ddBaudRate1.Items.Add(4800);
        if ((settableBaudRate & BAUD_7200) > 0)
            ddBaudRate1.Items.Add(7200);
        if ((settableBaudRate & BAUD_9600) > 0)
            ddBaudRate1.Items.Add(9600);
        if ((settableBaudRate & BAUD_14400) > 0)
            ddBaudRate1.Items.Add(14400);
        if ((settableBaudRate & BAUD_19200) > 0)
            ddBaudRate1.Items.Add(19200);
        if ((settableBaudRate & BAUD_38400) > 0)
            ddBaudRate1.Items.Add(38400);
        if ((settableBaudRate & BAUD_56K) > 0)
            ddBaudRate1.Items.Add(56000);
        if ((settableBaudRate & BAUD_57600) > 0)
            ddBaudRate1.Items.Add(57600);
        if ((settableBaudRate & BAUD_115200) > 0)
            ddBaudRate1.Items.Add(115200);
        if ((settableBaudRate & BAUD_128K) > 0)
            ddBaudRate1.Items.Add(128000);
        if ((settableBaudRate & BAUD_USER) > 0)
            ddBaudRate1.Items.Add(3000000);
    }

If anyone has tried this or has experience in it. I would appreciate any help i can get.

c#
serial-port
baud-rate
asked on Stack Overflow Feb 18, 2020 by Dirk jnr Odendaal • edited Feb 19, 2020 by Dirk jnr Odendaal

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0