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

Subversion Repositories c16

[/] [c16/] [trunk/] [tty.cc] - Blame information for rev 27

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

Line No. Rev Author Line
1 2 jsauermann
 
2
#include <assert.h>
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
#include <conio.h>
7
#include <wtypes.h>
8
 
9
void download(const char * filename, HANDLE comm)
10
{
11
char buffer[1000];
12
 
13
int total = 0;
14
 
15
FILE * in = fopen(filename, "r");
16
   assert(in);
17
 
18
   for (;;)
19
       {
20
         const int len = fread(buffer, 1, sizeof(buffer), in);
21
         if (len == 0)   break;
22
         total += len;
23
         printf("%d", total);
24
 
25
         DWORD written = 0;
26
         WriteFile(comm, buffer, len, &written, 0);
27
         if (written != len)
28
            {
29
              errno = GetLastError();
30
              perror("WriteFile() failed");
31
              return;
32
            }
33
         printf(" ");
34
       }
35
 
36
   printf("\nTotal of %d (0x%X) bytes downloaded\n", total, total);
37
   fclose(in);
38
}
39
 
40
int main(int argc, char * argv[])
41
{
42
const char * filename = "rtos.ihx";
43
   if (argc > 1)   filename = argv[1];
44
 
45
HANDLE comm = CreateFile("COM1",
46
                         GENERIC_READ | GENERIC_WRITE,
47
                         0,
48
                         0,
49
                         OPEN_EXISTING,
50
                         0,
51
                         0);
52
 
53
   assert(comm);
54
 
55
   if (!SetupComm(comm, 1024, 1024))
56
      {
57
        errno = GetLastError();
58
        perror("SetupComm() failed");
59
        fprintf(stderr,
60
                "Can't open COM1: (probably used by another process)\n");
61
        return 1;
62
      }
63
 
64
DCB dcb;
65
   if (!GetCommState(comm, &dcb))
66
      {
67
        errno = GetLastError();
68
        perror("GetCommState() failed");
69
        return 1;
70
      }
71
 
72
   dcb.BaudRate        = CBR_115200;
73
   dcb.ByteSize        = 8;
74
   dcb.Parity          = NOPARITY;
75
   dcb.StopBits        = ONESTOPBIT;
76
   dcb.fOutxCtsFlow    = 0;
77
   dcb.fOutxDsrFlow    = 0;
78
   dcb.fDsrSensitivity = 0;
79
   dcb.fDtrControl     = DTR_CONTROL_DISABLE;
80
   dcb.fRtsControl     = RTS_CONTROL_DISABLE;
81
   dcb.fOutX           = 0;
82
   dcb.fInX            = 0;
83
   dcb.fNull           = 0;
84
   dcb.fAbortOnError   = 0;
85
 
86
   if (!SetCommState(comm, &dcb))
87
      {
88
        errno = GetLastError();
89
        perror("SetCommState() failed");
90
        return 1;
91
      }
92
 
93
COMMTIMEOUTS touts = { MAXDWORD, 0, 50, 0, 0 };
94
 
95
   if (!SetCommTimeouts(comm, &touts))
96
      {
97
        errno = GetLastError();
98
        perror("SetCommTimeouts() failed");
99
        return 1;
100
      }
101
 
102
char buffer[1000];
103
int std = fileno(stdin);
104
 
105
   for (;;)
106
       {
107
         DWORD read    = 0;
108
         DWORD written = 0;
109
         ReadFile(comm, buffer, sizeof(buffer), &read, 0);
110
         char c;
111
 
112
         for (int i = 0; i < read; i++)
113
             {
114
               c = buffer[i];
115
               if      (c == '\r')   putchar(c);
116
               else if (c == '\n')   putchar(c);
117
               else if (c < ' ')     printf("^%c", c + 0x40);
118
               else if (c < 0x7F)    putchar(c);
119
               else                  printf("\\%2.2X", c & 0xFF);
120
             }
121
 
122
         if (kbhit())
123
            {
124
              c = _getch();
125
              if (c == 0x1B)   break;
126
              if (c == 0x0C)
127
                 {
128
                   download(filename, comm);
129
                   continue;
130
                 }
131
              WriteFile(comm, &c, 1, &written, 0);
132
            }
133
       }
134
}

powered by: WebSVN 2.1.0

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