Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    printf keyboard input on the other computer

    i have connected two computers using null modem cable, i am connecting them using the serial ports. It is a console application using C++. How do i printf the key that is typed on Computer A to be on Computer B? Stucked, need help, thanks!

    I have separated the codes into two, one is for the "writing" computer and the other is for the "reading" computer


    For the "reading" computer:
    Code:
    #include "windows.h"
    #include <stdio.h>
    #include <io.h>
    #include <conio.h>
    
    #define maxBytes 32
    
    int main()
    {
    HANDLE hSerial;
    DCB dcbSerialParams = {0};
    COMMTIMEOUTS timeouts = {0};
    DWORD dwBytesRead = 0;
    char szBuff[maxBytes] = {0};
    
    //opening the serial port
    hSerial = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    
    if(hSerial==INVALID_HANDLE_VALUE)
    {
    if(GetLastError()==ERROR_FILE_NOT_FOUND)
    {
    printf("Serial port does not exist\n");
    }
    printf("Other errors\n");
    }
    
    //setting parameters
    dcbSerialParams.DCBlength = sizeof (dcbSerialParams);
    
    //GetCommState is to retrieves the current control settings for a specific communications device.
    if (!GetCommState(hSerial, &dcbSerialParams))
    {
    printf("Not GetCommState, not able to retrieves the current control.\n");
    }
    
    dcbSerialParams.BaudRate = CBR_9600;
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.Parity = NOPARITY;
    
    //SetCommState configures a communications device according to the specifications
    //in DCB. The function reinitializes all hardware control settings but it does not
    //empty output or input queues
    if (!SetCommState(hSerial, &dcbSerialParams))
    {
    printf("Not SetCommState, cannot configures serial port according to DCB specifications set\n");
    }
    
    //setting timeouts
    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 50;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier = 50;
    
    //SetCommTimeouts set the time out parameters for all read and write operation
    if (!SetCommTimeouts(hSerial, &timeouts))
    {
    printf("Not SetCommTimeouts, cannot set the timeout parameters to serial port\n");
    }
    
    //reading data
    //ReadFile reads data from the specified file or i/o devices.
    if (!ReadFile(hSerial, szBuff, maxBytes, &dwBytesRead, NULL))
    {
    printf("Serial port cannot read file");
    }
    
    //closing down
    CloseHandle(hSerial);
    
    }
    I did not know what is the code to be written under the ReadFile() so that it can sense the keyboard input of the "writing" computer through the serial port.





    For the "writing" computer:
    Code:
    #include "windows.h"
    #include <stdio.h>
    #include <io.h>
    #include <conio.h>
    
    #define maxBytes 32
    
    int main()
    {
    HANDLE hSerial;
    DCB dcbSerialParams = {0};
    COMMTIMEOUTS timeouts = {0};
    DWORD dwBytesRead = 0;
    char szBuff[maxBytes] = {0};
    
    //opening the serial port
    hSerial = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    
    if(hSerial==INVALID_HANDLE_VALUE)
    {
    if(GetLastError()==ERROR_FILE_NOT_FOUND)
    {
    printf("Serial port does not exist");
    }
    printf("Other errors");
    }
    
    //setting parameters
    dcbSerialParams.DCBlength = sizeof (dcbSerialParams);
    
    //GetCommState is to retrieves the current control settings for a specific communications device.
    if (!GetCommState(hSerial, &dcbSerialParams))
    {
    printf("Not GetCommState, not able to retrieves the current control.");
    }
    
    dcbSerialParams.BaudRate = CBR_9600;
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.Parity = NOPARITY;
    
    //SetCommState configures a communications device according to the specifications
    //in DCB. The function reinitializes all hardware control settings but it does not
    //empty output or input queues
    if (!SetCommState(hSerial, &dcbSerialParams))
    {
    printf("Not SetCommState, cannot configures serial port according to DCB specifications set");
    }
    
    //setting timeouts
    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 50;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier = 50;
    
    //SetCommTimeouts set the time out parameters for all reand and write operation
    if (!SetCommTimeouts(hSerial, &timeouts))
    {
    printf("Not SetCommTimeouts, cannot set the timeout parameters to serial port");
    }
    
    //Writting data
    //WriteFile write data from the specified file or i/o devices.
    if (WriteFile(hSerial, szBuff, maxBytes, &dwBytesWrite, NULL))
    {
    while(1)
    {
    WriteFile(hSerial, szBuff, maxBytes, &dwBytesWrite, NULL);
    if (_kbhit() != 0)
    {
    printf("Receiving\n");
    break;
    }
    }
    }
    
    
    //closing down
    CloseHandle(hSerial);
    
    }
    Under the WriteFile(), my main motive is to printf the "Receiving" on the "reading" computer instead of this but i am not sure how to let the two computers coordinate(i know the codes are wrong). I am really new in this and have no knowledge, some one please correct me.

  • #2
    New Coder
    Join Date
    May 2009
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    I just had a google search for you, there's plenty out there about serial ports, there's even a library for it: http://sourceforge.net/projects/libserial/
    This looks like another particularly useful one: http://www.fogma.co.uk/foggylog/archive/140.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •