Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! 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

Member level 1
Joined
May 14, 2009
Messages
37
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,732
java serial port pic

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()
 

receive data from com port with java

Not familiar with java, but this is a common question in serial comms. A serial port will just send 8 serial bits with parity. It does not care about what the data is. Hex Dec Ascii is meaningless. It is how the data is interpreted that matters. If if you send a byte to a terminal or printer, it will be read as either a control code or character. If on the other hand you send exactly the same code to an MCU, it will be in the receive buffer as a number, you may then read it as bin, hex, or dec. If you are sending data as single bytes between MPUs, it is probably better to read and write directly to the serial port buffers. You receive exactly what you send without any interpretation. Not sure if this is the info you needed, but hope it helps a little.
 

ascii characters to serial port java

Hi,
As it looks right now, your code will only print the characters "axyz«" in this exact sequence, but I guess this is just a test of yours.

The serial port input stream should deliver even non-ASCII characters, so the SerialPortEvent should fire on any data (is that so?).
In that case I would question the printing part, where println is not declared to take bytes, so cast each one to an integer before throwing them to println.
(You could also convert each byte to two ASCII-equivalent bytes and print them with write(byte[] buf, int off, int len) instead.

Arthur
 

nu mber of bytes at serial port

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top