Receiving extra bytes on serial port c#

  • Thread starter Thread starter TheByteLord
  • Start date Start date
T

TheByteLord

Guest
Hello to all.

I am making a proyect that implies a PIC18F4610 comunicating through a Zigbee wireles UART to a UsB serial port with a wireless Zigbee module. When I press a button on the PIc it send a 10 byte packet of data (bytes) wirelessly to the other Zigbee module on the USB port.

The packet it sends is 0A 01 80 90 A0 B0 C0 D0 E0 F0. If I use on the PC any terminal program like RealTerm I get that exact packet of bytes sent. However, on my program I get (generally) the following packet: 3F 3F 0A 01 80 90 A0 B0 C0 D0 E0 F0 that includes de good packet sent but two 3F at the beginning (that is, twelve bytes instead of ten that were sent). I could overcome this by program but sometimes it only places one 3F at the beginning and others it slips a 3F between the packet items. No packet items are lost, only 3Fs are added (only one or two) here and there, generally at the beginning.

The serial port is open as it receives the packet sent. It´s configuration:

ST.puertoserie.PortName = "COM1";

ST.puertoserie.BaudRate = 9600;

ST.puertoserie.Parity = (Parity) 1;

ST.puertoserie.DataBits = 8;

ST.puertoserie.StopBits = (StopBits) 1;

ST.puertoserie.Encoding = System.Text.Encoding.GetEncoding(28591);

The DataReceived event handler has been defined and fires upon receiving bytes at the serialport.

ST.puertoserie.DataReceived += new SerialDataReceivedEventHandler(RecepcionBytePuertoSerie);

The event handler procedure (ST is a global class I have and includes some variables and the serialport):


private void RecepcionBytePuertoSerie(object sender, SerialDataReceivedEventArgs e)
{
while(ST.puertoserie.BytesToRead > 0) {

ST.rxcadena[ST.rxindex] = (byte) ST.puertoserie.ReadByte();

ST.rxindex++;
}



byte[] kprueba = new byte[ST.rxindex];

for (int t = 0; t < ST.rxindex; t++) { kprueba[t] = ST.rxcadena[t]; }

MessageBox.Show(BitConverter.ToString(kprueba).Replace("-", " ")); // Display bytes in message box


ST.rxindex = 0;

ST.puertoserie.DiscardInBuffer();


}

Any clue on why this is happening? I've tried all sorts of things but no way. Terminal emulator receives correctly. There has to be some underlying reason in the program, etc. that makes this happen. Right now the program only does this with a simple form until I can get serial communication working. The wireless doesn't seem the problem (hidden codes or anything) as the terminal emulator receives exactly what the PIC is sending. Windows doesn't seem to be the problem either as the terminal emulator receives correctly.

Any clues / help?

Paul

Continue reading...
 
Back
Top