Inhaltsverzeichnis

serieller Port

in der Shell

ein Beispiel:

#!/bin/sh

exec 3 <> /dev/ttyS0

echo "Command 1" >&3
Lesen Reaktion <&3
echo "$response" >> /tmp/response.log

exec 3>&-

Pascal

⇒ ISBN 3-928051-42-3

rechteckgenerator.pas

Program Rechteckgenerator;

uses CRT;

const BA = $3F8;
var   Zeit : Word;
      Taste : Char;

procedure Generator (Schleifenzeit: Word);
var   N: Word;

begin
      Port [BA+4] := 1;
      for N := 0 to Schleifenzeit do;
      Port [BA+4] := 0;
      for N := 0 to Schleifenzeit do;
end;

begin
      repeat
              write ('Schleifenzahl 0...65535 ');
              readln (Zeit);
              repeat
                      Generator (Zeit);
              until KeyPressed;
              Taste := ReadKey;
      until Taste = chr(27);
end.

schalterabfrage.pas

Program Schalterabfrage;

uses CRT;

const BA = $3F8;
var Puffer: Array[1..2000] of Byte;
      n: Word;

funktion CTS: Byte;

begin
      CTS := (Port [BA+6] AND 16) div 16;
end.

begin
      Port [BA+4] := 2;
      ClrScr;
      repeat
              for n := 1 to 2000 do Puffer[n] := CTS;
              for n := 1 to 2000 do write (Puffer[n]);
              delay (1000);
      until KeyPressed;
      delay (2000)
end.

C/C++

http://www.easysw.com/%7emike/serial/serial.html#2_5:

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

/*
 * 'open_port()' - Open serial port 1.
 *
 * Returns the file descriptor on success or -1 on error.
 */

int
open_port(void)
{
    int fd; /* File descriptor for the port */
    // seriellen USB-Port öffnen
    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)
    {
    /*
     * Could not open the port.
     */

        perror("open_port: Unable to open /dev/ttyS0 - ");
    }
    else
        fcntl(fd, F_SETFL, 0);

    return (fd);
}
  
// Daten in den seriellen USB-Port schreiben
n = write(fd, "ATZ\r", 4);
if (n < 0)
    fputs("write() of 4 bytes failed!\n", stderr);

// Daten aus dem seriellen USB-Port lesen
fcntl(fd, F_SETFL, FNDELAY);
//
// The FNDELAY option causes the read function to return 0
// if no characters are available on the port.
// To restore normal (blocking) behavior,
// call fcntl() without the FNDELAY option:
//fcntl(fd, F_SETFL, 0);

// seriellen USB-Port schließen
close(fd);

Konfiguration des seriellen Ports → No parity (8N1):

options.c_cflag &= ~PARENB
options.c_cflag &= ~CSTOPB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;