Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellow
i have connect two computers with serial cabel
in computer A ,i use the below program to send a string to computer B
but i could not
those notes might be helpfull for you
1- i have com1 only in both computers
2- the serial cable ends in femal shapes,another word i have used
the right serial cable not any otherone

when i got your fine solution i will ask for more question regarding the serialport
program so be aware.
//below are both programs that was in computer A and B
C#



What I have tried:

C#
//below are both programs that was in computer A and B
// in computer A(the sender)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.IO.Ports;

namespace MondaySerial
{
    public partial class Form1 : Form
    {
        static SerialPort sp = new SerialPort();
        public Form1()
        {
            InitializeComponent();
        }
        //--------------------------------------------------------------
        private void FormLoad(object sender, EventArgs e)
        {
            sp.PortName = "com1";
            //sp.PortName = "com2";
            sp.BaudRate = 9600;
            sp.DataBits = 8;
            sp.Parity = Parity.None;
            sp.StopBits = StopBits.One;
            sp.Handshake = Handshake.None;
            //
            sp.ReadTimeout = 5000;// Timeout.Infinite;//500;
            sp.Open();
        }
        //-------------------------------------------------------
        protected override void OnClosing(CancelEventArgs e)
        {
            DialogResult result;
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            result = MessageBox.Show(this, "Are You Sure Want to exit", " Answer Yes or No",
                buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            if (result == DialogResult.Yes)
            {
                sp.Close();
                //-------------------
                //ButtonForm1.ActiveForm.Close();
                //this.Close();
                Application.Exit();
            }//Yes
            else e.Cancel = true;
        }
        //---------------------------------------------------------
        private void SendStringClick(object sender, EventArgs e)
        {
            sp.WriteLine(textBox2.Text);
        }
        //----------------------------------------------------------
    }
}

// and in computer B (the receiver )

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.IO.Ports;

namespace MondaySerial
{
    public partial class Form1 : Form
    {
        static SerialPort sp = new SerialPort();
        public Form1()
        {
            InitializeComponent();
        }
        //--------------------------------------------------------------
        private void FormLoad(object sender, EventArgs e)
        {
            sp.PortName = "com1";
            //sp.PortName = "com2";
            sp.BaudRate = 9600;
            sp.DataBits = 8;
            sp.Parity = Parity.None;
            sp.StopBits = StopBits.One;
            sp.Handshake = Handshake.None;
            //
            sp.ReadTimeout = 5000;// Timeout.Infinite;//500;
            sp.Open();
        }
        //-------------------------------------------------------
        protected override void OnClosing(CancelEventArgs e)
        {
            DialogResult result;
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            result = MessageBox.Show(this, "Are You Sure Want to exit", " Answer Yes or No",
                buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            if (result == DialogResult.Yes)
            {
                sp.Close();
                //-------------------
                //ButtonForm1.ActiveForm.Close();
                //this.Close();
                Application.Exit();
            }//Yes
            else e.Cancel = true;
        }
        //---------------------------------------------------------
        private void ReadStringClick(object sender, EventArgs e)
        {
			textBox2.Text =  sp.ReadLine();
        }
        //----------------------------------------------------------
    }
}
Posted
Updated 14-Mar-16 0:08am
Comments
Jochen Arndt 14-Mar-16 6:03am    
Do you use a crossover cable or adapter?
This is required to connect the serial port of two PCs.
See also https://en.wikipedia.org/wiki/Null_modem
Jochen Arndt 14-Mar-16 6:15am    
Please don't repost your question. There is actually a bug here at CP that shows an error when positing questions. But the questions are posted.

If you need to add additional information, use the green 'Improve question' link to edit this question.
Engineer khalid 14-Mar-16 6:43am    
i am using the old model serial cable not the cross serial cable
Engineer khalid 14-Mar-16 6:45am    
sorry for reposting the quesion again but i was expecting to see an icon for updating the question. may be i did not see it

1 solution

Start with Hyperterminal or similar - run it on both computers and make sure that what you type at each can be seen at the other. Until you have that working, you have far too many variables to start debugging code.
The problem is that all "serial cables" aren't equal: some cross over (which is needed), some don't. Some provide flow control wires, some don't. And so on. If you can't talk between them with known working software, then you can't do much at all!
If PC to PC doesn't work, unplug one end, and use a paperclip to short pin 2 to pin 3 of the cable, and try again on the connected PC - you should see what you type being sent back (this is a simple loopback cable, and it's useful for testing).
When you have Hyperterminal working - and I can't do that for you - then start using the debugger to check if the two applications are working.
 
Share this answer
 
Comments
Engineer khalid 14-Mar-16 6:55am    
after few hours i will buy a cross serial cable which might work but
do you see any error in my code ?
OriginalGriff 14-Mar-16 7:06am    
I can't see anything obvious - but I can't test it under the same circumstances you are. So make sure the physical connections are working first, and your code may start to function!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900