Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SerialPort

SerialPort

Scheduled Pinned Locked Moved C#
question
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tichaona J
    wrote on last edited by
    #1

    Hi - I am trying to send a simple string over an established Bluetooth connection using serial port. The probelm here is I don't know if what I am doing is correct as neither application are throwing any exceptions. Here is the code for the remote application private void btnConnect_Click(object sender, EventArgs e) { try { SerialPort inPort = new SerialPort("COM2", 9600); inPort.Open(); string message = "Hello"; inPort.Write(message); inPort.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } On the other end I have used an event handler: private void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e) { try { SerialPort inPort = new SerialPort("COM7", 9600); inPort.Open(); string mess = inPort.ReadLine(); MessageBox.Show("This worked..."); inPort.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } When I click on the button on the external application nothing happens? Any ideas????:confused:

    L R L T 4 Replies Last reply
    0
    • T Tichaona J

      Hi - I am trying to send a simple string over an established Bluetooth connection using serial port. The probelm here is I don't know if what I am doing is correct as neither application are throwing any exceptions. Here is the code for the remote application private void btnConnect_Click(object sender, EventArgs e) { try { SerialPort inPort = new SerialPort("COM2", 9600); inPort.Open(); string message = "Hello"; inPort.Write(message); inPort.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } On the other end I have used an event handler: private void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e) { try { SerialPort inPort = new SerialPort("COM7", 9600); inPort.Open(); string mess = inPort.ReadLine(); MessageBox.Show("This worked..."); inPort.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } When I click on the button on the external application nothing happens? Any ideas????:confused:

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      There could be dozens of things wrong, getting a serial communication up and running requires all of them being right. Here are some suggestions: - start by checking your physical connection by the simplest means, e.g. one party is constantly sending text (say one line every second), the other party uses HyperTerminal to receive them. Now experiment with cables, connectors, port settings, until characters are coming in. - only when that works is it worthwhile expanding the software. One immediate hint: ReadLine waits for a line of text, i.e. some characters ending on SerialPort.NewLine (which defaults to CR or LF or both, I can't remember). Your Write isn't sending any of them, try WriteLine. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      1 Reply Last reply
      0
      • T Tichaona J

        Hi - I am trying to send a simple string over an established Bluetooth connection using serial port. The probelm here is I don't know if what I am doing is correct as neither application are throwing any exceptions. Here is the code for the remote application private void btnConnect_Click(object sender, EventArgs e) { try { SerialPort inPort = new SerialPort("COM2", 9600); inPort.Open(); string message = "Hello"; inPort.Write(message); inPort.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } On the other end I have used an event handler: private void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e) { try { SerialPort inPort = new SerialPort("COM7", 9600); inPort.Open(); string mess = inPort.ReadLine(); MessageBox.Show("This worked..."); inPort.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } When I click on the button on the external application nothing happens? Any ideas????:confused:

        R Offline
        R Offline
        Rajesh Anuhya
        wrote on last edited by
        #3

        private void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e)
        {
        try
        {

        SerialPort inPort = new SerialPort("COM7", 9600);
        inPort.Open();

        string mess = inPort.ReadLine();

        MessageBox.Show("This worked...");

        inPort.Close();

        }
        catch (Exception ex)
        {
        MessageBox.Show(ex.Message);
        }
        }

        in the above handler comport should be open previously, the receive comport should be open before sending the data by the sender comport., And also problem may be the comport settings as said in the above answer.., check all the comport settings are correct before starting your serialport communication.

        Rajesh B --> A Poor Workman Blames His Tools <--

        1 Reply Last reply
        0
        • T Tichaona J

          Hi - I am trying to send a simple string over an established Bluetooth connection using serial port. The probelm here is I don't know if what I am doing is correct as neither application are throwing any exceptions. Here is the code for the remote application private void btnConnect_Click(object sender, EventArgs e) { try { SerialPort inPort = new SerialPort("COM2", 9600); inPort.Open(); string message = "Hello"; inPort.Write(message); inPort.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } On the other end I have used an event handler: private void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e) { try { SerialPort inPort = new SerialPort("COM7", 9600); inPort.Open(); string mess = inPort.ReadLine(); MessageBox.Show("This worked..."); inPort.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } When I click on the button on the external application nothing happens? Any ideas????:confused:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Use the sample from Microsoft : http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx[^]

          1 Reply Last reply
          0
          • T Tichaona J

            Hi - I am trying to send a simple string over an established Bluetooth connection using serial port. The probelm here is I don't know if what I am doing is correct as neither application are throwing any exceptions. Here is the code for the remote application private void btnConnect_Click(object sender, EventArgs e) { try { SerialPort inPort = new SerialPort("COM2", 9600); inPort.Open(); string message = "Hello"; inPort.Write(message); inPort.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } On the other end I have used an event handler: private void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e) { try { SerialPort inPort = new SerialPort("COM7", 9600); inPort.Open(); string mess = inPort.ReadLine(); MessageBox.Show("This worked..."); inPort.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } When I click on the button on the external application nothing happens? Any ideas????:confused:

            T Offline
            T Offline
            Tichaona J
            wrote on last edited by
            #5

            After alot of hair pulling and banging my head on the wall, finally figured out why by code isn't working. First of all, there are 4 serial ports available on my PC. The fact that I am trying to use the same serial port to test my Windows mobile application and send data over the same serial port explains why. So one would then suggest trying to use another port, well there is two problems with this; first active sync lunches and takes this spare serial port(have tried killing the process, this didn't help), Secondly also discovered that my Windows mobile device only supports on connection at a time so hence while I am testing it I can not open another port, which really sucks... So now I am diching using bluetooth all together and am using WiFi. I know what your thinking (won't I run into the same problems as with bluetooth), well with WiFi I am going to create a dummy application (desktop) that will sent strings, if this works I will then rebuild it as a Windows Mobile application, hopefully it should work...Thanks for the help everyone...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups