Solved

C# control Servo Motor over com port problem

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

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 21

Expert Comment

by:viki2000
ID: 37758905
0
 
LVL 21

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
This paper addresses the security of Sennheiser DECT Contact Center and Office (CC&O) headsets. It describes the DECT security chain comprised of “Pairing”, “Per Call Authentication” and “Encryption”, which are all part of the standard DECT protocol.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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