开发者

threading and getting COM port of attached phone

开发者 https://www.devze.com 2023-01-03 13:05 出处:网络
i have the following code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;

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

  1. 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?)

  2. 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

  3. 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?

what am i doing wrong?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消