19148

threading and getting COM port of attached phone

i have the following 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() { } } private void Form1_Load(object sender, EventArgs e) { ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork); Thread myThread = new Thread(myThreadDelegate); myThread.Start(); // Begin communications 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); } private void serialPort1_DataReceived_1(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { string response = serialPort1.ReadLine(); this.BeginInvoke(new MethodInvoker(() => textBox1.AppendText(response + "\r\n"))); } } }

what i am trying to do is send AT COMMANDS to my phone which is attached to the computer through USB

<ol> <li>

how do i know how to configure the properties of the serial port? (like which COM is the phone on [it's attached through USB], what about baudrate and databits?)

</li> <li>

when i run the program nothing really happens, i would like to send AT COMMANDS to my phone and the textbox is there to receive the response from my phone

</li> <li>

this is my first time using threads. am i using them correctly? what is the purpose of it in the current example? is it to just have a delay between send a response?

</li> </ol>

what am i doing wrong?

Answer1:

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.

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.

I've modified the code and moved the serial port code into the thread delegate. And also hooked on the data received event.

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(() => textBox1.AppendText(response + "\r\n"))); } } }

Also check out the MSDN docs on using the SerialPort DataReceived event.

Recommend

  • SERIAL PORT doesn't accept Arabic letters? [duplicate]
  • Recieving Blank SMS SIM800 using AT Commands and Python on Raspberry Pi 2
  • Xcode quits unexpectedly every time I open Xcode
  • ASP.NET - Efficiently delay code execution for single request?
  • Why Clipboard.GetFileDropList() returns an empty collection on Remote Desktop?
  • Task.IsCancelled doesn't work
  • Finding max value in CUDA
  • javafx 8 dialog and concurrency
  • “Complex Header” not responsive in current DataTables.net build?
  • iPhone - UIWebView multiple locks
  • PHP socket_read() only gets the first byte of the stream
  • How do I fix “The program issued a command but the command length is incorrect.” error when calling
  • Is the Go HTTP handler goroutine expected to exit immediately in this case?
  • Extracting a process's exit code in the case of ThreadInterrupted
  • How can I have an actor running on one process send a message to another actor running on a separate
  • Distribute Range of Numbers between each threads
  • Exception in the iconization of JInternalFrame with DefaultDesktopManager
  • Parallel sieve of Eratosthenes - Java Multithreading
  • In Akka, is ActorContext thread safe?
  • Android: How to correctly use NotifyDataSetChanged with SimpleExpandableListAdapter?
  • Connecting to PostgreSQL in java app engine
  • Suqueries in select clause with JPA
  • 550 Access denied - Invalid HELO name
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • SharedPreferences or SQLite Database?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Bad request using file_get_contents for PUT request in PHP
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Spray.io: When (not) to use non-blocking route handling?
  • How to add date and time under each post in guestbook in google app engine
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Can I make an Android app that runs a web view in Chrome 39?
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • coudnt use logback because of log4j
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster