Welcome, Guest. Please login or register.

Author Topic: Sending one byte to the parallel port  (Read 4328 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Sending one byte to the parallel port
« on: May 23, 2006, 04:26:21 PM »
I have a parallel port relay board which I want to control with my Amiga. I have seen applications on Aminet for controlling these, but I want something I can run from the CLI. The application itself (I think) just sends a single byte of data to the port, with each bit representing one of the 8 relays. So I need a way to send a single byte to the Amiga's parallel port.

I have the source code of the Linux version of the relay board control software, and have found what I believe to be the portion of the code which sends the actual data to the port:

void out_byte(int port, unsigned char byte)
{
  off_t s;
  int r;

  s = lseek(dev_port_fd, port, 0);
  if (s < 0) perror("lseek");
  else if (s != port)
    fprintf(stderr, "hmmm: seeking to %d, went to %ld.\n", port, (long)s);

  r = write(dev_port_fd, &byte, 1);
  if (r != 1)
  {
    fprintf(stderr, "hmmm: write returned %d\n", r);
    if (r < 0) perror("write");
  }
}


What I don't know is how to adapt this for the Amiga. Hopefully someone can point me in the right direction.

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Sending one byte to the parallel port
« Reply #1 on: May 23, 2006, 05:14:31 PM »
Ok I found pario on Aminet, and have cobbled together the following from the example code:

#include    "pardemo.h"

void main(void)
{
   UBYTE data;            /* Port I/O data */
   LONG  error = 0;       /* Error code on exit */
   #ifdef  AZTEC_C
   #endif

   getport();

   portdir(1);
   datatype = HEX;        /* Only write if at least one bit */
   data = GetData(choice);   /* dir set to output */
   wrport(data);

   freeport();
}


I have no idea if this would work. Can anybody who is familiar with the pario routines let me know if this looks ok?

If this does work, I need to replace the getdata() command with something which will read the data variable from an argument. I'll cross that bridge once I've got port routines working :-)

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline Kronos

  • Resident blue troll
  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4017
    • Show only replies by Kronos
    • http://www.SteamDraw.de
Re: Sending one byte to the parallel port
« Reply #2 on: May 23, 2006, 05:23:18 PM »
Want something reeeeeaaaaaally simple ?

Create a file only containing 1 byte (you may need a hex-editor to manipulate that byte), and then type:

copy myfile to par:

*clack* *clack*  :-D

Or if in C (untested but should work, and if with every compiler):

char c = 0xf3;
FILE *file;

file = fopen("PAR:","w");
putc(file,c);
fclose(file);

Done from memory, typos or under international copyright
1. Make an announcment.
2. Wait a while.
3. Check if it can actually be done.
4. Wait for someone else to do it.
5. Start working on it while giving out hillarious progress-reports.
6. Deny that you have ever announced it
7. Blame someone else
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Sending one byte to the parallel port
« Reply #3 on: May 23, 2006, 05:48:32 PM »
Quote
Kronos wrote:
Create a file only containing 1 byte (you may need a hex-editor to manipulate that byte), and then type:

copy myfile to par:

That's much easier - but I would need a way of creating the file from the command line, rather than from a hex editor. Any thoughts?

Quote
Kronos wrote:
Or if in C (untested but should work, and if with every compiler):

char c = 0xf3;
FILE *file;

file = fopen("PAR:","w");
putc(file,c);
fclose(file);

Looks like a C version of the same thing? Much simpler than what I wrote! Do I need to #include anything for this to work, or can I just put it in my main void? Also, are there any free C compilers for the Amiga?

Thanks!

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline Kronos

  • Resident blue troll
  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4017
    • Show only replies by Kronos
    • http://www.SteamDraw.de
Re: Sending one byte to the parallel port
« Reply #4 on: May 23, 2006, 05:58:46 PM »
You'll need to atleast include stdio.h.

GCC is available for Amiga, you may need to google a bit.
1. Make an announcment.
2. Wait a while.
3. Check if it can actually be done.
4. Wait for someone else to do it.
5. Start working on it while giving out hillarious progress-reports.
6. Deny that you have ever announced it
7. Blame someone else
 

Offline Kronos

  • Resident blue troll
  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4017
    • Show only replies by Kronos
    • http://www.SteamDraw.de
Re: Sending one byte to the parallel port
« Reply #5 on: May 23, 2006, 06:01:28 PM »
Oh, and about that other idea:

"echo "blabla" >par:" would also work (you may need to replace "blabla" with some escape-sequence in order to send unprintable bit-combination (and no I don't know the next best thing about escape-sequences).
1. Make an announcment.
2. Wait a while.
3. Check if it can actually be done.
4. Wait for someone else to do it.
5. Start working on it while giving out hillarious progress-reports.
6. Deny that you have ever announced it
7. Blame someone else
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Sending one byte to the parallel port
« Reply #6 on: May 23, 2006, 08:10:00 PM »
I tried your idea of creating a file and copying it to PAR:. I created a file in a hex editor containing just the byte "00" and saved it. I then copied it to PAR:, and all the relays turned off. Unfortunately, the copy command never returned and I couldn't break it, which locked the parallel port. This happened every time I tried it. The only way to allow further access to the parallel port was to reboot. I'm guessing the C programme would cause the same problem as it is doing the same thing by copying a byte to PAR:.

Do I perhaps need to pad the data I send to the port, so the first byte controls the relays and the rest is padding? If so, then how much data does the port read at a time?

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline Kronos

  • Resident blue troll
  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4017
    • Show only replies by Kronos
    • http://www.SteamDraw.de
Re: Sending one byte to the parallel port
« Reply #7 on: May 23, 2006, 08:40:29 PM »
CRTL-C should do the trick.

You may also try a bigger file, if it just switches once the HW is missing some sort of handshake, if it goes to the end it's copy which doesn't return cleanly.

In that case you could try other commands (echo,type etc) which can also be used to send files to par:

BUT in I personaly send several times files to ser: that way without copy getting blocked (unless the receiving computer wasn't listening, in which case CTRL-C worked just fine).
1. Make an announcment.
2. Wait a while.
3. Check if it can actually be done.
4. Wait for someone else to do it.
5. Start working on it while giving out hillarious progress-reports.
6. Deny that you have ever announced it
7. Blame someone else
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Sending one byte to the parallel port
« Reply #8 on: May 23, 2006, 08:41:36 PM »
I tried CTRL-C but it didn't break the command. I'll try echo instead and let you know if it works.

Thanks

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Sending one byte to the parallel port
« Reply #9 on: May 23, 2006, 09:08:26 PM »
Echo did the same thing :-( The command failed to return, which locked PAR:. CTRL-C didn't work, neither did opening a new shell and using break to end the process.

I think I need to write an app in C (or ASM???) with proper routines to open the port, send a byte, and then close the port. Unfortunately I have no experience in C or ASM, though I learn languages quickly. Guess I'll have to give it a try :-)

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline KThunder

  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 1509
    • Show only replies by KThunder
Re: Sending one byte to the parallel port
« Reply #10 on: May 23, 2006, 09:11:16 PM »
i started working up some schematics for a parallel port relay setup but i was setting up partial 8bit decoding for more relays.
where can you get an 8 relay one or schematics for one. i was using relays for 12volt motor control mostly for robotics and tinkering.
Oh yeah?!?
Well your stupid bit is set,
and its read only!
(my best geek putdown)
 

Offline Tigger

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 1890
    • Show only replies by Tigger
Re: Sending one byte to the parallel port
« Reply #11 on: May 23, 2006, 09:39:43 PM »
Not sure I can get to a working Amiga to make this for you, but the RKMs have a demo of talking to the parallel port that should help you out.  For what you are trying to do, I would use a command variable input which would allow you to put 00 through FF as the input, convert it to binary and sent the byte out the port.  The advantage of that is you can use it from a script or from Arexx with the tool itself being pretty simple.    
     -Tig
Well you know I am scottish, so I like sheep alot.
     -Fleecy Moss, Gateway 2000 show
 

Offline Kronos

  • Resident blue troll
  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4017
    • Show only replies by Kronos
    • http://www.SteamDraw.de
Re: Sending one byte to the parallel port
« Reply #12 on: May 23, 2006, 09:40:52 PM »
@motorollin
Have yout tried  this  ?


Mom and Dad, I love you, please sell me to Paris Hilton.
-Kronos  ;-)
1. Make an announcment.
2. Wait a while.
3. Check if it can actually be done.
4. Wait for someone else to do it.
5. Start working on it while giving out hillarious progress-reports.
6. Deny that you have ever announced it
7. Blame someone else
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: Sending one byte to the parallel port
« Reply #13 on: May 23, 2006, 09:47:39 PM »
Hi, just had a look at it. It appears to be a Workbench app rather than CLI. I need a CLI app so I can use it in a script.

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline Doobrey

  • Hero Member
  • *****
  • Join Date: Oct 2002
  • Posts: 1876
    • Show only replies by Doobrey
    • http://www.doobreynet.co.uk
Re: Sending one byte to the parallel port
« Reply #14 on: May 23, 2006, 09:54:20 PM »
Quote

motorollin wrote:
 Unfortunately I have no experience in C or ASM, though I learn languages quickly. Guess I'll have to give it a try :-)


AmiBlitz to the rescue  ;-)

 ;-- Parallel port Blitz example..
 ;-- No more stupid Poking/peeking...
 ;-- remember to use all.res !
NEWTYPE .byte
 b.b
End NEWTYPE

*PPort_Dir.byte  = $BFE301
*PPort_Bits.byte = $BFE101

If AllocMiscResource_(#MR_PARALLELPORT,"MyAppName") = 0

  *PPort_Dir\b= %11111111   ; set all bits direction to output

  ;-- Ready to rock and roll..

  *PPort_Bits\b=%01010101   ; set the little buggers

  FreeMiscResource_ #MR_PARALLELPORT
Else
  NPrint "Crap, somethings already got the port :("
EndIf

End
On schedule, and suing