locked
Problem with serial communication. RRS feed

  • Question

  • ok I have done serial communication before in programs but this is the first time I have seen this.

    I have a vision System which is built into a PC with touch screen. It's like an all in one devie and it can be controlled through the RS232 port and its runnin on winXP E. The system does not have hyper terminal but does come with its own serial communication test software and I am using this to monitor send and recieve.

    I had a USB-to-Serial cable to communicate to another PC running my program. If i sent a string from my program, the vision system doesn't recieve anything but if I sent from the vision system to my program it works fine. So to elliminate my program as being the problem I fired up hyper terminal on the PC running my app. If i send from hyper terminal, the vision system recieves something but not the same characters that were sent. Again, sending from the vision system works fine.

    So i have 2 issues. 1) why is my app not sending anything? 2) why, when using hyper terminal, does the vision system recieve different characters?

    First I thought it was encoding but then surely anything sent from the vision system would look different on the PC running hyper terminal?

    Anyway, I then changed the cable over to a spare one I had and now everything works fine in hyper terminal but my app is still not sending.

    I made a quick console app to see if it was my coding. I can recieve but if I send I get nothing on the vision system.

    Any ideas as im just using simple SerialPort.WriteLine() method to send?

     

    Monday, January 24, 2011 5:28 PM

Answers

  • Sorry for the late reply, internet has been down.

    It seems there was a few issues that caused my problem.

    1) I had to set both RtsEnable and DtrEnable to true.

    2) For some reason the vision system is set to 19200 for the baud rate by default so I set that the same in my code but some of the chars didn't arrive to the vision system. Changing the baud rate down to 9600 seemed to have solved that issue. Bad cable maybe?

    Anyway thanks for the replies, gave me something to consider.

    • Marked as answer by GaztheLegend Monday, January 31, 2011 1:11 PM
    Monday, January 31, 2011 1:11 PM

All replies

  •     static void Main(string[] args)
        {
          SerialPort sp = new SerialPort();
          //sp.Handshake = Handshake.None;
          //sp.NewLine = "\r";
          //sp.Parity = Parity.Odd;
          //sp.StopBits = StopBits.One;
          //sp.DataBits = 8;
    
          bool Running = true;      
    
          while (Running)
          {
            string CMD = Console.ReadLine();
            
              if(CMD.StartsWith("open"))
              {
                try
                {
                  sp.Open();
                  Console.WriteLine("Port Open");
                }
                catch (Exception ex)
                {
                  Console.WriteLine(ex.Message);
                }
              }                 
              else if (CMD.StartsWith("com"))
              {
                try
                {
                  sp.PortName = "COM" + CMD.Split('.')[1];
                  Console.WriteLine("PortName = {0}", sp.PortName);
                }
                catch (Exception ex)
                {
                  Console.WriteLine(ex.Message);
                }
              }
              else if (CMD.StartsWith("close"))
              {
                try
                {
                  sp.Close();
                  Console.WriteLine("Port Closed");
                }
                catch (Exception ex)
                {
                  Console.WriteLine(ex.Message);
                }
              }
              else if (CMD.StartsWith("read"))
              {
                try
                {
                  Console.WriteLine(sp.ReadExisting());
                }
                catch (Exception ex)
                {
                  Console.WriteLine(ex.Message);
                }
              }
              else if (CMD.StartsWith("send"))
              {
                try
                {
                  sp.WriteLine(CMD.Split('.')[1]);              
                  Console.WriteLine("[Sent] {0}", CMD.Split('.')[1]);
                }
                catch (Exception ex)
                {
                  Console.WriteLine(ex.Message);
                }
              }
              else if (CMD.StartsWith("end"))
              {
                Running = false;
              }        
          }
        }
    

    Forgot to post some code. That is the code of a quick console program I made. I have commented out some of the setting SerialPort properties as I manually set them through control panl but it makes no difference.

    When I run it I just do the following in console

    COM.8 - set the com port to 8 which I have checked and it is correct.

    open - Opens the port

    send.TEST - sends the string "TEST". At this point I check the vision system and it recieves nothing.

    If I then send "TEST" from the vision system I go back to the console and type read which gives me "TEST"

    So why does it work with hyper terminal and not my app?

    Tuesday, January 25, 2011 9:29 AM
  • Hi Gaz25:

    Welcome to the MSDN Forum.

    Based on communication with SerialPort, there are many facts would make your application work failed. 

    Based on my experiences, if there are too may COM Ports in your machine, you should install a virtual driver with them, or you will find the higher port don't correct. But not all of them failed to work, some of the USB hubs or ethernet hub may work correct by the installation of their own drivers or management application.

    So, my suggestion is:
    1. Please make sure that the successful installation of the drivers is must.
    2. Use only one COM Port in your machine, such as COM1, because COM8 is too high Port to work, many machine does not support so high COM port.  Most of machines work with more than 8 COM Ports should use analogging virtual port to work.
    3. If you have open and close the same port with several times, please restart your machine and try it again. The COM Port may dead when you are debugging.

    If you have any questions, please feel free to tell us.

    Best Regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the C# Forum! If you have any feedback, please tell us.
    • Marked as answer by Neddy Ren Monday, January 31, 2011 2:40 AM
    • Unmarked as answer by GaztheLegend Monday, January 31, 2011 1:01 PM
    Wednesday, January 26, 2011 1:26 PM
  • In your "send" routine, are you getting an exception? 

    Do you have the correct handshaking set up? I guess you probably do, as I doubt you would receive anything.

    Is the target system waiting for any message terminator other than carriage return and line feed? Does the message coming back to you have a different message terminator? You could try using sp.Read and check the incoming raw bytes.

    I wouldn't worry about the port numbers, as suggested by Neddy Ren - I've used up to COM32 before without any problems. Also, if the drivers were not installed correctly you'd have problems opening the port in the first place. Plus, if your port was dead, you'd see an exception at some point or another.

    K

    • Marked as answer by Neddy Ren Monday, January 31, 2011 2:40 AM
    • Unmarked as answer by GaztheLegend Monday, January 31, 2011 1:11 PM
    Wednesday, January 26, 2011 11:46 PM
  • Sorry for the late reply, internet has been down.

    It seems there was a few issues that caused my problem.

    1) I had to set both RtsEnable and DtrEnable to true.

    2) For some reason the vision system is set to 19200 for the baud rate by default so I set that the same in my code but some of the chars didn't arrive to the vision system. Changing the baud rate down to 9600 seemed to have solved that issue. Bad cable maybe?

    Anyway thanks for the replies, gave me something to consider.

    • Marked as answer by GaztheLegend Monday, January 31, 2011 1:11 PM
    Monday, January 31, 2011 1:11 PM
  • Glad to hear you got it sorted

    K

    Monday, January 31, 2011 6:44 PM