C# serial port programming help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Maja Gajic
    New Member
    • Jul 2011
    • 3

    C# serial port programming help

    Hello I am writing a program for my final year project at uni attempting to communicate with a CTG machine via an RS232 interface. I have the documentation of the machine and have set the correct properties of the port in the code and also in the properties of the port through my computer.
    There is no handshaking.

    I am trying to send a block in the form of

    The data code for G-mode (auto send mode) is:
    <DLE><STX>G<DLE ><ETX><CRC><CRC >

    Where DLE = 0x10 , STX = 0x02 , G = 0x47 , ETX = 0x03
    The CRC is calculated from an online calculator 16bit -CCITT(XMODEM)an d split into two (Tripple checked that it is correct)

    The above data block should initiate data transfer from the CTG machine to my computer and print to console however this is not happening. Data Recieved event is not triggered and console just says "Waiting for data" I am really not sure where to go from here and would appreciate any help! Below is my code

    Code:
    using System;
    using System.IO.Ports;
    using System.Collections.Generic;
    
    
    namespace SerialCom
    {
        class Program
        {
           
                     
            //Created serial port object and initialised
            static SerialPort com = new SerialPort(SerialPort.GetPortNames()[0],
              1200, Parity.None, 8, StopBits.One);
    
            
            
            static void Main(string[] args)
            {
    
                // Get a list of serial port names.
                string[] ports = SerialPort.GetPortNames();
    
                 Console.WriteLine("The following serial ports were found:");
    
                // Display each port name to the console.
                foreach (string port in ports)
                {
                    Console.WriteLine(port);
                }
    
                Console.ReadLine();
                
                 
                initiateDataStreaming();
    
                
                Console.ReadKey();
            
            }
    
            private static void initiateDataStreaming()
            {
                          
                //Open com port
                com.Open();
    
                if ((com.IsOpen == true))
                {
                    Console.WriteLine("Is open");
                }
    
    
                // Write Line writes string and new line value to output buffer
                // { <DLE> , <STX> , "G" , <DLE> , <ETX> , 16 BIT CRC CCITT split into two bytes}
                // byte[] byteToSend = new byte[] {0x10, 0x02, 0x47, 0x10, 0x03, 0x53, 0x13 };  (0xFFFF as initial CRC value)
               byte[] byteToSend = new byte[] {0x10,0x02,0x47,0x10,0x03,0x42,0x1F}; // 0 CRC starting value
              
                
                        
               com.Write(byteToSend, 0, byteToSend.Length);
    
                Console.WriteLine("Waiting for incoming data...");
                Console.ReadKey();
    
                com.DataReceived +=
                  new SerialDataReceivedEventHandler(com_DataReceived);
            }
    
            private static void com_DataReceived(object sender,
            SerialDataReceivedEventArgs e)
            {
                // Show all the incoming data in the port's buffer
                Console.WriteLine("Hello0"); // Checking if enter method
                Console.WriteLine(com.ReadExisting());
                
            }
    
               
        }
    }
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I suggest some error trapping in there to find at what point it is failing.
    Also try simple test code to check there is communication between each interface.

    Sometimes the connectivity needs a bit of a kick, I used to send the port high tnen low then high again, and variations of to 'wake up' the chip.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Well you have a problems.
      First, execution stops when you call Console.ReadKey (), so no code will be executed until you hit a key.
      Second, you should attach that eventhandler before you do any data sending. Otherwise the data could have come in well before you attach the handler

      Comment

      Working...