Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
can i write a byte to the serial port and then read the values it 'll send me ?
i want to be able to write to the serial port "1"
and when my pic micro controller receives this "1" it will sends me the values of portD in which i want to use later

but i 'm not able to communicate correctly with the serial port

this is not the program i'm trying to build but i' just testing the basics on how to read and write from/to a serial port and nothing is working so far :/

i'm just testing how to communicate with the serial port and this is the code i'm trying

i have a button that when i click i want a "1" to be sent and then the textbox1 will display the value received

SerialPort serialPort1 = new SerialPort("COM2", 300, Parity.None, 8, StopBits.One);
        
        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("1");
            serialPort1.Close();

            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.Open();

            }
            else
            {
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.Open(); //opens the port
            }

        }
        int rs;

        private void serialPort1_DataReceived(object sender,SerialDataReceivedEventArgs e)
        {
            SerialPort serialPort1=(SerialPort)sender;
            try
            {

                rs = serialPort1.ReadByte();
                this.Invoke(new EventHandler(type));
            }
            catch (System.TimeoutException)
            {
            }


        }
        private void type(object s,EventArgs e)
        {
            textBox1.Text = rs.ToString();
        }
Posted

1 solution

Firstly, stop opening and closing the port: it won't help and it may cause data to be lost. While the port is closed, anything received by the physical port will be thrown away...and it allows other applications to "grab" the port as well. And you don't need an event handler to pass the data either.

So open the port once, add the DataReceived handler once, and send the data.
Then use the event to fetch all the data the PIC returns, and display it - but don't use an int to transfer it, as the RX thread could easily overwrite it before your UI thread gets a chance to read it and display it. Instead, read all the bytes in one go, and pass the data through to the UI thread via a Invoke method call:
C#
string data = SerialPort1.ReadExisting();
Invoke(new MethodInvoker(() => { ShowData(data);}));

C#
private void ShowData(string s)
   {
   textBox1.Text += s;
   }
 
Share this answer
 
Comments
Duaa7 2-May-14 12:03pm    
i'm not sure i can write and then read from the port and i'm not able to
Duaa7 2-May-14 12:04pm    
private void button1_Click(object sender, EventArgs e)
{

serialPort1.Write("1"); // there's an exception that's unhandled in this line
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.Open(); //opens the port


}


private void serialPort1_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
SerialPort serialPort1=(SerialPort)sender;
string data = serialPort1.ReadExisting();
Invoke(new MethodInvoker(() => { ShowData(data); }));


}
//even this isn't working so i think i can't write and directly read afterwards or am i wrong?
OriginalGriff 2-May-14 12:07pm    
You're wrong! Trust me - you can use the same port for reading and writing at the same time.

Check your connections with Hyperterminal or similar, and make sure it all works to your PIC first, then copy the settings into your code and try there.
One variable at a time: it could be as simple as you plugged the connector in wrong! :laugh:
OriginalGriff 2-May-14 12:09pm    
Sorry - just saw your bit at the top - you can't write to a closed port.
Move the serialPort1.Open and the DataReceived event handler out of the button click event, and into the form load. You only want to execute each of them once, ever.
Duaa7 2-May-14 12:38pm    
alright thank you for the info :), and sorry for my lack of knowledge i'm new to this ,and i have nothing but the internet to get the help i need.

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