09-07-2011 12:13 AM
Gidday,
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.
Solved! Go to Solution.
09-07-2011 01:17 AM
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.
09-07-2011 05:40 PM
Gidday,
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.
Cheers
09-07-2011 06:13 PM
09-07-2011 07:25 PM
Gidday,
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!!
09-07-2011 07:31 PM
Query,
What if it is not asynchronous with start and stop bit?
K
09-07-2011 07:51 PM
09-07-2011 08:34 PM
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.
09-07-2011 09:06 PM
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
delay(400);
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++)
{
entireBuffer.Add(buffer[i]);
}
readCount++;
}
// Create the byte [] to pass back.
try
{
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);
}
//-----------------------------------------LFLFLFLFLFLFLFLFLFLFLFLFLFLFLF--------------------------------
//-------------LF data is storage from here BN
//-------------------If COM2 take this
if(this.currentSerialPort == this.serialport1) //LF
{
delay(200);
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;
}
09-07-2011 11:08 PM