Showing results for 
Search instead for 
Did you mean: 

Reading a bitstream from the COM Port.

Go to solution



I have some information streaming (from a microcontroller) into a serial port on the computer. I know the data is made up of;


5 bytes - Wake Up - Combination of 1s and 0s


1 byte - Header - $A2H


4 bytes - ID code


1 byte -  Code


1 byte -  Code


Check sum - 1 byte


I was informed that I cannot just use the serial/read examples. Can someone please shed some light on the matter as I am unfamiar with dealing with pure bitstreams.


Baudrate is 19200.

0 Kudos
Message 1 of 18

The issue with the example is that it is looking for a termination character. Your data stream does not have that. You could use the Bytes at Port value and wait until you have at least 13 bytes at the port. This is the total length of your message. When you read that data (simply do a read of 13 bytes (make sure to disable the termination character)) you should validate that the first 5 bytes match your wake up pattern. If they you should have a valid message which you can confirm using the checksum. As they say after this you simply rinse, lather and repeat.

Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 2 of 18



Thank you for the suggestion, however there are not bytes being read.


Let me build the bigger picture.


I have a product that sends 18 bytes of data (including the 5bits preamble) over RF. The manufactuer has provided a micro-controller to do the decoding of their encryptions and it in turn spits out 1s and 0s (bitstream). Whether this is ACSII or HEX data I have no idea. Most likely it is just a pure bit stream and I have to then decode all the bits manually. There is no documentation what so ever on the micro-controller or the C# code!!!


The project before this uses C# and from what I can gather from the code, the serial port is set 19200 and thats it. Not familiar at all with C# I decided to use LabView as there is some control as well.


So if someone could suggest a plan of attack for me to try, that would be great.







0 Kudos
Message 3 of 18
If you don't know what it is sending ("ascii or hex") just run hyperterminal or something similar. It is a good first step anyway to make sure you have all the correct parameters. If it is indeed rs232 (asynchronous with start and stop bit) then you can read it. The bytes are framed between start and stop bits - you cannot read the "raw bit stream". You have to get bytes because that is just the way the hardware works.
LabVIEW 2012

Message 4 of 18



I have tried this on numerous occasions.


There is nothing on the serial port at all. It is strange, because when I try with the original software (C# on another computer) it works. But when I run the hyper terminal on that machine it doesn't work.

I have no idea. Frustrating as HELL!


Cheers Though!!

0 Kudos
Message 5 of 18



What if it is not asynchronous with start and stop bit?



0 Kudos
Message 6 of 18
RS232 is asynchronous with a start and stop bit. (or bits). If you cannot get it to work with hyperterm then I don't think it will work with LabVIEW. Any chance you can post the C# code?
LabVIEW 2012

0 Kudos
Message 7 of 18

I wish I could but there is sensative customer information.


Hmm... I am giving the code another look through. Let you know what I find.

0 Kudos
Message 8 of 18

From what I can tell,


The serial port is open, the buffer is read by the code below. I hope this Helps.


private byte[] readSerialPort(int readTimeout, System.IO.Ports.SerialPort portName)
            byte[] buff = new byte[1];

            //serialport.DiscardInBuffer();       // Clear the Recieve Buffer
            serialport.ReadTimeout = readTimeout;
            int readCount = 1;          // Keep track of if we have to read again.
            Char[] magic=new char[8];
            ArrayList entireBuffer = new ArrayList();   // Use this arraylist to store the bytes for now.
            delay(50);  //give some time for port to have data        
            while (portName.BytesToRead > 0)
                //200 not enough time to read serial port make a little larger
                //there has been times when this was on the critical limit of reading the serial port
                byte[] buffer = new byte[portName.BytesToRead];
//danger bad code dm200707              
                //int resultOfRead = portName.Read(buffer, 0, portName.BytesToRead);
//end danger bad code
//use this code -next line- to avoid race condition of buffer size available growing
                int resultOfRead = portName.Read(buffer, 0, buffer.Length);

                for (int i = 0; i < buffer.Length; i++)

            // Create the byte [] to pass back.
                buff = (byte[])entireBuffer.ToArray(typeof(byte));  // Export our arraylist into a byte array.
            catch (Exception e)
                MessageBox.Show("Couldn't Convert byte [] in readSerialPort()\n" + e.Message);

            //-------------LF data is storage from here BN
            //-------------------If COM2 take this
            if(this.currentSerialPort == this.serialport1) //LF
                for (int j = 0; j < buff.Length; j++)
                    if (j < buff.Length - 12) //so we dont go out of bounds of aRRAY IN OURT CHECK
                        if (buff[j] == (char)0x49)  //make it faster by checking for this char first before all the others at once as well
                            if ((buff[j + 1] == (char)0x44) & (buff[j + 2] == (char)0x45) & (buff[j + 3] == (char)0x3D))
                                 //if ((tranponderBytes[_keyNumber] != masterTranponderByte[_keyNumber]) && (rpt1 <= 100))
                                buff = new byte[]{buff[j+4],buff[j+5],buff[j+6],buff[j+7],buff[j+8],buff[j+9],buff[j+10],buff[j+11]};
                                return buff;
            return buff;

0 Kudos
Message 9 of 18
All I can tell is that this definitely looks like standard serial port stuff. I don't see any settings but if you know the baud rate you are off to a good start. Does this C# code work? If so are you using the same computer, cable and serial port with LabVIEW? Also can you post the LabVIEW code?
LabVIEW 2012

0 Kudos
Message 10 of 18