Results 1 to 15 of 15

Thread: [RESOLVED] Connect 2 computers via serial

  1. #1

    Thread Starter
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,119

    Resolved [RESOLVED] Connect 2 computers via serial

    I need to connect 2 computers together via serial.

    I want the stdin/stdout from a console program on machine A to be redirected to serial com1 and then for a program on machine B to obtain the serial output from A and to send input to serial on A. Initially I will be using YAT on machine B for testing before writing the program for machine B.

    Machine A has a 9-pin male serial port. Machine B doesn't have any serial ports so it'll need a USB-serial convertor. Control signals (for handshaking etc) as well as data signals need to be passed. Will a USB serial convertor with a 9-pin female plug work - or do I need a null modem as well for cross-over?

    Does anyone have any advice re the hardware (I can do the software) to use please?

    Thanks.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,797

    Re: Connect 2 computers via serial

    As far as could find:
    The cable itself apparently must be "null modem"
    https://www.fieldtriptoolbox.org/faq...two_computers/
    https://forums.ni.com/t5/LabVIEW/Com...80843#M1208774
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,677

    Re: Connect 2 computers via serial

    Yeah, you'll need a null-modem or a cross-over patch cable -- that's the most important part of it " A null modem cable is an RS-232 serial cable where the transmit and receive lines are cross-linked." (from the Z's first link) ... that cross over in the Tx-Dx line is the key to making the direct link between the two systems working.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,119

    Re: Connect 2 computers via serial

    Thanks. I've ordered a StarTech usb to female db9 null modem. I've provide an update when it comes and I've tried it. Watch this space.......
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5

    Thread Starter
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,119

    Re: Connect 2 computers via serial

    OK. I've now got the StarTech usb to D9 null modem cable installed between the 2 PCs. Set the right baud rate etc on both PCs in Device Manager and tried..... Nope, just garbage being displayed which usually means mismatched baud rates etc. But both show the same in device manager..... Some time later and after a couple or so of stiff ones, I realise that I have to set the baud rate etc within the command prompt as I'm using a console program. Dohhh!!! After using MODE to set the right baud, bits etc .... Success! I can control the console program running on machine A by YAT on machine B. Ok! Now it's time to start coding - but first I think another couple of stiff ones....
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,619

    Re: Connect 2 computers via serial

    Basic stuff from the old days when a serial port was used for comms between a PC and any sort of hardware or for o/s debugging.

    This method of serial debugging still used by ReactOS devs to this very day when trying to debug boot and driver booting issues in ReactOS.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  7. #7

    Thread Starter
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,119

    Re: Connect 2 computers via serial

    Things were going so good, but there's been a fall at the second fence......

    I'm currently using YAT for testing purposes before I start coding my own program so that I know if there's an issue isn't not me.

    I'm got YAT on both computers and I can send/receive between then OK. From a command prompt on one computer I can do dir >com1 (redirect stdout to com1) and see it's output in YAT on the other. Works both ways. OK.

    However when I try to re-direct stdin from com1 (myapp <com1) then there is the first problem. Using YAT on the other computer to send the data, the first sent text is only received after CR is pressed twice. And after that no further text is received until I've closed down and re-started the command prompt. Then again only the first sent line of text is received after CR pressed twice.

    The other problem is that I can't redirect both stdout/stdin to/from com1. ie I can't do myapp <com1 >com1 as I get the error message 'Access is denied'. Redirection of both stdin and stdout to the same com port is a fundamental requirement.

    Does anyone have any insights, thoughts, solutions please? and no, I can't change the myapp program.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,619

    Re: Connect 2 computers via serial

    You may find some QB4.5 source or a more later converted to QB64 program that will do something similar to what you want. Get that working and you will determine whether it is the o/s that is blocking you or not. Find something that works, a binary or in source for comparison. If you have a QB4.5 program then you can probably use it as a basis for a conversion.

    The QB64 forum was nuked recently and all old threads were removed but the current forum(s) may assist.

    QB was a language used for this sort of thing in the old days, I used it myself to do similar. The only problem will be whether any code from that period is still extant and available. Mine isn't.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  9. #9

    Thread Starter
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,119

    Re: Connect 2 computers via serial

    I think the issue is with the OS as the problems are:

    1) redirecting stdin to be from a com port doesn't work as expected on a char-by-char basis. In this case only obtaining the first line of text (terminated by CR) works. Further sends (by YAT) aren't being received by the app program (but using YAT-YAT works OK so this issue isn't the serial transmission but stdin being redirected to use a comm port).

    2) I can't redirect both stdin and stdout for both to use the same com port (ie app <com1 >com1). The os reports the error 'Access is denied' Presumably because cmnd is trying to open com1 twice with the second open failing. As I indicated above, I can't change this console program.

    Does anyone know why redirecting stdin to use a comm port is not working 'as expected' - and is there a way of changing this behaviour?

    Also is there a way using c API's within a c/c++ program (not VB code please as I only use c/c++) to redirect stdin and stdout to use a com port and then execute the required program with these file streams already redirected? It's no good even starting to write my own serial controller until I have this working with YAT.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10

    Thread Starter
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,119

    Re: Connect 2 computers via serial

    I've come across these on the internet for redirecting both stdin and stdout:

    1) app 1>com1 0<&1
    Does not work. stdout is redirected ok, but the program ends when stdin is used for input.

    2) app <>com1 >&0
    Invalid syntax with <>

    3) app <com1 >&0
    Does not work. Nothing sent from stdout and nothing received on stdin.

    Maybe these were for Linux? Any similar ideas for Windows?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,619

    Re: Connect 2 computers via serial

    You possibly ought to have stated the language you were using right at the very beginning. It seems it is a skill, almost an art, raising a forum request with all the useful information, contained therein.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  12. #12

    Thread Starter
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,119

    Re: Connect 2 computers via serial

    Quote Originally Posted by yereverluvinuncleber View Post
    You possibly ought to have stated the language you were using right at the very beginning. It seems it is a skill, almost an art, raising a forum request with all the useful information, contained therein.
    Apart from the api question in my post #9 (when I did state c/c++), the rest were not programming related questions hence I didn't state the programming language I will be using once it all works with YAT. The issues are how to get an existing console program to have stdin and stdout both redirected to the same com1 and how to get stdin to correctly obtain chars from a console port when redirected. If this can be done using a command line magical incantation then great. Otherwise I was asking if this could be done using api in c/c++

    PS I've come across dup2() (as c):

    Code:
    int comfd = open("COM1", O_RDWR);
    dup2(comfd, fileno(stdin));
    dup2(comfd, fileno(stdout));
    which apparently will redirect both stdin and stdout to com1. Which is fine as it goes, but once they have been redirected I then need to execute the required console program with the redirected streams. I've tried using:

    Code:
    CreateProcessA(NULL, szCmdline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
    WaitForSingleObject(pi.hProcess, INFINITE);
    following the above, but whilst the required program starts OK, there's no input/output on com1. Ahhhh.........
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,619

    Re: Connect 2 computers via serial

    OK but this being a VBforum you get the idea that disclosing the language you will be using early on is a sensible idea. I am leaving now.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  14. #14

    Thread Starter
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,119

    Re: Connect 2 computers via serial

    I've found one problem with why stdin wasn't working properly when re-directed to the com port. YAT was set by default to use CR as a text line terminator. Setting the line terminator to <CR><LF> solved that problem.

    However I'm still left with the issue of redirecting both stdin and stdout to the same comm port from the command prompt when the program is executed. Redirecting either stdin or stdout works, but not both at once from the command line.....
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15

    Thread Starter
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,119

    Re: Connect 2 computers via serial

    Cracked it! This is a wrapper program that will redirect both stdin and stdout to com1 and then call the specified console program. The specified program will then use com1 for both stdin and stdout.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    
    #include <io.h>			// For dup2
    #include <fcntl.h>		// For _O_RDWR
    #include <stdio.h>
    #include <windows.h>
    
    
    int main(int argc, char *argv[]) {
    	if (argc != 2)
    		return fprintf(stderr, "Must specify the program to execute\n"), 1;
    
    	const int comfd = _open("COM1", _O_RDWR);
    
    	if (comfd == -1)
    		return fprintf(stderr, "Bad open: %d\n", errno), 2;
    
    	if (_dup2(comfd, _fileno(stdin)) == -1)
    		return fprintf(stderr, "Bad dup2 stdin: %d\n", errno), 3;
    
    	if (_dup2(comfd, _fileno(stdout)) == -1)
    		return fprintf(stderr, "Bad dup2 stdout: %d\n", errno), 4;
    
    	STARTUPINFO si {sizeof(STARTUPINFO)};
    	PROCESS_INFORMATION pi {};
    
    	if (!CreateProcessA(NULL, argv[1], NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
    		return fprintf(stderr, "Bad create: %d\n", GetLastError()), 5;
    
    	WaitForSingleObject(pi.hProcess, INFINITE);
    
    	DWORD ext {};
    
    	GetExitCodeProcess(pi.hProcess, &ext);
    
    	if (ext)
    		fprintf(stderr, "Error with program: %d\n", ext);
    
    	CloseHandle(pi.hProcess);
    	CloseHandle(pi.hThread);
    }
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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



Click Here to Expand Forum to Full Width