Maja Gajic

Maja Gajic

  • NA
  • 3
  • 2.5k

Serial Port Communications Help

Jul 11 2011 10:26 PM
Hi I am working on a biomedical project at Univeristy and we need to communicate with a fetal CTG machine via its  RS232 interface. To begin with I am just trying to send the initial packet to start communications and display the result on the console (Later I will have to process this data).


<DLE><STX>G<DLE><ETX><CRC><CRC>        Where CRC is the 16bit CCITT CRC split into two bytes

The above is what needs to be sent to the CTG to initiates coms, I am worried that I am not sending it correctly in the code below, The serial port settings are definitely correct.



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)
        { 
           
            initiateDataStreaming();
                       
            Console.ReadKey();
       
        }


        private static void initiateDataStreaming()
        {
                     
            //Open com port
            com.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,0x42,0x1F};
           
                     
           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());
           
        }


           
    }
}


When I run the console just says "Waiting for incoming data"
I cannot get the CTG to send data, I have checket the CRC a few times and have tried different CCITT codes to no avail.
If anyone is able to see what is wrong or able to help would be much appreciated, We have clinical trials ready to go in hospital that are waiting on me to get this working so any help is much appreciated!


Answers (4)