/*
file: send.c
compile: make
sends PRG using 1551USB
Version 0.2
9.4.2017
+ runtime (linux)
some bugfix
Version 0.1
19.2.2017
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/io.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include "rs232.h" /* uses: http://www.teuniz.net/RS-232 */
int main(int argc, char** argv)
{
FILE *datei;
unsigned char * buffer;
unsigned char * buffer2;
int sendsize,a,point,lopoint,hipoint,blocks,tsize,rest,i, port=1;
int size;
char* filename = NULL;
int xport;
char mode[]={'8','N','1',0};
int baudrate = 57600;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'f': filename = argv[++i];
break;
case 'p': port = atoi(argv[++i]);
break;
}
}
}
switch (port) {
case 1: xport = 16; break; // Windows COM1
case 2: xport = 17; break; // Windows COM2
}
if ((datei = fopen(filename, "rb")) == NULL)
{ printf("nError: File not found !an");
printf("Usage: send -f {filename} -p {port}n");
#if defined(__linux__) || defined(__FreeBSD__) /* Linux & FreeBSD */
printf("port: 1 = /dev/ttyUSB0 (default)nn");
printf("port: 2 = /dev/ttyUSB1nn");
#else /* Windows */
printf("port: 1 = COM1 (default)nn");
printf("port: 2 = COM2 nn");
#endif
;return 0;
}
/* Filesize */
fseek(datei,0,SEEK_END);
size=ftell(datei);
fseek(datei,0,SEEK_SET);
if (size > 65535) { printf("Abort ! - File to big for your target machine !n");exit(1);}
/* Calculate complete blocks and carryover bytes */
tsize = size -2;
blocks = (tsize/256);
rest = tsize - (blocks*256);
/* Calculate pointer */
point= 1025+tsize;
hipoint = point /256;
lopoint = point - (hipoint*256);
printf("# Complete Blocks : %i n",blocks);
printf("# Carryover Bytes : %i n",rest);
/* Memory */
buffer = (unsigned char *) malloc(size);
buffer2 = (unsigned char *) malloc(size);
/* Read File */
fread(buffer, sizeof(unsigned char), size,datei);
fclose(datei);
printf("# Low-Address: %#X n", buffer[0]);
printf("# High-Address: %#X n", buffer[1]);
a =3;
for(i=0; i<=size; i++)
{
buffer2[a] = buffer[i];
a++;
}
if (rest != 0) { blocks++;}
buffer2[0]= lopoint;
buffer2[1]= hipoint;
buffer2[2]= blocks;
a=0;
if (rest != 0)
{
for(i=0; i<=255-rest; i++)
{
buffer2[size+3+a] = 0;
a++;
}
}
sendsize=size+a+3;
printf("# Filename: %s n",filename);
printf("# Adding Pointer: %i", sendsize);
printf(" Bytes n");
printf("# Completing Blocks: %i n", blocks);
printf("# Sending @ %i ", baudrate);
printf("bps n");
/* Transfer */
/* Debug and secondary RS232 for Linux */
filename ="sendarray.prg";
datei = fopen(filename, "wb");
fwrite(buffer2, sendsize+1, 1, datei);
fclose(datei);
system ("time -f '# Runtime: %e Seconds' sh -c 'cat sendarray.prg >/dev/ttyUSB0'");
system ("rm sendarray.prg");
/*
RS232_OpenComport(xport,baudrate,mode);
for(i=0; i<=sendsize+1; i++)
{
RS232_SendByte (xport,buffer2[i]);
if (i <= sendsize) {
printf("r# Progress: %i", i); printf(" Bytes");
fflush(stdout);
}
usleep(100);
}
RS232_CloseComport(xport);
printf("n");
*/
}Letzte Änderung: 2019-01-06 09:45:54