044
03.10.2024, 20:28 Uhr
Ordoban
|
ich hatte meine Erika am PC über einen MAX232 Pegelwandler angeschlossen. Für den Fall, dass es jemandem hilft, hier mein kurzes C-Programm für Linux:
Quellcode: | // C library headers #include <stdio.h> #include <string.h>
// Linux headers #include <fcntl.h> // Contains file controls like O_RDWR #include <errno.h> // Error integer and strerror() function #include <termios.h> // Contains POSIX terminal control definitions #include <unistd.h> // write(), read(), close() #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include <linux/serial.h>
unsigned char convert1[256] = { //x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x78, 0x00, 0x00, // 0x 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 1x 0x71, 0x42, 0x43, 0x00, 0x48, 0x04, 0x02, 0x29, 0x1d, 0x1f, 0x00, 0x25, 0x64, 0x62, 0x63, 0x40, // 2x ' !".$%&'().+,-./' 0x0d, 0x11, 0x10, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x13, 0x3b, 0x00, 0x2e, 0x00, 0x35, // 3x '0123456789:;.=.?' 0x00, 0x30, 0x18, 0x20, 0x14, 0x34, 0x3e, 0x1c, 0x12, 0x21, 0x32, 0x24, 0x2c, 0x16, 0x2a, 0x1e, // 4x '.ABCDEFGHIJKLMNO' 0x2f, 0x1a, 0x36, 0x33, 0x37, 0x28, 0x22, 0x2d, 0x26, 0x31, 0x38, 0x00, 0x00, 0x00, 0x19, 0x01, // 5x 'PQRSTUVWXYZ...^_' 0x2b, 0x61, 0x4e, 0x57, 0x53, 0x5a, 0x49, 0x60, 0x55, 0x05, 0x4b, 0x50, 0x4d, 0x4a, 0x5c, 0x5e, // 6x '`abcdefghijklmno' 0x5b, 0x52, 0x59, 0x58, 0x56, 0x5d, 0x4f, 0x4c, 0x5f, 0x51, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, // 7x 'pqrstuvwxyz.....' 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 8x 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9x 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ax 0x00, 0x00, 0x15, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // bx 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cx 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x47, // dx 0x00, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ex 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x67, 0x00, 0x00, 0x00};// fx
void punchchar(unsigned char a, int device) { unsigned char b; b=a; int status; usleep(10000); do { ioctl(device, TIOCMGET, &status); } while ((status & TIOCM_CTS) == 0); write(device, &b, 1); do { ioctl(device, TIOCMGET, &status); } while ((status & TIOCM_CTS) == 0); }
int main() { // Open the serial port. Change device path as needed (currently set to an standard FTDI USB-UART cable type device) int serial_port = open("/dev/ttyS0", O_RDWR);
// Create new termios struct, we call it 'tty' for convention struct termios tty;
// Read in existing settings, and handle any error if(tcgetattr(serial_port, &tty) != 0) { printf("Error %i from tcgetattr: %s\n", errno, strerror(errno)); return 1; }
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common) tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common) tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size tty.c_cflag |= CS8; // 8 bits per byte (most common) tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common) tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
tty.c_lflag &= ~ICANON; tty.c_lflag &= ~ECHO; // Disable echo tty.c_lflag &= ~ECHOE; // Disable erasure tty.c_lflag &= ~ECHONL; // Disable new-line echo tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars) tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed // tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX) // tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX)
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received. tty.c_cc[VMIN] = 0;
// Set in/out baud rate to be 9600 cfsetispeed(&tty, B1200); cfsetospeed(&tty, B1200);
// Save tty settings, also checking for error if (tcsetattr(serial_port, TCSANOW, &tty) != 0) { printf("Error %i from tcsetattr: %s\n", errno, strerror(errno)); return 1; }
unsigned char a;
while (1) { int i = read(0, &a, 1); if (i>0) { a = convert1[a]; if (a>0) punchchar(a,serial_port); } }
close(serial_port); return 0; // success };
|
-- Gruß Stefan |