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

Subversion Repositories logicprobe

[/] [logicprobe/] [trunk/] [src/] [pc/] [receive.c] - Blame information for rev 8

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

Line No. Rev Author Line
1 4 hellwig
/*
2
 * receive.c -- LogicProbe serial line receiver
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
#include <fcntl.h>
11
#include <unistd.h>
12
#include <termios.h>
13
 
14
 
15
#define SERIAL_PORT     "/dev/ttyS0"
16
 
17
 
18
static int debug = 0;
19
 
20
static FILE *diskFile = NULL;
21
static int sfd = 0;
22
static struct termios origOptions;
23
static struct termios currOptions;
24
 
25
 
26
void serialClose(void);
27
 
28
 
29
void error(char *fmt, ...) {
30
  va_list ap;
31
 
32
  va_start(ap, fmt);
33
  printf("Error: ");
34
  vprintf(fmt, ap);
35
  printf("\n");
36
  va_end(ap);
37
  if (diskFile != NULL) {
38
    fclose(diskFile);
39
    diskFile = NULL;
40
  }
41
  if (sfd != 0) {
42
    serialClose();
43
    sfd = 0;
44
  }
45
  exit(1);
46
}
47
 
48
 
49
void serialOpen(void) {
50
  sfd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
51
  if (sfd == -1) {
52
    error("cannot open serial port '%s'", SERIAL_PORT);
53
  }
54
  tcgetattr(sfd, &origOptions);
55
  currOptions = origOptions;
56
  cfsetispeed(&currOptions, B38400);
57
  cfsetospeed(&currOptions, B38400);
58
  currOptions.c_cflag |= (CLOCAL | CREAD);
59
  currOptions.c_cflag &= ~PARENB;
60
  currOptions.c_cflag &= ~CSTOPB;
61
  currOptions.c_cflag &= ~CSIZE;
62
  currOptions.c_cflag |= CS8;
63
  currOptions.c_cflag &= ~CRTSCTS;
64
  currOptions.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
65
  currOptions.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK);
66
  currOptions.c_iflag &= ~(INPCK | ISTRIP | INLCR | IGNCR | ICRNL);
67
  currOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
68
  currOptions.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET | OFILL);
69
  tcsetattr(sfd, TCSANOW, &currOptions);
70
}
71
 
72
 
73
void serialClose(void) {
74
  tcsetattr(sfd, TCSANOW, &origOptions);
75
  close(sfd);
76
}
77
 
78
 
79
int serialSnd(unsigned char b) {
80
  int n;
81
 
82
  n = write(sfd, &b, 1);
83
  return n == 1;
84
}
85
 
86
 
87
int serialRcv(unsigned char *bp) {
88
  int n;
89
 
90
  n = read(sfd, bp, 1);
91
  return n == 1;
92
}
93
 
94
 
95
int main(int argc, char *argv[]) {
96
  unsigned char b;
97
  int i, j;
98
 
99
  if (argc != 2) {
100
    printf("Usage: %s <data_file>\n", argv[0]);
101
    exit(1);
102
  }
103
  diskFile = fopen(argv[1], "wb");
104
  if (diskFile == NULL) {
105
    error("cannot open data file %s for write", argv[1]);
106
  }
107
  serialOpen();
108
  serialRcv(&b);
109
  for (i = 0; i < 512; i++) {
110
    if (debug) {
111
      printf("%03d:  ", i);
112
    }
113
    for (j = 0; j < 16; j++) {
114
      while (!serialRcv(&b)) ;
115
      if (fwrite(&b, 1, 1, diskFile) != 1) {
116
        error("cannot write to data file %s", argv[1]);
117
      }
118
      if (debug) {
119
        printf("%02X  ", b);
120
      }
121
    }
122
    if (debug) {
123
      printf("\n");
124
    }
125
  }
126
  if (diskFile != NULL) {
127
    fclose(diskFile);
128
    diskFile = NULL;
129
  }
130
  if (sfd != 0) {
131
    serialClose();
132
    sfd = 0;
133
  }
134
  return 0;
135
}

powered by: WebSVN 2.1.0

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