From 268446a633c590c6edc812732533e3778482cd4c Mon Sep 17 00:00:00 2001 From: drowe67 Date: Sun, 6 Mar 2016 02:21:00 +0000 Subject: [PATCH] working with test files git-svn-id: https://svn.code.sf.net/p/freetel/code@2714 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/src/drs232.c | 197 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 codec2-dev/src/drs232.c diff --git a/codec2-dev/src/drs232.c b/codec2-dev/src/drs232.c new file mode 100644 index 00000000..f0a08ef1 --- /dev/null +++ b/codec2-dev/src/drs232.c @@ -0,0 +1,197 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: drs232.c + AUTHOR......: David Rowe + DATE CREATED: March 2016 + + Looks for a unique word in series of bits. When found, deframes a RS232 + encoded frame of bytes. Used for high biit rate Horus SSTV reception. + + Frame format: + + 16 bytes 0x55 - 0xabcd UW - 256 bytes of payload - 2 bytes CRC + + Each byte is encoded as a 10 bit RS232 serial word: + + 0 LSB .... MSB 1 + + Building: + + $ gcc drs232.c -o drs232.c -Wall + +\*---------------------------------------------------------------------------*/ + + +/* + Copyright (C) 2016 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include +#include +#include + +/* states -----------------------------------------------*/ + +#define LOOK_FOR_UW 0 +#define COLLECT_PACKET 1 + +/* packet parameters */ + +#define UW_BYTES 2 +#define UW_BITS 20 +#define BYTES_PER_PACKET 256 +#define CRC_BYTES 2 +#define BITS_PER_BYTE 10 +#define UNPACKED_PACKET_BYTES ((UW_BYTES+BYTES_PER_PACKET+CRC_BYTES)*BITS_PER_BYTE) + +/* UW pattern we look for, inclduing start/stop bits */ + +uint8_t uw[] = { + /* 0xb 0xa */ + 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, + /* 0xd 0xc */ + 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, +}; + + // from http://stackoverflow.com/questions/10564491/function-to-calculate-a-crc16-checksum + +unsigned short gen_crc16(unsigned char* data_p, int length){ + unsigned char x; + unsigned short crc = 0xFFFF; + + while (length--){ + x = crc >> 8 ^ *data_p++; + x ^= x>>4; + crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x); + } + + return crc; +} + + +int main(int argc, char *argv[]) { + FILE *fin, *fout; + int state, next_state, i, j, k, ind, score, bits_read; + char bit; + uint8_t unpacked_packet[UNPACKED_PACKET_BYTES]; + uint8_t packet[BYTES_PER_PACKET+CRC_BYTES]; + uint8_t abyte; + uint16_t tx_checksum, rx_checksum; + + if (argc < 3) { + printf("usage: drs232 InputOneBitPerChar OutputPackets\n"); + exit(1); + } + + if (strcmp(argv[1], "-") == 0) fin = stdin; + else if ( (fin = fopen(argv[1],"rb")) == NULL ) { + fprintf(stderr, "Error opening input speech file: %s: %s.\n", + argv[2], strerror(errno)); + exit(1); + } + + if (strcmp(argv[2], "-") == 0) fout = stdout; + else if ( (fout = fopen(argv[2],"wb")) == NULL ) { + fprintf(stderr, "Error opening output speech file: %s: %s.\n", + argv[3], strerror(errno)); + exit(1); + } + + state = LOOK_FOR_UW; + for(i=0; i