Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Morning guys, I wonder if anyone knows how to work with automation system? I found this
example on the net http://www.codeworks.it/net/VBNetRs232.htm[^]

Just can not capture the event Spinning Electronic Turnstile? Can someone pass me a solution?
Posted
Updated 15-Feb-13 4:19am
v2
Comments
Thyago Analista 15-Feb-13 7:56am    
I found another example in C # I found it easier to work with and has the same characteristics, except that you can not capture the event swivel ratchet electronics.

https://docs.google.com/file/d/0BwoCzDQ2EvFBYkdGTTFRcUhUTnc/edit?usp=sharing
Jegan Thiyagesan 15-Feb-13 10:22am    
HiI've a lot of automation work, what is your actual problem? can you post your code?

Regards
Jegan
Thyago Analista 15-Feb-13 11:06am    
My problem is trying to find out when the received electronic turnstile spinning so I can catch, tenhos these examples here that the internet worked, but I do not know which part I see to capture the event swivel ratchet, understand?

Code Serial Porta
https://docs.google.com/file/d/0BwoCzDQ2EvFBcy1nck1ZV0IxSDQ/edit?usp=sharing
Thyago Analista 15-Feb-13 11:22am    
Here disponibilizei the source code of my view there:

https://docs.google.com/file/d/0BwoCzDQ2EvFBcy1nck1ZV0IxSDQ/edit?usp=sharing
Jegan Thiyagesan 18-Feb-13 4:37am    
HiThese codes are for just opening the serial port and setting its properties. There is no code that contains your command protocol. All serial interfaces has some form of command protocol. You need to get hold of the command protocol that applies to your turnstile or use an available hyperTerminal (http://www.der-hammer.info/terminal/) to reverse engineer the command protocol from you turnstile.

Regards
Jegan

1 solution

Hi
Here is a serial port handler code:

C#
// Author: Jegan Thiyagesan
// Date: 12/02/2013

using System;
using System.IO.Ports;

namespace PortHandler
{
        /// <summary>
    /// Port handler that handles receiving and sending data over serial port.
        /// </summary>
    public class PortHandler
    {
        private const string Delimiter = "\r";
        private const string Rx = "Rx << ";
        private const string Tx = "Tx >> ";

        private string m_data;
        private SerialPort m_serialPort;

        /// <summary>
        /// Data received event handler
        /// </summary>
        public event EventHandler<EventArgs<string>> DataReceived;
        /// <summary>
        /// Data send event handler
        /// </summary>
        public event EventHandler<EventArgs<string>> DataSend;

        /// <summary>
        /// Error message event handler
        /// </summary>
        public event EventHandler<EventArgs<string>> ErrorMessage;

        /// <summary>
        /// Initiate data received event
        /// </summary>
        /// <param name="dataReceived"></param>
        public void OnDataReceived(string dataReceived)
        {
            var handler = DataReceived;
            if (handler != null)
            {
                handler(this, new EventArgs<string>(dataReceived));
            }
        }
        /// <summary>
        /// Initiate data send event
        /// </summary>
        /// <param name="dataSend"></param>
        public void OnDataSend(string dataSend)
        {
            var handler = DataSend;
            if (handler != null)
            {
                handler(this, new EventArgs<string>(dataSend));
            }
        }

        /// <summary>
        /// Initiate error message event.
        /// </summary>
        /// <param name="errorMessage"></param>
        public void OnErrorMessage(string errorMessage)
        {
            var handler = ErrorMessage;
            if (handler != null)
            {
                handler(this, new EventArgs<string>(errorMessage));
            }
        }

        /// <summary>
        /// return collections of available ports
        /// </summary>
        public string[] Ports
        {
            get
            {
                return SerialPort.GetPortNames();
            }
        }

        /// <summary>
        /// check is port open
        /// </summary>
        public bool IsPortOpen
        {
            get
            {
                return (m_serialPort != null && m_serialPort.IsOpen);
            }
        }

        /// <summary>
        /// basic constructor
        /// </summary>
        public PortHandler()
        {
        }

        /// <summary>
        /// Disconnect port
        /// </summary>
        public void DisconnectPort()
        {
            if (m_serialPort != null)
            {
                CloseSerialPort();
            }
        }

        /// <summary>
        /// Connect port
        /// </summary>
        /// <param name="portName"></param>
        /// <param name="baudRate"></param>
        public bool ConnectPort(string portName, int baudRate)
        {
            if (m_serialPort == null)
            {
                m_serialPort = new SerialPort
                                   {
                                       PortName = portName,
                                       BaudRate = baudRate,
                                       Parity = Parity.None,
                                       StopBits = StopBits.One,
                                       DataBits = 8,
                                       Handshake = Handshake.None,
                                       ReadTimeout = 500 //500 ms
                                   };

            }
            else
            {
                CloseSerialPort();
                m_serialPort.PortName = portName;
                m_serialPort.BaudRate = baudRate;
            }
            OpenSerialPort();
            return IsPortOpen;
        }

        /// <summary>
        /// Send data over port
        /// </summary>
        /// <param name="data"></param>
        public void SendData(string data)
        {
            if ((m_serialPort != null) && m_serialPort.IsOpen)
            {
                m_serialPort.DiscardOutBuffer();
                m_serialPort.Write(data + Delimiter);
                OnDataSend(Tx + data + Delimiter);
            }
        }

        private void OpenSerialPort()
        {
            try
            {
                m_serialPort.DataReceived += SerialPortDataReceived;
                m_serialPort.Open();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message + ex.StackTrace);
            }
        }

        private void CloseSerialPort()
        {
            if (m_serialPort.IsOpen)
            {
                m_serialPort.DataReceived -= SerialPortDataReceived;
                m_serialPort.Close();
            }
        }

        private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            var serialPort = (SerialPort)sender;
            var data = serialPort.ReadExisting();
            ProcessData(data);
        }

        private void ProcessData(string data)
        {
            Console.Out.WriteLine(data + "\r");
        }
    }
}



I hope this help you.

Regards
Jegan
 
Share this answer
 
Comments
Thyago Analista 15-Feb-13 11:09am    
Jegan Thanks,
In your code where I know that the electronic turnstile rotation when did you move to catch me again? In part there? This is my doubt capture the electron spin ratchet ...
Jegan Thiyagesan 18-Feb-13 3:55am    
HiI didn't quite understand your questions.
In serial communication like this, all operations are done through commands, and you must have a sets of command protocol to operate the turnstile that you are working on.

The code above will open the com port and send and receive messages. When the message is received, it will notify you via received event. It is up to you to process the received message according to the command protocol.

I hope this helps.

Regards
Jegan
Thyago Analista 18-Feb-13 8:31am    
Okay

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900