/*
|
/*
|
* sertest.c -- serial line test program
|
* sertest.c -- serial line test program
|
*/
|
*/
|
|
|
|
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
#include <stdarg.h>
|
#include <stdarg.h>
|
#include <fcntl.h>
|
#include <fcntl.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#include <termios.h>
|
#include <termios.h>
|
|
|
|
|
#define SERIAL_PORT "/dev/ttyS0"
|
|
|
|
#define NUM_TRIES 10
|
#define NUM_TRIES 10
|
|
|
#define SYN 0x16
|
#define SYN 0x16
|
#define ACK 0x06
|
#define ACK 0x06
|
|
|
|
|
static FILE *diskFile = NULL;
|
static FILE *diskFile = NULL;
|
static int sfd = 0;
|
static int sfd = 0;
|
static struct termios origOptions;
|
static struct termios origOptions;
|
static struct termios currOptions;
|
static struct termios currOptions;
|
static int errors;
|
static int errors;
|
|
|
|
|
void serialClose(void);
|
void serialClose(void);
|
|
|
|
|
void error(char *fmt, ...) {
|
void error(char *fmt, ...) {
|
va_list ap;
|
va_list ap;
|
|
|
va_start(ap, fmt);
|
va_start(ap, fmt);
|
printf("Error: ");
|
printf("Error: ");
|
vprintf(fmt, ap);
|
vprintf(fmt, ap);
|
printf("\n");
|
printf("\n");
|
va_end(ap);
|
va_end(ap);
|
if (diskFile != NULL) {
|
if (diskFile != NULL) {
|
fclose(diskFile);
|
fclose(diskFile);
|
diskFile = NULL;
|
diskFile = NULL;
|
}
|
}
|
if (sfd != 0) {
|
if (sfd != 0) {
|
serialClose();
|
serialClose();
|
sfd = 0;
|
sfd = 0;
|
}
|
}
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
|
|
void serialOpen(void) {
|
void serialOpen(char *serialPort) {
|
sfd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
|
sfd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY);
|
if (sfd == -1) {
|
if (sfd == -1) {
|
error("cannot open serial port '%s'", SERIAL_PORT);
|
error("cannot open serial port '%s'", serialPort);
|
}
|
}
|
tcgetattr(sfd, &origOptions);
|
tcgetattr(sfd, &origOptions);
|
currOptions = origOptions;
|
currOptions = origOptions;
|
cfsetispeed(&currOptions, B38400);
|
cfsetispeed(&currOptions, B38400);
|
cfsetospeed(&currOptions, B38400);
|
cfsetospeed(&currOptions, B38400);
|
currOptions.c_cflag |= (CLOCAL | CREAD);
|
currOptions.c_cflag |= (CLOCAL | CREAD);
|
currOptions.c_cflag &= ~PARENB;
|
currOptions.c_cflag &= ~PARENB;
|
currOptions.c_cflag &= ~CSTOPB;
|
currOptions.c_cflag &= ~CSTOPB;
|
currOptions.c_cflag &= ~CSIZE;
|
currOptions.c_cflag &= ~CSIZE;
|
currOptions.c_cflag |= CS8;
|
currOptions.c_cflag |= CS8;
|
currOptions.c_cflag &= ~CRTSCTS;
|
currOptions.c_cflag &= ~CRTSCTS;
|
currOptions.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
|
currOptions.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
|
currOptions.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK);
|
currOptions.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK);
|
currOptions.c_iflag &= ~(INPCK | ISTRIP | INLCR | IGNCR | ICRNL);
|
currOptions.c_iflag &= ~(INPCK | ISTRIP | INLCR | IGNCR | ICRNL);
|
currOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
|
currOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
|
currOptions.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
|
currOptions.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
|
tcsetattr(sfd, TCSANOW, &currOptions);
|
tcsetattr(sfd, TCSANOW, &currOptions);
|
}
|
}
|
|
|
|
|
void serialClose(void) {
|
void serialClose(void) {
|
tcsetattr(sfd, TCSANOW, &origOptions);
|
tcsetattr(sfd, TCSANOW, &origOptions);
|
close(sfd);
|
close(sfd);
|
}
|
}
|
|
|
|
|
int serialSnd(unsigned char b) {
|
int serialSnd(unsigned char b) {
|
int n;
|
int n;
|
|
|
n = write(sfd, &b, 1);
|
n = write(sfd, &b, 1);
|
return n == 1;
|
return n == 1;
|
}
|
}
|
|
|
|
|
int serialRcv(unsigned char *bp) {
|
int serialRcv(unsigned char *bp) {
|
int n;
|
int n;
|
|
|
n = read(sfd, bp, 1);
|
n = read(sfd, bp, 1);
|
return n == 1;
|
return n == 1;
|
}
|
}
|
|
|
|
|
void block(void) {
|
void block(void) {
|
unsigned char src[1000];
|
unsigned char src[1000];
|
unsigned char dst[1000];
|
unsigned char dst[1000];
|
unsigned char *p, *q;
|
unsigned char *p, *q;
|
int i;
|
int i;
|
|
|
for (i = 0; i < 1000; i++) {
|
for (i = 0; i < 1000; i++) {
|
src[i] = rand();
|
src[i] = rand();
|
}
|
}
|
p = src;
|
p = src;
|
q = dst;
|
q = dst;
|
while (1) {
|
while (1) {
|
if (p != &src[1000] && serialSnd(*p)) {
|
if (p != &src[1000] && serialSnd(*p)) {
|
p++;
|
p++;
|
}
|
}
|
if (q != &dst[1000] && serialRcv(q)) {
|
if (q != &dst[1000] && serialRcv(q)) {
|
q++;
|
q++;
|
}
|
}
|
if (q == &dst[1000]) {
|
if (q == &dst[1000]) {
|
break;
|
break;
|
}
|
}
|
}
|
}
|
for (i = 0; i < 1000; i++) {
|
for (i = 0; i < 1000; i++) {
|
if (((src[i] + 0x5C) & 0xFF) != dst[i]) {
|
if (((src[i] + 0x5C) & 0xFF) != dst[i]) {
|
errors++;
|
errors++;
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
|
|
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
|
char *serialPort;
|
int i;
|
int i;
|
|
|
if (argc != 1) {
|
if (argc != 2) {
|
printf("Usage: %s\n", argv[0]);
|
printf("Usage: %s <serial port>\n", argv[0]);
|
exit(1);
|
exit(1);
|
}
|
}
|
serialOpen();
|
serialPort = argv[1];
|
|
serialOpen(serialPort);
|
errors = 0;
|
errors = 0;
|
for (i = 1; i <= 100; i++) {
|
for (i = 1; i <= 100; i++) {
|
block();
|
block();
|
if (i % 10 == 0) {
|
if (i % 10 == 0) {
|
printf("%d bytes, errors = %d\n", i * 1000, errors);
|
printf("%d bytes, errors = %d\n", i * 1000, errors);
|
}
|
}
|
}
|
}
|
if (sfd != 0) {
|
if (sfd != 0) {
|
serialClose();
|
serialClose();
|
sfd = 0;
|
sfd = 0;
|
}
|
}
|
return 0;
|
return 0;
|
}
|
}
|
|
|