Solved

C# control Servo Motor over com port problem

Posted on 2012-03-22
5
1,197 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
Comment Utility
0
 

Author Comment

by:Halon
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 20

Expert Comment

by:viki2000
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The Rasberry PI is a low cost piece of hardware that you can have a lot of fun with through experimenting and building/working on projects like media players, running a low cost computer, build data loggers etc. - see: https://www.raspberrypi.org
Moving your enterprise fax infrastructure from in-house fax machines and servers to the cloud makes sense — from both an efficiency and productivity standpoint. But does migrating to a cloud fax solution mean you will no longer be able to send or re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

19 Experts available now in Live!

Get 1:1 Help Now