#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define BUFFER_SIZE 1024
// for AM3517 ttyS2 is available. //ttyUSB0
//#define DEVICE_FILE "/dev/ttyO0"
int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio, oldtio;
if(tcgetattr(fd, &oldtio) != 0)
{
perror("SetupSerial 1");
return -1;
}
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch(nBits)
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch(nEvent)
{
case 'O':
newtio.c_cflag |= PARENB ;
newtio.c_cflag |= PARODD ;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_cflag |= PARENB ;
newtio.c_cflag &= ~PARODD ;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'N':
newtio.c_cflag &= ~PARENB ;
break;
}
switch(nSpeed)
{
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 19200:
cfsetispeed(&newtio, B19200);
cfsetospeed(&newtio, B19200);
break;
case 57600:
cfsetispeed(&newtio, B57600);
cfsetospeed(&newtio, B57600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
}
if(1 == nStop)
newtio.c_cflag &= ~CSTOPB;
else if(2 == nStop)
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;
tcflush(fd, TCIFLUSH);
if((tcsetattr(fd, TCSANOW, &newtio))!=0)
{
perror("com set error!");
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
//char package[BUFFER_SIZE] = "abcdefghijklmnopqrstuvwxyz1234567890";
//const char package[] = "abcdefghijklmnopqrstuvwxyz1234567890";
char package[BUFFER_SIZE] = ""; //"abcde12345";
char device[32] = ""; //"/dev/ttyO0";
int baud_rate = 115200;
int data_bit = 8;
int stop_bit = 1;
char parity = 'N';
int fd;
int nread, nwrite;
int ret;
char recv_buf[BUFFER_SIZE];
char total_buf[BUFFER_SIZE];
long total_bytes, total_packages;
int pos, len_package;
if(argc == 1)
{
printf("Usage: %s <device> <sendstring>\n", argv[0]);
printf(" eg. %s /dev/ttyS0 abcde12345\n", argv[0]);
return 0;
}
strcpy(device, argv[1]);
strcpy(package, argv[2]);
printf("device=%s,baud_rate=%d,data_bit=%d,stop_bit=%d,parity=%c\n", device, baud_rate, data_bit, stop_bit, parity);
// open
fd = open(device, O_RDWR|O_NOCTTY|O_NDELAY);
if(-1 == fd)
{
perror("Can't open Serial Port");
return -1;
}
if(fcntl(fd, F_SETFL, 0)<0)
{
printf("fcntl failed!\n");
return -1;
}
// set
ret = set_opt(fd, baud_rate, data_bit, parity, stop_bit);
if(ret < 0)
{
perror("Set_opt error!");
return;
}
pos = 0;
total_bytes = 0;
total_packages = 0;
len_package = strlen(package);
while(1)
{
nwrite = write(fd, package, len_package);
if(nwrite < 0)
{
perror("wirte error");
break;
}
total_bytes += nwrite;
total_packages++;
printf("Total Bytes = %ld, Total Packages = %ld\n", total_bytes, total_packages);
//if(total_packages % 100 == 0) break;
usleep(100000);
if(total_bytes >= 1024 * 1024 * 10)
{
printf("1024 * 1024 * 10 bytes have been sent.\n");
close(fd);
return 0;
}
}
close(fd);
return 0;
}