Jump to content

Recommended Posts

Posted

Hello,

 

I have a .net application that connects to a 570 via RS-232 using System.IO.Ports.SerialPort. Normally, it will connect. However, when playing with different baud rates, it sometimes does not connect until I connect the same PC with any Unitronics software (Visilogic or Remote Operator). My question is this:

 

With VIsilogic, it appears that you can define the baud rate to anything in the connection properties and it always connects. How does the controller adapt accordingly? I think "out of the box", the rate is set to 115200.

 

If I change my application to 57600, it failes to connect. But, if I connect using Visilogic @ 57600 it works. I go back to my application and then it works. I am trying to determine what Visilogic is doing that I am not. My connection looks like this:

 

                    // Connect to the PLC
                    try
                    {
                        string comPort = Properties.Settings.Default.CommPort;
                        int comBdRate = Properties.Settings.Default.CommBdRate;
                        int comTimeOut = Properties.Settings.Default.CommTimeOut;
                        _serialPort.PortName = comPort;
                        _serialPort.BaudRate = comBdRate;
                        _serialPort.DataBits = 8;
                        _serialPort.Parity = Parity.None;
                        _serialPort.Handshake = Handshake.None;
                        _serialPort.ReadTimeout = comTimeOut;
                        _serialPort.NewLine = "\r";
                        _serialPort.Open();
                    }
                    catch (Exception)
                    {
                        // Disconnect from the PLC
                        _serialPort.Close();
                    }
 
Once this executes, I begin sending PCOM commands:
 
            try
            {
                // Send the message out the serial port.
                _serialPort.DiscardInBuffer();
                _serialPort.WriteLine(message);
                // Verify the message was received, catch the timeout
                string response = _serialPort.ReadTo("/A00SBF5");
                if (response == "/A00SBF5")
                {
                    //do nothing, success!!
                }
            }
            catch (Exception)
            {
                _connectionLost = true;
                _serialPort.Close();
                return -1;
            }
 
The _serialPort.ReadTo() catches the timeout (default 1 sec). When that occurs, we assume we lost the connection. Is there something I should be doing to prime the port first?
 
Thanks!
 
 
 
 
 
Posted

You cannot communicate with the PLC if the baudrates does not match, so the only way that is left is using hardware signals.

If you set the BreakState of the serial port, then it signals the PLC to switch to 9600.

            serial.BreakState = true;
            Thread.Sleep(500);
            serial.BreakState = false;
            Thread.Sleep(1000);


Now you need to tell the PLC that you want to change the baud rate. The command would be:
        internal string BreakCommand()
        {
            string breakCommand = "CPC";
            breakCommand += "1"; // plc port
            breakCommand += "T"; // Temp change
            switch (BaudRate)
            {
                case BaudRate.BR110:
                    breakCommand += "01";
                    break;
                case BaudRate.BR300:
                    breakCommand += "02";
                    break;
                case BaudRate.BR600:
                    breakCommand += "03";
                    break;
                case BaudRate.BR1200:
                    breakCommand += "04";
                    break;
                case BaudRate.BR2400:
                    breakCommand += "05";
                    break;
                case BaudRate.BR4800:
                    breakCommand += "06";
                    break;
                case BaudRate.BR9600:
                    breakCommand += "07";
                    break;
                case BaudRate.BR19200:
                    breakCommand += "08";
                    break;
                case BaudRate.BR38400:
                    breakCommand += "09";
                    break;
                case BaudRate.BR57600:
                    breakCommand += "0A";
                    break;
                case BaudRate.BR115200:
                    breakCommand += "0B";
                    break;
            }

            breakCommand += "FF"; // Timeout - no change
            breakCommand += "FF"; // Flow Control - no change
            return breakCommand;
        }


For the result you got, you add the STX + Unit ID + Checksum + ETX like in any ASCII command:
STX + Unit ID + Command + Checksum + ETX


As I said, you need to change your baud rate to 9600 in order to send that command.
For that, you need to disconnect the serial port (since changing the baud rate while the connection is opened will result an exception).

            serial.Disconnect();
            serial.BaudRate = BaudRate.BR9600;
            serial.Connect();
            Thread.Sleep(1000);
            serial.WriteLine(_theResultCommandThatYouGot);
            serial.Disconnect();
            serial.BaudRate = originalBaudRate; // This is the baud rate that you told the PLC you want to use
            serial.Connect();

// now you are good to go.
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

This site uses cookies. By clicking I accept, you agree to their use.