Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <algorithm>
- #define SERIAL "/dev/ttyS0"
- #define BAUDRATE B9600
- #define NUM_ENDBUF 8
- int tty_fd, flags;
- void send(unsigned char what) {
- write(tty_fd, &what ,1);
- }
- void send(unsigned char* buffer, uint32_t length) {
- // sleep(1);
- write(tty_fd, buffer, length);
- }
- void recieve(unsigned char* buffer, unsigned int length) {
- read(tty_fd,buffer,length);
- }
- int main(int argc,char** argv)
- {
- if (argc<2) {
- printf("Usage: com <filename>\n");
- return 1;
- }
- // Load file
- FILE* file = fopen(argv[1], "rb");
- if (file==NULL) {
- printf("File %s not found\n", argv[1]);
- return 1;
- }
- // Get file length
- fseek(file,0,SEEK_END);
- uint32_t length = ftell(file);
- fseek(file,0,SEEK_SET);
- // Load data
- unsigned char* buffer = new unsigned char[length];
- fread(buffer, sizeof(char),length,file);
- printf("Sending file %s (%d bytes, %d chunks)\n", argv[1], length, length>>8);
- struct termios tio;
- memset(&tio,0,sizeof(tio));
- cfsetospeed(&tio,BAUDRATE);
- cfsetispeed(&tio,BAUDRATE);
- tio.c_iflag=0;
- tio.c_oflag=0;
- tio.c_cflag=CS8|CREAD|CLOCAL|CRTSCTS; // Obs, CRTSCTS รคr ny
- tio.c_lflag=0;
- tio.c_cc[VMIN]=1;
- tio.c_cc[VTIME]=5;
- if((tty_fd = open(SERIAL , O_RDWR)) == -1){ // | O_DSYNC
- printf("Error while opening %s\n\r", SERIAL);
- return -1;
- }
- tcsetattr(tty_fd,TCSANOW,&tio);
- tcflush(tty_fd, TCIOFLUSH);
- // Send size as four bytes, MSB first, to initialize transfer
- send((length>>24)&0xff);
- send((length>>16)&0xff);
- send((length>>8)&0xff);
- send((length)&0xff);
- printf("Sent length\n");
- bool done=false;
- uint16_t lastchunknr=-1;
- while (!done) {
- // Get chunk number
- uint16_t chunknr=0;
- uint8_t input[2];
- recieve( &input[0], 2);
- chunknr=(input[0]<<8) | input[1];
- //printf("\nchunknr:%x\n", chunknr);
- // If chunk $ffff is asked for, kindly exit
- if (chunknr==0xffff) {
- printf("\nQuitting...\n");
- exit(0);
- }
- // Just debug
- if (chunknr==lastchunknr) printf("\n\n\rResending chunk %d\n\n",chunknr);
- lastchunknr=chunknr;
- uint32_t chunklength = std::min(uint32_t(256),uint32_t(length-chunknr*256));
- // Send checksum
- uint8_t checksum=0;
- for (uint32_t i=0; i<chunklength; i++) {
- checksum+=buffer[chunknr*256+i]; // (will %256 automatic)
- }
- printf("\nSending chunk [%3d of %3d] (checksum:%3d, length:%3d) ", chunknr,length>>8, checksum, chunklength);
- fflush(stdout);
- send(checksum);
- send(uint8_t(chunklength-1));
- tcdrain(tty_fd);
- // Send part
- send(buffer+(chunknr<<8), chunklength);
- tcdrain(tty_fd);
- // Send some crap at the end just to make sure, code
- // timeout on the amiga to cope with this instead
- uint8_t endbuf[NUM_ENDBUF];
- for (int i=0; i<NUM_ENDBUF; i++) endbuf[i]=65;
- write(tty_fd,endbuf, NUM_ENDBUF);
- tcdrain(tty_fd);
- }
- printf("File sent, exiting...\n\n");
- close(tty_fd);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement