Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 853281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:44:03+00:00 2026-05-15T07:44:03+00:00

i have the following code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:44:04+00:00Added an answer on May 15, 2026 at 7:44 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 440k
  • Answers 440k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you are using basic server authentication (which I think… May 15, 2026 at 5:08 pm
  • Editorial Team
    Editorial Team added an answer The EntryPoint attribute should work (e.g. with an ordinal), if… May 15, 2026 at 5:08 pm
  • Editorial Team
    Editorial Team added an answer There is nothing in the codde you have posted the… May 15, 2026 at 5:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.