Source

tw-comport-win32 / com.c

Full commit
//---------------------------------------------------------------------------

#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused

#include <conio.h>
#include <stdio.h>
#include <windows.h>
#define EOF -1

int main(int argc, char* argv[]) {
    HANDLE hComm;
    DCB dcb;
    char send_data[512], recv_data[132], choose, buffer[8];
    long size, n, i, pos;
    int x_paket[132], n_paket, z, l;
    FILE *fp;
    DWORD number;
    COMMTIMEOUTS timeout;

    // create handle to comport
    hComm = CreateFile(
        "COM1",
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
        OPEN_EXISTING,
        0,
        0
    );
    if (hComm == INVALID_HANDLE_VALUE) {
        // error opening port; abort
        return FALSE;
    }

    // get the current DCB
    if (!GetCommState(hComm, &dcb)) {
        // error getting state
        return FALSE;
    }

    // settings for dcb struct
    dcb.BaudRate = CBR_9600;
    timeout.ReadTotalTimeoutConstant = 10000;
    timeout.ReadIntervalTimeout = 10000;
    timeout.ReadTotalTimeoutMultiplier = 0;
    SetCommTimeouts(hComm, &timeout);

    if (!SetCommState(hComm, &dcb)) {
        // error setting state
        return FALSE;
    }

    do {
        choose = 'x';
        while(choose != 'r' && choose != 'w' && choose != 'e') {
            clrscr();
            printf("(r)ead, (w)rite or (e)xit?\n");
            choose = getch();
            printf("\n");
        }
        //----------------------Receive File--------------------------
        if(choose == 'r') {
            printf("Waiting for data on port...\n");
            // read from port
            if(!ReadFile(hComm, recv_data, 132, &number, NULL)) {
                printf("Error while reading.\n");
            }
            else {
                if(number > 0) {
                    printf("Received %d bytes:\n", number, recv_data);
                    printf("--------\n");
                    for(i=0; i<132; i++) {
                        buffer[i] = recv_data[3+i];
                    }
                }
                else {
                    printf("Timeout!\n");
                }
            fp = fopen("C:\\inn.jpg", "w+b");
            fclose(fp);
            if(fp == NULL)
                printf("Error: Coudn't create new file!\n");
            fp = fopen("C:\\inn.jpg", "wb"); 
            fwrite(recv_data, 128, size, fp);
            fclose(fp); 
            }
            printf("Press Enter to continue\n");
            getch();
        }
        
    //----------------------Send File-----------------------------------
            else if(choose == 'w') {
            //printf("Path to file: ");
            //scanf("%s", send_data);
            fp = fopen("C:\\out.jpg", "rb");
            if(fp == NULL) {
                printf("Error opening file!\n");
                getch();
                return FALSE;
            }
            fseek(fp, 0, SEEK_END);
            size = ftell(fp);
            fseek(fp, 0, SEEK_SET);
            fread(send_data, 1, size, fp);
            printf("%s\n%d\n", send_data, size);
            fclose(fp);
            pos = 0;
            n = 0;
            l = 0;
            itoa(size, buffer, 10);
            x_paket[0] = 0x01;
            x_paket[1] = n;
            x_paket[2] = ~n;
            for(i=0; i<8; i++)
            {
                x_paket[3+i] = buffer[i];
            }
            /*for(i=0; i<120; i++)
            {
                x_paket[11+i] = pfad;
            }*/
            
            
            WriteFile(hComm, x_paket, 132, &number, NULL);
            do
            {
                n++;
                x_paket[0] = 0x01;
                x_paket[1] = n;
                x_paket[2] = ~n;
                z = size % 128;
                size /= 128;
                if(z != 0)
                    n_paket = size+1;
                else
                    n_paket = size;
                for(i=0; i<pos+128; i++ )
                    x_paket[3+i] = send_data[pos+i];
                x_paket[131] = 99;

                printf("\n");
                pos += 128;
                WriteFile(hComm, x_paket, 132, &number, NULL);
            } while(n < n_paket);
        }

    } while(choose != 'e');

    CloseHandle(hComm);
    printf("Press Enter to exit.");
    getch();
    return 0;
}
//---------------------------------------------------------------------------