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.
It looks like you are not initializing your Com Port. You need to set your BaudRate, Parity, DataBits, StopBits and Handshake property's. Look at the above MSDN examples for guidence. You can set the property's individualy or set them in the constructor.
According to MSDN the Default values for the above propertys are:
BuadRate = 9600
Parity = Parity.None
DataBits = 8
StopBits = StopBits.One
HandShake = HandShake.None
I would suggest:
Use the Click event instead of the MouseClick event.
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);
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.
Related
I recently got a sas expander card.
The one who gave card to me said:
It has a firmware on the chip and it can show sensor's temperature.
He wants me to develop a C# Console app to execute the firmware.
I didn't know the firmware source code looks like.
But it could be executed by PuTTy and it's connection was via RS232 Serial Port.
PuTTy connection setting:
After I click Open,press Enter and type command sys:
What I try in my C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Threading;
namespace SerialPortExample
{
class SerialPortProgram
{
// Create the serial port with basic settings
[STAThread]
static void Main()
{
SerialPort mySerialPort = new SerialPort("COM5");
mySerialPort.BaudRate = 115200;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.DtrEnable = true;
mySerialPort.ReadTimeout = 2000;
mySerialPort.WriteTimeout = 1000;
mySerialPort.Open();
if(mySerialPort.IsOpen)
{
string str= "Enter";
mySerialPort.Write(str);
}
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Console.ReadLine();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
}
}
}
What my code execute:
What is the problem with my code?
How can I execute firmware via RS232 and interact like PuTTy have done?
My Console app is using .Net Framework 4.7.2.
You are sending the string Enter (like typing ⇧E, N, T, E, R). You should probably send the ⏎ Enter key, which is represented on a terminal with the ASCII code 13, i.e. \r in a C# string.
Also you don't yet send any actual command. Try something like counters\r or sys\r.
(You currently receive the Enter back as response because that's what you sent out, and the card echoes any incoming characters so it can be used like a shell without blind typing. There is no other response yet because from the card's perspective you basically started typing the (invalid) command Enter but haven't yet submitted it with the ⏎ Enter key.)
Also I'd suggest adding the event listener for received data before sending any data, otherwise there would be a race condition in which the card could be responding before you even set up your listener and you would lose part of the data.
Additional note: Using the DataRecieved event may not even be desirable in your case.
According to docs:
The DataReceived event is not guaranteed to be raised for every byte received. Use the BytesToRead property to determine how much data is left to be read in the buffer.
This means that your DataReceived event may not even fire at all if there wasn't enough data sent in total yet (it may fire some time later with all the data at once) - but if you quit your program before that, you will never see.
As suggested here (emphasis mine):
Here is my general approach:
Use event-driven (DataReceived) code for streaming data. That is, where data is delivered at regular intervals, without specific associated commands that originate from your application.
Use polling for Command/Response protocols. These might involve a thread the you create to poll, but more frequently would be simple loops that may or may not block other operations until they complete.
So, it would be recommended to use one of the Read methods instead (see docs). There is also ReadTo and ReadLine which you may find useful. You may choose to use ReadTo(" bp1 >") for instance.
im very new to C# and is working on a personal project to send a message from Arduino when a button is pressed to my C# code listening on the serial port and writing the message in the console.
This is so far I've gotten:
using System;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static SerialPort _serialPort;
public static void Main()
{
_serialPort = new SerialPort();
_serialPort.PortName = "COM4";//Set your board COM
_serialPort.BaudRate = 9600;
_serialPort.Open();
while (true)
{
string a = _serialPort.ReadExisting();
Console.WriteLine(a);
Thread.Sleep(200);
}
}
}
}
In my console application I would also like to do other things not just wait on incoming data so i guess my option here is to use async?
I tried this: C# Async Serial Port Read
But could not get it to work.
Does anyone have any recommendation on where to start, and sorry for this noobish question i have approx 10 hours of c# experiance :).
Here are some options you can use:
Create a thread with loop calling Read() and call the event when data is received.
Use event DataReceived (but it's not recommended, here's why: long article about SerialPort in C#
Just check the stream if there any data with BytesToRead. If BytesToRead > 0, then call Read.
Use BaseStream property with BeginRead and async callback
Accoring to the article mentioned above the only reliable way is to use Read OR use BaseStream with BeginRead (async option)
I'm working on writing a simple program to move a stepper motor in C#. I have some past experience in C++, but have decided to transition over, as I'd have to reteach myself a bit of C++.
Previously, I've controlled the motor (using an Applied Motion ST5 Stepper Controller) with serial commands via PuTTY. My thought is that I could just send the same commands using C# by opening the correct COM Port (which works fine, as it crashes when I feed in a nonworking port number), and sending a string over. However, when I send a string of the same command that I had been using via serial terminal (FL1000, followed by a carriage return, it tells the motor to move 1000 steps clockwise), the motor does nothing. WriteLine should be the correct thing to use here, as it sends the string then a return, correct?
Does anybody see any glaring mistakes that would make my string not make it to the controller?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ConsoleApp3
{
class Program
{
static SerialPort comPort;
static void Main()
{
//These values in the declared serial port match what my device manager says.
comPort = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);
comPort.ReadTimeout = 5000;
comPort.WriteTimeout = 5000;
comPort.Open();
//Pauses for a moment so that I can see the console otuput.
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
string command = "FL1000";
Console.WriteLine("Moving Motor...");
//Tells the controller to move the motor 1000 steps clockwise
comPort.WriteLine(command);
//confirms that the code made it past the comPort writeline
Console.Write("Command Sent");
//Pauses for a moment so that I can see the console output.
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
}
}
}
I expect that this would move the motor 1000 steps. The only results I see are that my "markers" appear on the console. The program exits without error.
Thank you in advance!
Your command does not contain a carriage return or newline. The motor is looking for one of these to know that the command is complete.
I haven't worked with their ST5 line of motors, but other products they carry require the command to be terminated with a carriage return. Try changing your message to:
string command = "FL1000\r";
I am starting from the ground up and trying to write a simple console to interface with a serial port on a windows 7 computer.
I am using:
Visual Studio 2010 Ultimate
C# console project
Code: http://pastebin.com/0ADaEiSY
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM5");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
mySerialPort.Write("This is a test");
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.Write(indata);
}
}
}
So far i have ran this code and interfaced with a xbee module connected to my computer. That xbee module sent the serial data to another xbee connected to a msp430. The msp430 is programmed to take whatever it receives and echo it back. This works with the code I have. In my console I will get "This is a test" echoed back onto the console window.
The problem I am having is when I use a virtual serial connection to a putty window. I am using this to try to ease development and not have to use hardware all of the time. I will use HHD Free Virtual Serial Ports to create a bridged connection between two serial ports. I will connect one to the putty terminal and the other will be for my console program. When running the program I recieve the error.
"A first chance exception of type 'System.TimeoutException' occurred in System.dll"
on the line
mySerialPort.Write("This is a test");
But the "This is a test" will appear on the Putty terminal.
If I remove the "mySerialPort.Write("This is a test");" line and attempt to send data from the Putty window to the console window, nothing appears.
Again this works with my hardware solution just fine.
Please help and i will try to clarify any questions. Thank you again.
I guess the problem is in virtual utility you are using. It seems it sets pin states incorrect. If I use 2 putty instances and connect to bridged ports I see infinite sending of symbol I entered. So I think your code is fine.
When I was working on such tasks I used a special cable for connecting 2 hardware com ports (com1 and com2, if you don't have them you can try usb-to-com converters) and it worked fine.
I'm has the very same problem with HHD Free Virtual Serial Ports, but this work great with asynchronous write operation.
Also you can replace
mySerialPort.Write("This is a test");
with (for example)
var buffer = Encoding.ASCII.GetBytes("This is a test");
mySerialPort.BaseStream.BeginWrite(buffer, 0, buffer.Length, ar => mySerialPort.BaseStream.EndWrite(ar), new object());
Firstly I would like to apologize for my English language. I try to create a Windows Service which run program for BuckUp data when the computer is shutting down.
Problem is that the operating system during shutdown to kill my Windows Service before BackUp data is executed by to the end of. I changed the registry value HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout to 3600000 but it didn't help, my Windows Service is killed before it is executed. Maybe someone knows how to make the operating system does't kill the Windows Service as quickly to BackUp data could be made. Please help me, I'm waiting for your response. Below I include my code Windows Service:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace backUp_ser
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
this.CanShutdown = true;
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
protected override void OnShutdown()
{
ProcessStartInfo stratInfo = new ProcessStartInfo();
stratInfo.WindowStyle = ProcessWindowStyle.Hidden;
stratInfo.FileName = "C:\\Program Files\\Cobian Backup 10\\Cobian.exe";
stratInfo.Arguments = "list:C:\\Program Files\\Cobian Backup 10\\DB\\MainList.lst -bu -nogui -autoclose";
Process process = Process.Start(stratInfo);
process.WaitForExit(360000);
}
}
}
Apart from your query, I want to remind you that the services are running in a separate logon session and the services won't interact with the logged in desktop session (mostly).
So, you need to intercept the shutdown event in your service code. Then, you need to hold the shutdown event till you complete your backup process. You can hook those Windows events through message pumps/queues. You need to intercept the WM_ENDSESSION/ WM_QUERYENDSESSION events.
This query is already discussed in this post. You can refer that.