Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

receiving integer number via serial port

Status
Not open for further replies.

hhhsssmmm

New Member
hello

im using a PIC to transmit 5 HEX numbers. I see my results displayed very nicely on a PC terminal program. Im not using hyperterminal but instead another thrid party terminal program which allows you to view binary information via serial port. This terminal program is called "SuperMon Serial Ver 5.5".

Now after the successfull test with the above, i then wrote my own JAVA program and used the RXTX package to use the serial port with JAVA language. Sadly now when I power up the PIC i do not get any thing displayed in my window. Interstingly my JAVA program compiles but is not displaying anything!

Also interestingly is the fact that if I send ASCII characters from the PIC such as 'a' 'b' 'c' 'd' ... etc, then my JAVA program displays those characters on screen with out any problem. So i know that my JAVA program does receive from the serial port and does work... but only for ASCII characters though.

Below is the code snippet of the JAVA code which actually receives data via serial port and displays it on screen. Please can someone look at it and kindly suggest why can i not print the HEX numbers on screen.

Code:
public void serialEvent(SerialPortEvent event) //listens to incoming data from serial port
    {
   
		switch (event.getEventType()) //receiving data from serial port
		{		
			
			case SerialPortEvent.DATA_AVAILABLE:
			
			// instantiating and initiallizing receiving byte[] array			
			byte[] readBuffer = new byte[8];					
			
			try 
			{			
				// read data and place in byte[]
				while (inputStream.available() > 0) 
				{				
					int numBytes = inputStream.read(readBuffer);									
				}			
				
				//If the below numbers are received then print on screen
				if(readBuffer[0] == 0x61 && 
				   readBuffer[1] == 0x78 &&
				   readBuffer[2] == 0x79 &&
				   readBuffer[3] == 0x7A &&				   
				   readBuffer[4] == 0xAB )				   
				   {  
				
						System.out.println(readBuffer[0]);
						System.out.println(readBuffer[1]);
						System.out.println(readBuffer[2]);
						System.out.println(readBuffer[3]);
						System.out.println(readBuffer[4]);			
					
				   }		    			
				
			} 
			catch (IOException e) 
			{}
   
			break;
			
		}
		
	} //end of serialEvent()
 
Most likely, your operating system is getting in the way and not allowing you to access the serial port.
 
Mike

Thank you for replying.

I can not understand your statement...in technical sense. What do you mean by Operating System getting in way?

Im using WinXP service pack3.

I can receive ASCII characters via serial port perfectly with my JAVA program.

Why can I not receive the hex numbers?

Please can you suggest something more elaborate.

Thank you

Haseeb
 
Upon rereading your post, I see that the operating system is not the issue. What comm paramaters are you using? You would have to use N81 to suppress parity checking so that you get all eight bits in the buffer.
 
yes thats correct...here is the JAVA statement which configures the serial port...

Code:
 try
		{        
			
			serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
			
		} 
		catch (UnsupportedCommOperationException e) 
		{}


like i said...if use the lines of code below for ASCII character reception and verification....my JAVA program works!!

Code:
public void serialEvent(SerialPortEvent event) //listens to incoming data from serial port
    {
   
		switch (event.getEventType()) //receiving data from serial port
		{		
			
			case SerialPortEvent.DATA_AVAILABLE:
			
			// instantiating and initiallizing receiving byte[] array			
			byte[] readBuffer = new byte[8];					
			
			try 
			{			
				// read data and place in byte[]
				while (inputStream.available() > 0) 
				{				
					int numBytes = inputStream.read(readBuffer);									
				}			
				
				//If the below numbers are received then print on screen
				if(readBuffer[0] == 0x61 &&   /* ASCII 'a' */
				   readBuffer[1] == 0x78 &&  /*  ASCII 'x' */
				   readBuffer[2] == 0x79 &&  /*  ASCII 'y' */
				   readBuffer[3] == 0x7A )    /* ASCII 'z' */				   
				    
				   {  
				
						System.out.println("ASCII  characters received");						
								
					
				   }		    			
				
			} 
			catch (IOException e) 
			{}
   
			break;
			
		}
		
	} //end of serialEvent()

So please can you help?

Haseeb
 
Last edited:
I dont read JAVA:D When you say HEX numbers, do you mean that in the PIC you are encoding 0-9 and A-F as ascii chars, or are you just sending a byte with a value of 0-255?
 
Last edited:
Would it be that . .

Your received byte is in fact a non printable character, therefore you have to add a value to it before it will display, like sending values to an LCD - if you sent 0 you get nothing - if you send 0+48 you get 0 displayed? Hope that makes sense :confused:
 
Thank you so much for your help..

I have figured out why i was experiencing the problem in the first place..

Please see my below code snippet which solves the problem for good in view of receiving ASCII characters or any non-ASCII character...such as any random number...(0xAB).

Now i can receive flawlessy any data packet via serial port using JAVA. I use my PIC to send to the serial port.

PEACE!

Haseeb


Code:
			try 
			{
			
				receiveBufferCounter = 0; //reset buffer counter
			
				// read and store data in buffer until you finish receiving
				while (inputStream.available() > 0) 
				{				
					
					receiveBuffer[receiveBufferCounter] = inputStream.read();
					
					receiveBufferCounter++;
					
				}
							
							
							
				/*****************************************			
							
				NOTE: Always use decimal numbers to interpret the recived data			 							 
							 
				*****************************************/			 

				//verify received packet....
				
				if(receiveBuffer[0] == 97 &&  //decimal value of ASCII character 'a'
				   receiveBuffer[1] == 120 && //decimal value of ASCII character 'x'
				   receiveBuffer[2] == 121 && //decimal value of ASCII character 'y'
				   receiveBuffer[3] == 122 && //decimal value of ASCII character 'z'              
				   receiveBuffer[4] == 171 )  //decimal value of number 0xAB
					{ 
             
						System.out.println("Haseeb is Happy!!");	
						
					}
				
						
					    			   
				
				
			} 
			catch (IOException e) 
			{}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top