Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
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 all replies
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 motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: Sending one byte to the parallel port
« Reply #2 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 motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: Sending one byte to the parallel port
« Reply #3 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 motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: Sending one byte to the parallel port
« Reply #4 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 all replies
Re: Sending one byte to the parallel port
« Reply #5 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 motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: Sending one byte to the parallel port
« Reply #6 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 motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: Sending one byte to the parallel port
« Reply #7 on: May 23, 2006, 09:58:16 PM »
Now that's more like it - Basic should be much easier :-) I assume I can compile with this?

--
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 all replies
Re: Sending one byte to the parallel port
« Reply #8 on: May 23, 2006, 10:21:55 PM »
Cool, gonna try it tomorrow :-) Can I just paste the code you gave me in to Blitz and execute it, or do I have to include stuff?

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