Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I am trying to create an application which will send a mock data on Serial Port COM1 for example a string "Hi".

Now this same application will try to read data from same COM1 i.e. I want to read same "hi" message from COM1. For this I have written the code but the serialPort_DataReceived() event never fire in my application. I don't know why i can get read the data from that port.

Is it not possible or something I am doing wrong?

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RS232MockInterface
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
            serialPort1.Open();
            serialPort1.Handshake = System.IO.Ports.Handshake.None;
            serialPort1.DtrEnable = true;
            serialPort1.RtsEnable = true;
            serialPort1.ReadTimeout = 6000;
            serialPort1.WriteTimeout = 6000;
        }

        delegate void OutputUpdateDelegate(string data);

        private void OutputUpdateCallback(string data)
        {
            txtMessages.Text += data;
            //MessageBox.Show(data);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(!serialPort1.IsOpen)
            {
                serialPort1.Open();
                serialPort1.Handshake = System.IO.Ports.Handshake.None;
                serialPort1.DtrEnable = true;
                serialPort1.RtsEnable = true;
                serialPort1.ReadTimeout = 6000;
                serialPort1.WriteTimeout = 6000;
            }
            serialPort1.Write("Hi");
            string str = serialPort1.ReadExisting();
            //str = serialPort1.ReadLine();
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                String RecievedData;
                RecievedData = serialPort1.ReadExisting();
                if (!(RecievedData == ""))
                {
                    txtMessages.Invoke(new OutputUpdateDelegate(OutputUpdateCallback), RecievedData);
                }
              
            }
        }

        private void serialPort1_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
        {

        }

        private void serialPort1_PinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
        {

        }
    }
}
Posted
Updated 4-Apr-18 20:41pm

1 solution

Before you go any further read this: Serial Comms in C# for Beginners[^]. There is a receive pin and a transmit pin. They are not connected.

You can make yourself a loopback adapter in order to do a loopback test: PassMark Loopback Parallel & Serial port pinouts[^]. This will require the aforementioned pins 2 and 3 to be connected together.

Now you can do some testing: How to Do a Serial Loopback Test - National Instruments[^]
 
Share this answer
 
Comments
Member 11985297 5-Apr-18 3:40am    
Thank you for the links.

One question, why all the examples of Serial Port Communication uses port pairs (COM1 <--> COM2) to send and receive data and not the single port only (COM1)?
[no name] 5-Apr-18 5:16am    
My answer tells you why. In a nutshell a serial port (UART) is a hardware device for communicating between nodes, not with itself. Thus in any usefull configuration you need two UARTS. Read the Codeproject article before you go further.
Member 11985297 5-Apr-18 5:27am    
so, should I convince myself that I cannot read and write data using only one Serial Port in my mock application?
Member 11985297 5-Apr-18 5:38am    
Also, can't I make MYself a loopback adapter for my virtually added Serial Port and then use my existing code? If yes then what change do I have to make?

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