outputting information to PC

sub-bg

New Member
I have generated the following code for a compass and I want to be able to display it on a java GUI .
---------------------------------------------------------------

dirsc =%00001111 'should declare all port c as inputs

SYMBOL NORTH = pin7
SYMBOL EAST = pin6
SYMBOL SOUTH = pin5
SYMBOL WEST = pin4


main:


if North=0 and South=1 and East=1 and West=1 then label_11 ' North

if North=0 and South=1 and East=0 and West=1 then label_12 ' North East

if North=1 and South=1 and East=0 and West=1 then label_13 ' East


if North=1 and South=0 and East=0 and West=1 then label_14 ' South East

if North=1 and South=0 and East=1 and West=1 then label_15 ' South

if North=1 and South=0 and East=1 and West=0 then label_16 ' South West


if North=1 and South=1 and East=1 and West=0 then label_17 ' West


if North=0 and South=1 and East=1 and West=0 then label_18 ' North West
goto main ; *****

label_11: let pins = %10000000 ' LED1 on
sertxd (1,0)
pause 500
goto main

label_12: let pins = %01000000 ' LED2 on
sertxd (2,0)
pause 500
goto main

label_13: let pins = %00100000 ' LED3 on
sertxd (3,0)
pause 500
goto main

label_14: let pins = %00010000 ' LED4 on
sertxd (4,0)
pause 500
goto main

label_15: let pins = %00001000 ' LED5 on
sertxd (5,0)
pause 500
goto main

label_16: let pins = %00000100 ' LED6 on
sertxd (6,0)
pause 500
goto main

label_17: let pins = %00000010 ' LED7 on
sertxd (7,0)
pause 500
goto main

label_18: let pins = %00000001 ' LED8 on
sertxd (8,0)
goto main
-------------------------------------------------------------
I want to display each direction on the PC as I turn the compass but I'm not sure how to use the sertxd command to do that.
Any help would be appreciated.
 

lbenson

Senior Member
If you're using a 28X1 as in the other post, why not keep it simple on the picaxe side? If your 4 inputs are on standard inputs 4,5,6, and 7, the following program should do what you need if you decode the numbers 0-15 in your Java. Try it in the simulator.
Code:
#picaxe 28X1
main:
  b0 = pins
  if b0 <> b1 then ' there has been a change
    b1 = b0
    b0 = b0 /16   ' shift the top 4 down to the bottom four bits
    sertxd(#b0)  ' send a number 0-16 to the PC
  endif
  goto main
Here's another option for the simulator.
Code:
#picaxe 28X1
main:
  b0 = pins
  if b0 <> b1 then ' there has been a change
    b1 = b0
    b0 = b0 /16   ' shift the top 4 down to the bottom four bits
    select b0
      case 7 sertxd("North",cr,lf)  ' (cr & lf are carriage return & line feed (new line) for the PC)
      case 3 sertxd("Northeast",cr,lf)
    endselect
'    sertxd(#b0)  ' send a number 0-16 to the PC
  endif
  goto main
If you need no more than the 4 inputs, you could do this with an 08M.
 
Last edited:

sub-bg

New Member
we just tried the program u suggested , but we keep getting unusual answers
such as -124 ,1 ,2 ....
we want number from 1-8 to be assigned to each direction in the java program. The program is working in the simulation but not in the java program.

Where do you think we r going wrong?
 

lbenson

Senior Member
>program is working in the simulation but not in the java program.

I don't know java, but what does your program look like? From the first program in post 2, you should be getting ASCII numbers in the pc, such as "7" (North), "3" (Northeast), and "14" (West)--each followed by a carriage return and line feed. If that is not what you appear to be receiving, try running just a terminal program on the PC (e.g., hyperterminal or putty) and seeing what you are are getting. The PC serial port settings should be 4800 baud, 8 bits, 1 stop bit, no parity, no flow control.
 
Last edited:

lbenson

Senior Member
slimplynth--for security reasons, javascript makes it pretty hard to write to a file. If you can use PHP, this works for me (picaxe is sending lines terminated by carriage return and line feed).
Code:
<?php // GetRx gets 315mHz xmits and echos to console
  $fh = fopen("COM7", 'r') or die("Can't open file");
  $i = 0;
  while(!feof($fh))
  {
    $line = trim(fgets($fh)); // message line, e.g., "A 22 22"
    echo $line."\n";
    if ($i++>5) break;
  }
  fclose($fh);
?
To initialize the com port I first, from a DOS prompt, issued this:

mode com7: baud=2400 parity=n data=8 stop=1 xon=off odsr=off octs=off dtr=off rts=off idsr=off
 

hippy

Technical Support
Staff member
As sub-bg would probably point out upon return to the thread; Java isn't Javascript.

The first thing to do is to start small with a simple PICAXE program, something like ...

Do
SerTxd("U")
Pause 1000
Loop

Check that can be received by the PC using the Terminal function of the Programming Editor or some other terminal emulator, then write a Java program to receive incoming data and display it. Form then on it's simply a case of growing the system until you have what you want.
 

sub-bg

New Member
We've amended the pic-axe chip, so it outputs a different value for each direction. Using AiTerm we get the correct values, however with our GUI we get wrong values, and they changes after a few tries. For example, AiTerm receives 5, our GUI receives -3,-3,-12.

ATM we just want to be able to read each time we click on the connect button; the code for the listener is;

in Controls.java
--------------------------------------------------------------------------
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Connect"))
{
m_SerialPort = new SimpleSerialJava(1);
if (m_SerialPort.isValid()) {
LblStatus.setIcon(tick);
pdirection = m_SerialPort.readByte();
System.out.println(pdirection);
}
}
--------------------------------------------------------------------------

pdirection is an int, and readByte returns an int.

in SimpleSerialJava.java
--------------------------------------------------------------------------
public int readByte() {
try {
return m_DIS.readByte();
}
catch (IOException e) {
System.out.println("### IO ERROR READING BYTE");
System.out.println("### error is: " + e);
return 0;
}
}
--------------------------------------------------------------------------

Thanks.
 
Top