Asii "SUB" (Hex 1a) char causing problems

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 10837 Views - Last Post: 09 September 2009 - 01:36 AM Rate Topic: -----

#1 RDR  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 15-March 09

Asii "SUB" (Hex 1a) char causing problems

Post icon  Posted 01 June 2009 - 06:34 PM

My VB.net application is used to display a combination of Ascii characters and numeric data sent from an embedded micro controller. Each block of data received consists of 16 chars Ascii followed by 16 bytes of numeric values. Whenever the program encounters a Hex 1a value (dec 26, Asc SUB) in the numeric block it appears to "crash" for about 10 seconds then recovers. No exceptions are thrown by the de-bugger though. Note that the "1a" value at this point is being processed as a byte value, not an ascii char.
Software uses "Windows -1252" unicode char ecoding.
Hardware path is: Micro-controller (HCS12X) - FT232 (FTDI USB to "COM3")- VB.net via normal serialport type software.
Note that all other data values display perfectly with this one exception.
Any ideas greatly appreciated.

(Originally posted in VB6 Forum by accident)

Is This A Good Question/Topic? 0
  • +

Replies To: Asii "SUB" (Hex 1a) char causing problems

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 01 June 2009 - 08:46 PM

Hex 0x1a is the Unix EOF character - ^Z. There is probably a configuration field you are yet missing in the serial port control - I know way back (VB 1 or 2)I had trouble with XOn/XOff (0x13/0x11, ^s/^q) flow control characters appearing in the data.
Was This Post Helpful? 0
  • +
  • -

#3 RDR  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 15-March 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 01 June 2009 - 09:20 PM

View Postmark.bottomley, on 1 Jun, 2009 - 07:46 PM, said:

Hex 0x1a is the Unix EOF character - ^Z. There is probably a configuration field you are yet missing in the serial port control - I know way back (VB 1 or 2)I had trouble with XOn/XOff (0x13/0x11, ^s/^q) flow control characters appearing in the data.


Thanks for the reply Mark. Is the "configuration field' you mention accessed via the SerialPort properties box or some other mechanism?
Was This Post Helpful? 0
  • +
  • -

#4 barby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-August 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 26 August 2009 - 11:20 AM

hi i'm new here :)

i have read the first post from RDR...
I have exactly the same problem and the same hardware configuration...

i use a microcontroller, a USB to serial cable (with FTDI chip) and a c# application to read data...
the problem is with the 0x1a / 26 / EOF char...in a 800 byte stream...

Some people use a separate thread to read the bytes...
look @
http://social.msdn.m...14-bd2aee7fc7e3
http://social.msdn.m...f5-e78cdd797cec

but i need a fast communication (~4000 bytes/sec with crc control)...
so i can't wait for this "misterious serial crush".... :angry:

any news rdr?
Was This Post Helpful? 0
  • +
  • -

#5 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 26 August 2009 - 01:16 PM

The only suggestions are that you check the HandShake and Encoding state on the port and use ReadByte to read it, not ReadChar or ReadLine.
Was This Post Helpful? 0
  • +
  • -

#6 barby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-August 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 26 August 2009 - 01:56 PM

handshake disable...no xon xoff or similar

encoding checked...i have change the encoding with alle the standards (try UNICODE,UT8 etc...) and all the internet suggest encoding (1252 for example) ==> no result...

I use the "readbyte" or the "read with 1 byte"...i try the "read with more byte" too...


it's not so simple guys belive me ;)

or maybe I'm stupid dunno lol

This post has been edited by barby: 26 August 2009 - 01:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 26 August 2009 - 05:49 PM

So the application is hanging on Socket.ReadByte?
Was This Post Helpful? 0
  • +
  • -

#8 barby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-August 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 26 August 2009 - 11:34 PM

View PostT3hC13h, on 26 Aug, 2009 - 04:49 PM, said:

So the application is hanging on Socket.ReadByte?


the right answer is:

Yes it fires. The problem is that the comBuffer from the position where is the 0x1A to is not filled.

For example, this is the content of the comBuffer if there isn't the 0x1A:

7E 4B 00 00 13 A0 64 1F 01 01 99 19 D3 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 F1 FF

And this is ther is 0x1A:
7E 24 00 00 13 A0 54 1A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


Maybe if i wait, the missing bytes arrived...

But now my question...my protocoll speed (remember that i have attached a microcontroller) is function of the number of 0x1A in the packet??? Are we crazy???

An alternative solution, can be (like I did in a .Net application) to use an alternative OCX for serial comunication...

Can avoid the problem?
Can be integrated into a C# Express edition?
Do you know a valid OCX for C#?
I used this http://home.comcast..../NETCommOCX.htm in the past...

Help a cheap firmware guy :D
Was This Post Helpful? 0
  • +
  • -

#9 RDR  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 15-March 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 27 August 2009 - 02:15 AM

As the original poster (RDR) I am following this very closely as I'm still looking for a solution myself.
Like barby I have tried all manner of "fixes", none of which are 100% effective.
Today I tried reading one byte at a time using a FOR - NEXT loop rather than the serialport.read direct to a byte array. This improved the situation but now I periodically get gross errors in the data like "22,608" rather than say "150".
It would appear that the data is always read correctly into the comms buffer (at least) but the error occurs when the buffer is read into a byte array. When the "transfer" encounters the h1A it aborts the read beyond that point. Doing single byte transfers seems to fix that problem but now creates these periodic data errors.
Not sure if all this helps but I'm willing to try anything to fix the problem.
Was This Post Helpful? 0
  • +
  • -

#10 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 27 August 2009 - 06:26 AM

I've always found reading single bytes to be the most reliable when reading from a socket type object. Can you please post some of the code?
Was This Post Helpful? 0
  • +
  • -

#11 RDR  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 15-March 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 27 August 2009 - 02:42 PM

Here is section of the code showing the transfer from com buffer and how some of that data is applied.
 If SerialPort1.ReceivedBytesThreshold = 33 Then ' 33 bytes received
				SerialPort1.Read(sadj, 0, 17)			   'this is the ASCII block (as string)
				For bnum = 0 To 15
					SerialPort1.Read(sdata, bnum, 0)		'read the data bytes one at a time
				Next bnum
				sadj(0) = " "
				label1.Text = sadj						  ' display the text string
				flags2 = sdata(15)						  ' define the flags byte
				If Not flags2 And &H4 Then				  ' do next if bit = 0
					lhighbyte = sdata(0)
					llowbyte = sdata(1)					 ' form into 16 bit word
					lrpm = (lhighbyte * &H100) Or llowbyte  'hex to decimal conversion
					Label2.Text = (CStr("RPM " & lrpm))	 ' display the result 


Was This Post Helpful? 0
  • +
  • -

#12 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 27 August 2009 - 03:26 PM

1: .ReceivedBytesThreshold doesnt do what you think, look it up. I think that your looking for is .BytesToRead

2: This loop:
For bnum = 0 To 15
	SerialPort1.Read(sdata, bnum, 0)		
Next bnum


doesn't do anything, all it does is read 0 bytes 16 times. Replace the 0 with a 1.
Was This Post Helpful? 0
  • +
  • -

#13 barby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-August 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 27 August 2009 - 11:39 PM

Lol...awesome code ;-P

I post my code after a little test :-D
Was This Post Helpful? 0
  • +
  • -

#14 barby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-August 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 28 August 2009 - 12:14 AM


const UInt16	RX_BUFF_SIZE			= 4096;
		const UInt16	REQUEST_SIZE			= 800;


		public Form1()
		{
			InitializeComponent();

			serialPort1.BaudRate				= 115200;
			serialPort1.DataBits				= 8;
			serialPort1.StopBits				= System.IO.Ports.StopBits.One;
			serialPort1.Parity				  = System.IO.Ports.Parity.None;			
			serialPort1.ReadTimeout			 = 100;
			serialPort1.WriteTimeout			= 100;
			serialPort1.ReadBufferSize		  = RX_BUFF_SIZE;
			serialPort1.ReceivedBytesThreshold  = REQUEST_SIZE;
			serialPort1.Handshake			   = System.IO.Ports.Handshake.None;
			//serialPort1.Encoding				= System.Text.Encoding.GetEncoding(28591);
			//serialPort1.Encoding				= System.Text.Encoding.GetEncoding(1252);
			//serialPort1.Encoding				= System.Text.Encoding.GetEncoding("Windows-1252");
			serialPort1.DiscardNull			 = false;
			serialPort1.DtrEnable			   = false;
			serialPort1.RtsEnable			   = false;
			//serialPort1.BaseStream.ReadTimeout = 10;  //???  

	   }

		// === DUMP button click EVENT ===//
		private void button1_Click(object sender, EventArgs e)
		{
			serialPort1.Close();
			serialPort1.PortName = comboBox1.Text;
			
			//try to open
			try
			{				
				serialPort1.Open();
			}
			catch (Exception exp)
			{
				MessageBox.Show(exp.Message);
				return;
			}

			Load_Timers();
			Load_Retry();

			//SEND START COMMAND : set Address & Size
			Mem_Adr = 0;	Mem_Size_Req = REQUEST_SIZE;
			send_cmd_on_serial_port(Mem_Adr, Mem_Size_Req);

			// manage controls to receive
			Flag_Start = true;
		}

		//============ SERIAL PORT DATA RECEIVE FUNCTION ============//
		private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
		{			
			if (Flag_Start == true)
			{
				if (manage_new_packet() == true)
					{
					// ===> OK <=== //

					Load_Timers();		  //re load
					Load_Retry();		   //re load

					Log_packets++;

					if (Flag_End == false)
						{
						Mem_Size_Req = REQUEST_SIZE;
						send_cmd_on_serial_port(Mem_Adr, Mem_Size_Req);
						}
					}   //if NOK, wait timer expiration for retry				   
			}
		}


		private bool manage_new_packet()
		{
			UInt16 len_data, len_for_crc;
			SERIAL_CMD cmd;
			UInt16 crc;
			UInt16 i;
			
			//check serial port status
			if (serialPort1.IsOpen == false)
			{
				Clear_RX(true);
				return(false);
			}

			//Read serial port bytes
			while (serialPort1.BytesToRead != 0)
			{
				RX_buff[idx_rx] = (byte)serialPort1.ReadByte();
				idx_rx++;

				if (idx_rx >= RX_BUFF_SIZE)		 //protection
				{
					Clear_RX(true);
					Log_errors = "buffer overflow";
					return (false);
				}
			}

			//Check packet parameters
			
			[...buffer analisys...]

			Clear_RX();
}

		//============ SERIAL PORT DATA "CLEAR RX PARAMETERS" ============//
		private void Clear_RX(bool dummy)
		{
			UInt16 i;

			serialPort1.DiscardInBuffer();  //free the buffer from data...

			for (i = 0; i < RX_BUFF_SIZE; i++)
				RX_buff[i] = 0;
			
			idx_rx = 0;						//clear Rx index
		}




with this code and no 0x1A in the packets all works fine...
now i try some adjustments for 0x1A...but (for me) it's anyway a huge serial component bug ;)

This post has been edited by barby: 28 August 2009 - 12:15 AM

Was This Post Helpful? 0
  • +
  • -

#15 RDR  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 15-March 09

Re: Asii "SUB" (Hex 1a) char causing problems

Posted 28 August 2009 - 04:26 AM

You are correct, the "0" value was a typo as the code listed was a bunch of copy and paste snippets.
The receivedbytes.threshold is required for my code to distinguish between two blocks of incoming data packets of different lengths, works fine for that pupose. I'm sure .bytestoread would also work fine.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2