Pregunta

I want to send some data from my pc to a PIC 16fxxxx microcontroller. I wrote the serial port transmitter code using c#:

here is the code :

       try
        {

            SerialPort port = new SerialPort(
              "COM11", 9600, Parity.None, 8, StopBits.One);

            // Open the port for communications
            port.Open();

            ////////////////////

            byte array1;
            array1 = Convert.ToByte("11100100", 2);

            /////
            byte[] array = new byte[1];
            array[0] = array1;



            port.Write(array, 0, 1);

            // Close the port
            port.Close();

        }
        catch (Exception ed)
        {

            MessageBox.Show(ed.Message);
        }

My question is : I tested that code and it succeeds, but the receiver was a also a PC. Is this code is general: if I connect it to the PIC, will it work in the same way?

¿Fue útil?

Solución

Yes, if you can transmit data from your port and receive it on another PC, then there's no reason why you shouldn't be able to receive it on your microcontroller - obviously you'll need the correct hardware and software at the microcontroller end too.

Otros consejos

The ports have to be opened on both transmitter and receiver.

Did you intend to to only send the first byte of your array? Why? Also closing the port immediately after sending the data could be interfering. Try leaving it open or at least putting in a delay (System.Threading.Thread.Sleep(500);

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top