Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1240
  • Last Modified:

C# control Servo Motor over com port problem

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
Winston Smith
Asked:
Winston Smith
  • 3
1 Solution
 
Winston SmithDeveloperAuthor Commented:
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
 
viki2000Commented:
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
 
viki2000Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now