SerialPort
-
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:
-
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:
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.
-
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:
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 <--
-
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:
-
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:
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...