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

Subversion Repositories diogenes

[/] [diogenes/] [trunk/] [sim/] [sendb.c] - Blame information for rev 236

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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