Hej alle
Jeg har udarbejdet flg. kode (noget af det har jeg fået foræret), men
det driller mig helt enormt!
Jeg skal læse en unsigned værdi, men kan kun læse værdier op til 127,
når en værdi angiveligt burde være -1 er den istedet 127 - hvorfor?
-1 bliver til 127
-2 bliver til 126
osv.
Hvis jeg læser med logserial får jeg værdier på over 200, så jeg vil æde
min gamle hat på at det er mit program der fejler:
-----
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <getopt.h>
#include <stdlib.h>
#define VERSION "0.0.1"
#define DEFAULT_PORT "/dev/ttyUSB0"
void set_port_opts(int fd);
int open_port(char *com_port);
void close_port(int fd);
int xfer(int fd, char *file_name);
void go(int fd);
int main(int argc, char **argv) {
int fd;
int c;
int ret;
int go_after_load = 0;
char *com_port = DEFAULT_PORT;
char *file_name;
while (1) {
static struct option long_options[] = {
{"port", required_argument, NULL, 'p'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"go", no_argument, NULL, 'g'},
{NULL, 0, NULL, 0}
};
int options_index = 0;
c = getopt_long(argc, argv, "p:vhg", long_options, &options_index);
if (c == -1)
break;
switch (c) {
case 'v':
puts("TinyDigScope " VERSION);
return 0;
case 'p':
com_port = optarg;
break;
case 'g':
go_after_load = 1;
break;
case 'h':
printf("Usage: tinyDigScope [OPTIONS] FILE\n"
"Fetches oscilloscope samples from the TinyDigScope\n"
"\n"
" -p --port=DEVICE COM port device to use (Default:
"DEFAULT_PORT")\n"
" -g --go go-after-load (TM)\n"
" -h --help Display this message\n"
" -v --version Display version info\n"
" FILE Executable to download\n"
"\n");
return 0;
default:
fprintf(stderr, "Invalid argument `%s'\n"
"Try `%s --help' for more info\n", argv[optind], argv[0]);
return -1;
}
}
if (optind < argc)
file_name = argv[optind++];
else {
fprintf(stderr, "Missing arguments\n"
"Try `%s --help' for more info\n", argv[0]);
return -1;
}
fd = open_port(com_port);
set_port_opts(fd);
ret = xfer(fd, file_name);
//if (ret == 0 && go_after_load == 1)
// go(fd);
close_port(fd);
return ret;
}
void set_port_opts(int fd) {
struct termios options;
tcgetattr(fd, &options);
/* Set BAUD rate to 38400 */
cfsetispeed(&options, B38400);
cfsetospeed(&options, B38400);
options.c_cflag |= (CLOCAL | CREAD);
/* 8N1 no hardware flowcontrol */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS;
options.c_iflag |= (INPCK | ISTRIP);
/* raw input */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/* Use XON/XOFF flowcontrol */
//options.c_iflag |= (IXON | IXOFF);
/* raw output */
options.c_oflag &= ~OPOST;
tcsetattr(fd, TCSANOW, &options);
}
int open_port(char *com_port) {
int fd;
fd = open(com_port, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1)
fprintf(stderr, "Unable to open %s - %s\n", com_port, strerror(errno));
else {
fcntl(fd, F_SETFL, 0);
tcflush(fd, TCIOFLUSH);
}
return fd;
}
void close_port(int fd) {
tcflush(fd, TCIOFLUSH);
if (close(fd) != 0)
fprintf(stderr, "Error closing file - %s\n", strerror(errno));
}
int xfer(int fd, char *file_name) {
int i,j,n;
FILE *stream;
signed char buf[127];
signed char input[2048];
// Clear buffer
for (i = 0; i < sizeof(buf); i++) {
buf[i] = 0;
}
for (i = 0; i < sizeof(input); i++) {
input[i] = 0;
}
// Flush input/output-buffer
fputs("Flush I/O buffer\n", stderr);
tcflush(fd, TCIOFLUSH);
// Receive input from device
fputs("Receiving 2048 bytes from device \n", stderr);
n = 0;
i = 0;
while (1) { //(i < sizeof(input) - 24) {
n = read(fd, buf, sizeof(buf));
if (n > 0) {
i += n;
for (j = 0; j < n; j++) {
//input[i-n+j] = buf[j];
fprintf(stderr, "%i ", (signed int) buf[j]);
}
} else {
fputs("Error: trying to read again \n", stderr);
}
}
/*
fprintf(stderr, "Read %i bytes from serial port\n", sizeof(input));
fprintf(stderr, "Saved %i bytes to file %s \n", sizeof(input),
file_name);
stream = fopen(file_name, "w");
for (i = 0; i < sizeof(input); i++) {
fprintf(stream, "%i \n", (int) input[i]); //(input[i] ^ 127));
}
fclose(stream);
*/
puts("");
return 0;
}
----
hvad er der dog galt?
Med venlig hilsen
Preben Holm
(posted i både dk.ebd.system.unix og dk.edb.programmering.c)
|