0

I am trying to parse input from an OSI LaserScan AutoSense 615 LiDaR in either a console application or a Windows Forms application using C#.

Per the manufacturer, the output of the LiDaR is byte 11111110 followed by thirty bytes of intensity data (with values between 00000000 and 11111111 inclusive) and thirty bytes of range data (with values between 00000000 and 11111101 inclusive), then byte 11111111 followed by thirty bytes of intensity data and thirty bytes of range data.

The output of the LiDaR is passed through a proprietary ten-pin port to a female nine-pin RS-422 port, then through a male nine-pin RS-422 port, a RS-422 to USB adapter manufactured by SeaLevel, and a USB 2.0 port. I have installed the driver “SeaCOM for Windows v3.7.4”. In Windows Device Manager, I've set the baud rate to 14400. A baud rate of 9600 generates similar results. I've also tried a Prolific USB to Serial COMM Port, which converts RS-232 to USB, and generated similar results.

I've created the following Windows Form application, which, given the code-behind below, displays the position of the leading beam header byte (11111110) in a byte array with length equal to the length of two information bursts, or 2 * 61 bytes. This position is in the single digits most of the time. Is this the right start byte? I figured out that when I convert the byte array to a BitArray, the bits of a byte are read from right to left. Am still good to read the bytes starting from a position with a smaller index and going to a larger index?

If I found the position of 11111110, I evaluate whether byte 11111111 is at that position plus 61. Sometimes byte 11111111 is found, most of the time it is not. Are there stop bits associated with the RS-422 protocol that I am missing? Does my cabling fail to collect all bits? Should I just start at 11111110, collect sixty more bytes, then start looking for 11111111?

enter image description here

namespace Serial_Communication
{
    public partial class formSerialCommunication : System.Windows.Forms.Form
    {

        // Constructor formSerialCommunication initializes form components.
        public formSerialCommunication()
        {
            InitializeComponent();
        }


        // On loading formSerialCommunication, add available port names
        // to comboBoxAvailablePorts.
        void formSerialCommunication_Load(object sender, System.EventArgs e)
        {
            comboBoxAvailablePorts.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
        }


        // Define a buttonOpenPort.Click event handler.
        private void buttonOpenPort_Click(object sender, System.EventArgs e)
        {

            try
            {
                // If buttonOpenPort has been clicked, but either a COM port is not selected
                // or the user has not entered a baud rate, add a message to textBoxReceivedData.
                if (comboBoxAvailablePorts.Text == "" || textBoxBaudRate.Text == "")
                {
                    textBoxReceivedData.Text = "Please select port and enter baud rate.";
                }

                else
                {
                    // Transfer the name of the selected COM port
                    // to serialPortAssociatedWithFormSerialCommunication.
                    serialPortAssociatedWithFormSerialCommunication.PortName =
                        comboBoxAvailablePorts.Text;

                    // Transfer the user's baud rate
                    // to serialPortAssociatedWithFormSerialCommunication.
                    serialPortAssociatedWithFormSerialCommunication.BaudRate =
                        System.Convert.ToInt32(textBoxBaudRate.Text);

                    // Try to open the serial port.
                    serialPortAssociatedWithFormSerialCommunication.Open();

                    // Cause a transition of progressBarConnectionStatus
                    // from gray / empty to green / full.
                    progressBarConnectionStatus.Value = 100;

                    // Disable buttonOpenPort.
                    buttonOpenPort.Enabled = false;

                    // Enable buttonClosePort.
                    buttonClosePort.Enabled = true;

                    // Enable buttonReceiveData.
                    buttonReceiveData.Enabled = true;

                }

            }

            catch(System.UnauthorizedAccessException)
            {
                textBoxReceivedData.Text =
                    "An UnauthorizedAccessException has been caught." +
                    "The exception is thrown when the operating system denies access" +
                    "because of an input / output error or a specific type of security error.";
            }

        } // buttonOpenPort_Click


        // Define a buttonClosePort.Click event handler.
        private void buttonClosePort_Click(object sender, System.EventArgs e)
        {
            // Close serial port.
            serialPortAssociatedWithFormSerialCommunication.Close();

            // Set progress bar at gray / empty.
            progressBarConnectionStatus.Value = 0;

            // Enable buttonOpenPort.
            buttonOpenPort.Enabled = true;

            // Disable buttonClosePort.
            buttonClosePort.Enabled = false;

            // Disable buttonReceiveData.
            buttonReceiveData.Enabled = false;

            // Clear text from textBoxPositionOfLeadingBeamHeaderByteInTwoBurstInput.
            textBoxPositionOfLeadingBeamHeaderByteInTwoBurstInput.Text = "";

            // Clear text from textBoxReceivedData.
            textBoxReceivedData.Text = "";

        }


        // Create a buttonReceiveData.Click event handler.
        private void buttonReceiveData_Click(object sender, System.EventArgs e)
        {
            // Refresh textBoxPositionOfLeadingBeamHeaderByteInTwoBurstInput.
            textBoxPositionOfLeadingBeamHeaderByteInTwoBurstInput.Text = "";

            // Refresh textBoxReceivedData.
            textBoxReceivedData.Text = "";

            try
            {
                // Define the number of bytes transmitted in one information burst
                // from an OSI LaserScan AutoSense 615 LiDaR.
                int bytesInInformationBurst = 61;

                // Define the number of bits transmitted in one information burst
                // from an OSI LaserScan AutoSense 615 LiDaR.
                int bitsInInformationBurst = 8 * bytesInInformationBurst;

                // Declare a buffer for bytes from the serial port.
                byte[] bufferAsByteArray = new byte[2 * bytesInInformationBurst];

                // Define an offset at which to write bytes in bufferAsByteArray.
                int offset = 0;

                // Read into bufferAsByteArray starting at position offset
                // bytesTransmittedInASecond bytes.
                serialPortAssociatedWithFormSerialCommunication.Read(
                    bufferAsByteArray,
                    offset,
                    2 * bytesInInformationBurst);

                // Considering each byte in bufferAsByteArray...
                for (int i = 0; i < bytesInInformationBurst; ++i)
                {
                    // If the present byte is 1111110...
                    if (bufferAsByteArray[i] == 0xFE)
                    {
                        // Display the position of the present byte in bufferAsByteArray.
                        textBoxPositionOfLeadingBeamHeaderByteInTwoBurstInput.Text =
                            System.Convert.ToString(i);

                        if (bufferAsByteArray[i + 61] == 0xFF)
                        {
                            textBoxPositionOfLeadingBeamHeaderByteInTwoBurstInput.Text +=
                                ";" + System.Convert.ToString(i + 61);
                        }

                        break;
                    }

                }

            } // try

            catch(System.TimeoutException)
            {
                textBoxReceivedData.Text =
                    "A TimeoutException has been caught." +
                    "The exception is thrown when the time allotted for a process or operation" +
                    "has expired.";
            } // catch(TimeoutException)

        }


    }

}
Tom Lever
  • 321
  • 3
  • 16
  • Well.. Code. You're missing code from this post. With no code and no hardware on hand, this is a riddle indeed. – CoolBots Feb 01 '20 at 01:57
  • Can you please be more specific on the question? – fenixil Feb 01 '20 at 01:57
  • Disregarding any hardware issue, have you played with the baud rates in the serial port class? Also paragraphs and some basic formatting would make this question more readable – TheGeneral Feb 01 '20 at 01:58
  • 1
    Dear @CoolBots (and fenixil and Michael Randall), I have taken to heart your suggestions. Thank you for your patience. I spent another few hours today experimenting, coming up with more specific questions, reformatting this thread, and providing a version of my code with content specific to a few of my questions, in a share-able format. I am continuing to play with baud rates. – Tom Lever Feb 02 '20 at 06:31

0 Answers0