Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are missing serialPort1.DataReceived += serialPort1_DataReceived_1 in your code. And opening the COM port on a separate thread is quite ok. As for baud rate, stop bits etc, I would go with a safe default. 9600 baud, no parity, one stop bit, 8 data bits. That should work for most modems. You might also want to send "ATZ" first to reset the modem before sending other commands.</p> <p>As for knowing which COM port to use, see my answer on your other SO question. Either iterate the COM ports and try them all, or query with WMI.</p> <p>I've modified the code and moved the serial port code into the thread delegate. And also hooked on the data received event.</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public class ThreadWork { public static void DoWork() { serialPort1.Open(); serialPort1.Write("AT+CMGF=1\r\n"); //Thread.Sleep(500); serialPort1.Write("AT+CNMI=2,2\r\n"); //Thread.Sleep(500); serialPort1.Write("AT+CSCA=\"+4790002100\"\r\n"); //Thread.Sleep(500); serialPort1.DataReceived += serialPort1_DataReceived_1; } } private void Form1_Load(object sender, EventArgs e) { ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork); Thread myThread = new Thread(myThreadDelegate); myThread.Start(); } private void serialPort1_DataReceived_1(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { string response = serialPort1.ReadLine(); this.BeginInvoke(new MethodInvoker(() =&gt; textBox1.AppendText(response + "\r\n"))); } } } </code></pre> <p>Also check out the MSDN docs on using the <a href="http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx?queryresult=true" rel="nofollow noreferrer">SerialPort DataReceived</a> event.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload