OpenCores
URL https://opencores.org/ocsvn/diogenes/diogenes/trunk

Subversion Repositories diogenes

[/] [diogenes/] [trunk/] [sim/] [send.c] - Blame information for rev 172

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 172 fellnhofer
#include <stdio.h>   /* Standard input/output definitions */
2
#include <string.h>  /* String function definitions */
3
#include <unistd.h>  /* UNIX standard function definitions */
4
#include <fcntl.h>   /* File control definitions */
5
#include <errno.h>   /* Error number definitions */
6
#include <termios.h> /* POSIX terminal control definitions */
7
 
8
 
9
int getbaud(int fd) {
10
        struct termios termAttr;
11
        int inputSpeed = -1;
12
        speed_t baudRate;
13
        tcgetattr(fd, &termAttr);
14
        /* Get the input speed.                              */
15
        baudRate = cfgetispeed(&termAttr);
16
        switch (baudRate) {
17
                case B0:      inputSpeed = 0; break;
18
                case B50:     inputSpeed = 50; break;
19
                case B110:    inputSpeed = 110; break;
20
                case B134:    inputSpeed = 134; break;
21
                case B150:    inputSpeed = 150; break;
22
                case B200:    inputSpeed = 200; break;
23
                case B300:    inputSpeed = 300; break;
24
                case B600:    inputSpeed = 600; break;
25
                case B1200:   inputSpeed = 1200; break;
26
                case B1800:   inputSpeed = 1800; break;
27
                case B2400:   inputSpeed = 2400; break;
28
                case B4800:   inputSpeed = 4800; break;
29
                case B9600:   inputSpeed = 9600; break;
30
                case B19200:  inputSpeed = 19200; break;
31
                case B38400:  inputSpeed = 38400; break;
32
                case B115200:  inputSpeed = 115200; break;
33
        }
34
        return inputSpeed;
35
}
36
 
37
 
38
int fd;
39
 
40
int initport(int fd) {
41
        struct termios options;
42
        // Get the current options for the port...
43
        tcgetattr(fd, &options);
44
        // Set the baud rates to 19200...
45
        cfsetispeed(&options, B115200);
46
        cfsetospeed(&options, B115200);
47
        // Enable the receiver and set local mode...
48
        options.c_cflag |= (CLOCAL | CREAD);
49
 
50
        options.c_cflag &= ~PARENB;
51
        options.c_cflag &= ~CSTOPB;
52
        options.c_cflag &= ~CSIZE;
53
        options.c_cflag |= CS8;
54
 
55
        // Set the new options for the port...
56
        tcsetattr(fd, TCSANOW, &options);
57
        return 1;
58
}
59
 
60
int main(int argc, char **argv)
61
{
62
        int ch, count=0;
63
 
64
        fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
65
        if (fd == -1) {
66
                perror("open_port: Unable to open /dev/ttyUSB0 - ");
67
                return 1;
68
        } else {
69
                fcntl(fd, F_SETFL, 0);
70
        }
71
 
72
        printf("baud=%d\n", getbaud(fd));
73
        initport(fd);
74
        printf("baud=%d\n", getbaud(fd));
75
 
76
 
77
        while(((ch=getchar())!=-1) || count<1024*8) {
78
          if(ch==-1) ch=0x00;
79
          usleep(1000);
80
          write(fd, &ch, 1);
81
          printf("%02x ", ch);
82
          fflush(stdout);
83
          count++;
84
        }
85
 
86
        puts("\n\n=>\n\n");
87
        usleep(500000);
88
 
89
        while(read(fd, &ch, 1)>0) {
90
          printf("%02x ", (unsigned char) ch);
91
          fflush(stdout);
92
        }
93
 
94
        close(fd);
95
        return 0;
96
}
97
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.