Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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 serial cable not any otherone
when i got your fine solution i will ask for more question regarding the serialport 
program so be aware.


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:29am
Comments
[no name] 14-Mar-16 6:42am    
Read this to have some Basics:
Serial Comms in C# for Beginners[^]
lukeer 14-Mar-16 6:58am    
Don't repost.

1 solution

Quote:
but i could not


what does that mean ? what happens ?? do you get an error ??? have you stepped through your program with a debugger (if so, what happened, if not, WHY NOT) ????

you say you have a serial cable - ok, did you build it or buy it ? either way, are you sure you have what is known as a 'null modem' cable ?

On the receiving side, wouldnt using the DataReceived Event Handler be better than just reading from the port assuming there is data there ?

finally .. the C# Implementation of System.IO.Ports leaves a lot to be desired - its 'buggy' - the net is full of fixes/abstractions to get around its quirks
 
Share this answer
 
Comments
Engineer khalid 14-Mar-16 13:42pm    
now i used the cross serial cable which make the job done i could send a string and receive it from the other computer but
do not be surprise if i post another question regarding sending a byte or decimal
through the serial cable or trueley CROSS SERIAL CABLE

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