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

  • SEARCH
  • Home
  • 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 6649817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:49:10+00:00 2026-05-26T00:49:10+00:00

Thanks in advance for your help. I’m writing a lightweight C# program to write

  • 0

Thanks in advance for your help. I’m writing a lightweight C# program to write lines to a serial port (sending commands to a PLC) for work, and I’m not a programmer. My problem is that my button won’t send the line to the serial port. Here is my 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.IO.Ports;

    namespace WindowsFormsApplication1 {
        public partial class Form1 : Form {

        string command = "Turn Motor";
        SerialPort COM1 = new SerialPort("COM1");

        public Form1() {
            InitializeComponent();
            COM1.Open();

        }

        private void button1_MouseClick(object sender, MouseEventArgs e) {

            COM1.WriteLine(command);

        }

    }

What can I do to make the button1 click send the text line to the serial port? The code must be as simple as possible as this is only run on one workstation. I’ve looked at all the examples, but have been unable to adapt them to my code. I also don’t want to blatantly rip off others’ code to get it working. Any advice would be appreciated.

EDIT: The code above is not everything, for example I’m initializing the port properly. I have another section of code that properly receives and processes strings, it’s just the sender I’m having problems with. I also created the button method using the form designer, so there should be no disconnect there.

  • 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-26T00:49:10+00:00Added an answer on May 26, 2026 at 12:49 am

    I would suggest:

    1. Use the Click event instead of the MouseClick event.

    2. If you just copied your button1_MouseClick method from an example somewhere that method probably isn’t subscribed to the click event properly. If you double click the button in Visual Studio designer it will set up a method subscribed to the click event correctly for you automatically. That’s where you would put your COM1.WriteLine(command);

    3. If both of those suggestions are incorrect, you should probably be getting an Exception thrown somewhere (unless you’re opening an incorrect port). In that case, you should describe that error in your question here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

thanks in advance for your help. I am wondering if there is a (design)
Thanks in advance for your help experts. I want to be able to copy
Thanks in advance for your help. I am wondering how I might go about
and thanks in advance for your help or any tip that could get me
Thanks in advance for your help. I cannot move forward creating or editing fields
Thanks in advance for your help. I'm completely stumped on how I can do
and thanks in advance for your help! Here's my situation: I have a set
Hello and thanks in advance for your help. I have a treeview that is
Hy everybody, Thanks in advance for your help. Here is my problem. I want
Hi everyone and thanks in advance for your help. This is the situation: I'm

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.