Solved

C# control Servo Motor over com port problem

Posted on 2012-03-22
5
1,205 Views
Last Modified: 2012-07-18
I have a C# window application that needs to talk to a stepper motor assembly over a serial port. When i run hyperterminal and pass commands they are accepted and returned with no issues.

In my application i open the port, send the first command and it returns no problem. The trouble is on any subsequent command issues nothing is sent or received. Same code, different result.
Load Form
 serialPort1.PortName = "COM6";
            serialPort1.BaudRate = 38400;

            serialPort1.Open();

Open in new window

code to fire command, where e is a single char (just for testing)
// If the port is closed, don't try to send a character.
            if (!serialPort1.IsOpen) return;


            byte[] data = StrToByteArray(e.KeyChar + "\r\n");
            // Send the user's text straight out the port
            serialPort1.Write(data, 0, data.Length);

Open in new window

code to listen for receipt of data
 
 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string RxString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }
private void DisplayText(object sender, EventArgs e)
        {
            textBox1.AppendText(RxString);
        }

Open in new window

What could be the difference between what hyperterminal sends/receives and how I am doing it?

Thanks for any help, sorry if vague but i am new to serial program flow.

Cheers
0
Comment
Question by:Halon
  • 3
5 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37754420
0
 

Author Comment

by:Halon
ID: 37754663
I have found these articles myself through a Google search, way too many googles.

The problem is not in setting up and making the calls, I know it works the first time it runs, its the subsequent calls. I need to know if I have to clear a buffer or reset the connection or something? However the hyperterminal does it works fine so if someone has links to how it works or the right direction it would be great.
0
 
LVL 20

Accepted Solution

by:
viki2000 earned 500 total points
ID: 37758836
I have done it in other languages few years ago for different instruments: starting with satellite receiver, programmable power supplies, digital oscilloscopes...and I have to do it in the next months for an embedded system. I do not program daily, only seldom when I need, few times per year...
OK, so my suggestions:
1) try to use other terminal than hyperterminal - just google it, try any trial version 30 days. If you need I can suggest some. Try for instance putty...
2) try to record, log the serial port activity using dedicated software to sniff, record the data. Analise the data and compare - the you see what is wrong.
3) If is necessary create virtual serial ports where the data is sent.

I am sure that is a simple answer, a direct solution to your question if somebody has in mind and works now with the protocol, but I suggest the above approach because you will learn more and you can control later all the situations.
I just remember that I controlled the serial port even from Excel in VBA.
0
 
LVL 20

Expert Comment

by:viki2000
ID: 37758905
0
 
LVL 20

Expert Comment

by:viki2000
ID: 37758939
Just log/record the 2 situations and compare - you can find what is different.
I expect that is something related with the end character or they way how the port is open/close.
For your second set of commands, I think the port is not ready to receive info. ou have to look at the control lines, not only at the data.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question