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

Subversion Repositories logicprobe

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

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

powered by: WebSVN 2.1.0

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