Leading Zero bytes Serial USB from Arduino teensy using termios

0

I have a singleboard odroid neo4 that is connected via usb to a teensy 4.0. I’m attempting to send 4 kb buffers at a time from the teensy to the neo4 of unsigned shorts (I realize that its ultimately just raw bytes transmitting). I’m getting 256 bytes (or 128 shorts) of leading zeros each 4kb buffer received. This project involves a sensor collecting data that is being run by the teensy, for now I’m attempting to just send the values, 0 - 2047 (4 kb’s of counting shorts).

The teensy is running arduino code that Serial.write(buffer[4096])) when triggered every 5 seconds and I’ve verified with labview that its successfully sending the data. The C code on the Neo4 opens the Serial port with the options below, I’ve tried many configurations found across the internet and after reading the linux man page as well as a few other resources on termios ended up using the non-canonical settings found here: http://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html with a few small changes. From what I’ve found, using termios in non-canonical mode relies on VTIME and VMIN, because I don’t want to ever miss a data point from my sensor I elected to use VTIME = 0, VMIN = 0, not using an inter-char timer and not blocking. What I’ve done is created 2 buffers, one thats 256 bytes to read into, and a second that is 4kb to copy the smaller buffer into incrementally until its full, then, for now, just printing it. The problem I’ve run into as I’ve touched on is that when I print each large buffer of 4kb, the first 256 byte buffer is all zeros. Without being able to find the problem I patched this by simply sliding my 4kb buffer over by 256 bytes which works but doesn’t explain what is causing this problem and is more of a successful bandaid than an efficient function.

Note I can’t just check if a value is zero and ignore it as that value is necessary as a potential data point, and to be extra clear each buffer should print 0-2047 Thanks for the help

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define BAUDRATE B38400
#define MODEMDEVICE “/dev/ttyACM0”
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

//small buffer size
#define BUFSIZE 256

//offset determined by the mysterious 127 shorts of leading zeros
#define OFFSET 256

volatile int STOP=FALSE;

int main()
{
int fd,c, res;
struct termios oldtio,newtio;
unsigned char buf[BUFSIZE];

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio); /* save current port settings */

bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0; //&= ~OPOST;

/* set input mode (non-canonical, no echo,…) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME] = 0; /* inter-character timer unused /
newtio.c_cc[VMIN] = 0; /
not blocking read*/

tcflush(fd, TCIOFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

int total = 0;
//allocating extra offset so I can allow the extra zeros on the front
and still get my 4kb buffer
unsigned char* buf1 = malloc(4096 + OFFSET);
unsigned char* pos = buf1;

while (STOP==FALSE)
{ /* loop for input /
res = read(fd,buf,BUFSIZE); /
returns after 255 chars have been
input */

//the buffer position increments by res (read)
if (res > 0)
{
pos += res;
//cpy buf to whereever pos is at res amount
memcpy(pos,buf,res);
total += res;
//printf(“total: %d\n”, res);
}
//once 4kb buffer (+offset) is full
if (total >= 4096)
{
printf("%d b buffer: ",total);
//start late because of leading zeros
unsigned short * ps = buf1 + 256;

//adjust pointer because of off by 1 byte
// unsigned char* cs = ps;
// cs++;
// ps = cs;

//print 4kb of shorts
for (int i = 0; i < (4096/2); i++, ps++)
{
printf("%d “, *ps);
}
printf(”\n\n");
pos = buf1;
total = 0;
}
}
tcsetattr(fd,TCSANOW,&oldtio);
return 0;
}

OUTPUT:

4096 b buffer: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
//…continues to increment normally…
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047

4096 b buffer: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
//…continues to increment normally…
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047

^C