Solved

C# control Servo Motor over com port problem

Posted on 2012-03-22
5
1,199 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Stuck in voice control mode on your Amazon Firestick?  Here is how to turn it off!!!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

864 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now