How to get the BaudRate of the device?
Asked Answered
C

2

8

I'd like to know if there is a way to get the Baud Rate when it's connected on the RS232 port
BEFORE you initialize the SerialPort class and set it's values. Let me try to explain the reason for that...

Today, I'm working with two different RFID Reader devices, each one works on a different BaudRate, so if I set a wrong baudrate when I create the SerialPort class, it will read the card id all wrong, instead get the real card's id, it will get something like ????|W2???.
Also, there's a possibilite that the device have a USB port.

That's why I'd like to know the device's baud rate before I instantiate the SerialPort class.

Coplanar answered 16/5, 2013 at 14:5 Comment(8)
You know which baud rates and devices your program supports. You want you are expecting when you request certain information. The logical solution is to connected to the device using one baund rate, check if the requested data is correct, and if its not disconnect and try the other baud rate. There is no way to "request" the band rate from a serial device.Milzie
@Ramhound you could post this as an answer.Turkmen
@Ramhound Hmmm... I have 2 differents devices, one workss on 9600 and the other works on 19200 but I cant create an application where I'll have to change it's value according to the configuration of the user's device... You know ? But how could I do that ? As I know, I'll be able to test the data when a RFID Card is read. Could you give me some ideia how to work around it ? Thanks !Coplanar
@ManuLetroll - I could post it as an answer, since I have no code to post, I won't do that.Milzie
@Coplanar - Why exactly can't you write software that trys different baud rates until it recieve data it can recongize? You only have 2 different band rates to check. You could also just do it by device type, or get the user KNOW what the band rate for the device is, and let choose that configuration. You asked how to get the band rate of a serial device, you can't, you need to already ve that information before the connection is made. I already gave you one suggestion....Milzie
@Ramhound I guess I'll make a new form with Configurations... I just don't get one thing: I'll open a new WinForm to set the configs, how may I get those values and set to the preview WinForm ? Maybe create a new table on my DB with the configurations, what do you think ? Would that be a good practice?Coplanar
Default values exists. You can set and call serialPort object. Please refer to my answer. Hope it may help you.Genuine
Create a new entity class.cs ? @Genuine and create the attributes for the values, like (BaudRate, PortName etc...) right ?Coplanar
G
6

I tried for my serial usb devices. Keep changing the baud rate and check. ComboBox contains series of possible baudrates.

    public void initConfig(SerialPort serialPort)
    {
        // you can assign these values in a combo box
        string[] ports= "{COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8"};

        //you can assign these values in a combo box in a string format
        int[] baudRate = { 4800, 9600, 19200, 38400, 57600, 115200, 230400 };

        serialPort.PortName = ports[0]; //else get from combobox  : portCombobox.SelectedItem
        serialPort.BaudRate = baudRate[0];
        //serialPort.BaudRate = Int32.Parse(speedComboBox.SelectedItem.ToString());

        //you can have controls to store and change these values if required
        serialPort.Handshake = System.IO.Ports.Handshake.None;
        serialPort.Parity = System.IO.Ports.Parity.None;
        serialPort.DataBits = 8;
        serialPort.StopBits = System.IO.Ports.StopBits.One;
        serialPort.ReadTimeout = 200;
        serialPort.WriteTimeout = 50;
    }

change the strings into respective types and call open.

finally:

    public void callingMethod() //or your connect event attached control
    {
        SerialPort serialPort = new SerialPort();

        initConfig(serialPort);

        try
        {
            serialPort.Open();
        }
        catch
        {
            MessageBox.Show("Error: Unable to Open the serial interface !");
            return;
        }
    }
Genuine answered 16/5, 2013 at 14:52 Comment(4)
Thanks for answering. I'll create it like a class.cs ? Call it as an object on my winform config then assign the values. Right ? =XCoplanar
yes, you can! For better clarity, I have made simple and also edited the above answer. I may not able to respond as I am leaving for the day. Best of Luck !Genuine
Thanks. I Just need to figure out how to save the configs on my Form2 then Call the SerialPort class on my Form1 with the configurations setted on Form2...Coplanar
Hello, I am facing same issue. Can u check below question: #59576456Glamorize
B
5

Depending upon the details of what you connect to etc. You can loop through a list of baud rates, attempt to connect and then perform an echo test. If you connect at the wrong rate, your echo will be returned as garbage instead of the string you sent. This methodology is working for me.

Barbey answered 22/8, 2016 at 15:46 Comment(2)
can you elaborate on how you perform an echo test? This sounds like a reasonable solution for my particular scenario.Stutman
An echo command is available in basically every terminal program out there - dos/batch, linux/bash, uboot, etc. It's perfectly safe to issue one. If you can programmatically send commands to a terminal and get back the results, then executing something like echo my-test-string should return my-test-string. If the baud rate is right, that will work. If it is not, the value will come back screwed up i.e. the output will not equal the input.Barbey

© 2022 - 2024 — McMap. All rights reserved.