7

I got a RS232 signal capture device. and it working great.

I need some help making sense of the data. Basically we bought it because we are dealing a late 80's machine controller that uses serial communication. We had little luck despite knowing the port parameters.

From the data I dumped machine control is using the break signal as part of it's protocol. I am having trouble duplicating it using VB and the MSComm. I know to toggle the break signal and on and off. But I am not sure what I am supposed to be doing with it. I am supposed to leave it on for each byte of data I send. Or send a byte of data and then toggle.

Also I am confused how I supposed to receive any data from the controller. Do I toggle a flag when the break is turned on and then when it turned off read the input?

7 Answers 7

11

Michael Burr's description of the way break works is accurate. Often, "break" signals are sent for significantly longer than one character time.

These days, "Break" is infrequently used in serial comms, but the most common use is as a 'cheap' way of providing packet synchronization. "Break" may be sent before a packet starts, to alert the receiver that a new packet is on the way (and allow it to reset buffers, etc.) or at the end of a packet, to signal that no more data is expected. It's a kind of 'meta-character' in that it allows you to keep the full range of 8 or 7-bit values for packet contents, and not worry about how start or end of packet are delineated.

To send a break, typically you call SetCommBreak, wait an appropriate period (say, around 2 millseconds at 9600 baud) then call ClearCommBreak. During this time you can't be sending anything else, of course.

So, assuming that the protocol requires 'break' at the start of the packet, I'd do this (sorry for pseudocode):-

procedure SendPacket(CommPort port, Packet packet)
{
    SetCommBreak(port)
    Sleep(2);  // 2 milliseconds - assuming 9600 baud. Pro-rata for others
    ClearCommBreak(port)

    foreach(char in packet)
        SendChar(port, char)
}

Pseudocode for a receiver is more difficult, because you have to make a load of assumptions about the incoming packet format and the API calls used to receive breaks. I'll write in C this time, and assume the existence of an imaginary function. WaitCommEvent is probably the key to handling incoming Breaks.

bool ReadCharOrBreak(char *ch); // return TRUE if break, FALSE if ch contains received char

We'll also assume fixed-length 100 byte packets with "break" sent before each packet.

void ReadAndProcessPackets()
{
  char buff[100];
  int count;

  count = 0;

while (true)
{
  char ch;
  if (ReadcharOrBreak(ch))
    count = 0; // start of packet - reset count
  else 
  {
     if (count < 100)
     {
       buff[count++] = ch;
       if (count == 100)
         ProcessPacket(buff);
     }
     else 
       Error("too many bytes rx'd without break")
  } 
} 

WARNING - totally untested, but should give you the idea...

For an example of a protocol using Break, check out the DMX-512 stage lighting protocol.

The start of a packet is signified by a Break followed by a "mark" (a logical one) known as the "Mark After Break" (MAB). The break signals end of one packet and the start of the next. It causes the receivers to start reception. After the break up to 513 slots are sent.

3
  • Thanks for this. I knew the Serial 101 stuff that the other answer had but you answered partly what I am looking for. A couple of followups. 1) Do you know if any protocol on the web where break is used. 2) what happens when you are receiving a break signal how you process that. You gave a send example how does it work on receive?
    – RS Conley
    Aug 14, 2009 at 19:39
  • This answers the first part of the OPs question, but not the second - how to tell if break is being turned on/off from the DCE.
    – J. Polfer
    Aug 14, 2009 at 19:40
  • I went ahead and implemented the API calls necessary for Roddy's answer to work. The complete solution is posted at the bottom of this thread.
    – mike
    Jan 6 at 21:55
8

A break signal is an invalid character. When the RS-232 line is idle, the voltage is in the 'mark' (or '1') state (which is -12 volts if I remember right). When a character is sent, the protocol toggles the line to the 'space' (or '0') state for one bit time (the start bit) then toggles the signal as appropriate for the data (the data bits) and any parity bits. It then holds the line in an idle/mark (or 1) state for a number of bits defined by the stop bits, which is typically configurable (usually 1 stop bit in my experience).

Since there is always some period of time where the line will be in a mark state between data characters, the start of a character can always be recognised. This also means that the longest period of time that the line can be in a space state is:

1 start bit + however many data bits + a parity bit (if any)

A break signal is defined as holding the line in the space state for longer than that period of time - no valid data byte can do that, so the break 'character' isn't really a character. It's a special signal.

As far as when you need to issue a break signal depends entirely on the protocol being used.

1
  • 1
    Trivia on the number of stop bits - it was typically 2 for 110 bps, 1 for all other rates. Hooray for Teletypes! Aug 14, 2009 at 19:24
1

'Break' was intended for when the line synchronization got totally mixed up.

I am supposed to leave it on for each byte of data I send. Or send a byte of data and then toggle.

Try sending a nice long 'break' signal (500 ms?) then wait a bit (50 ms?) then send your data.

1

Sending break can be achieved by:

  • lowering the bit-rate
  • sending 0x00 which will seem as break.
  • change bit-rate back.

During the break, it won't be possible to receive data since the bit-rate is not correct.

I used this for Linbus communication which has 1 master sending break then 0x55 as sync.

1

I helped out a friend and implemented the Comm Break in C#

It can be used as an extension method:

System.IO.Ports.SerialPort myPort = new System.IO.Ports.SerialPort("COM1");
              //... serial communications code
            myPort.SendCOMMbreak(5); //sends a break for rougly 5ms

Or a direct call:

RS232_Com_Break.UseWinAPItoSendBreak("COM1", 5); //aquires serial port and sends a break for rougly 5ms

Here is the complete code for the supporting class:

using System;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Threading;

public static class RS232_Com_Break
{
    /*===================================================================================
     *  EXTENSION METHOD TO HOLD THE VOLTAGE ON A COMM PORT HIGH FOR X(ms), 
     *     for talking to old devices that need an RS232 "break" signal sent.
     *     
     *          1/5/23 Blue Mosaic Software ~mwr
     *          
     *       Use:   System.IO.Ports.SerialPort myPort = new System.IO.Ports.SerialPort("COM1");
     *              ... serial communications code
     *             myPort.SendCOMMbreak(5); //sends a break for rougly 5ms
     *
     *        Or: RS232_Com_Break.UseWinAPItoSendBreak("COM1", 5); //aquires serial port and sends a break for rougly 5ms
     * ==================================================================================*/


    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAsAttribute(UnmanagedType.Bool)]
    static extern bool SetCommBreak([InAttribute] IntPtr fileHandle);

    [DllImport("kernel32.dll")]
    static extern bool ClearCommBreak(IntPtr hFile);

    public const short FILE_ATTRIBUTE_NORMAL = 0x80;
    public const short INVALID_HANDLE_VALUE = -1;
    public const uint GENERIC_READ = 0x80000000;
    public const uint GENERIC_WRITE = 0x40000000;
    public const uint CREATE_NEW = 1;
    public const uint CREATE_ALWAYS = 2;
    public const uint OPEN_EXISTING = 3;

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
        uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
        uint dwFlagsAndAttributes, IntPtr hTemplateFile);

    [DllImport("kernel32.dll")]
    static extern public bool CloseHandle(IntPtr hObject);


    /// <summary>
    /// Extension method to reset a serial port. Holds Port high (+5vdc) for the duration requested.
    ///    should work with an open or closed port.
    /// </summary>
    /// <param name="oPort"></param>
    public static void SendCOMMbreak(this SerialPort oPort, int milliseconds)
    {
        // I think it takes 1 ms to set and clear the break.
        int iPauseLength = milliseconds > 1 ? milliseconds - 1 : 1;

        //little management to insure the port is free.
        if (oPort.IsOpen)
        {
            oPort.Close();
            UseWinAPItoSendBreak(oPort.PortName, iPauseLength);
            oPort.Open();
        }
        else
        {
            UseWinAPItoSendBreak(oPort.PortName, iPauseLength);
        }
    }

    /// <summary>
    /// All the windows API calls are here, so this would be the code to use if 
    ///     not using the .NET System.IO.Ports.SerialPort object.
    /// </summary>
    /// <param name="sPortName">The name of the COM port</param>
    /// <param name="iPauseLengthMS">pause time in milliseconds</param>
    public static void UseWinAPItoSendBreak(string sPortName, int iPauseLengthMS)
    {
        //get a handle to the port.  Once we have done this sucessfully, it is unavalible elsewhere
        IntPtr port_ptr = CreateFile(sPortName, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

        if (port_ptr.ToInt32() == INVALID_HANDLE_VALUE) // -1 = oops
        {
            // Did not get a handle.  We need to ask the framework to marshall the win32 error code to an exception
            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
        }
        else
        {
            SetCommBreak(port_ptr);
            Thread.Sleep(iPauseLengthMS);
            ClearCommBreak(port_ptr);

            //Close only happens if handle achieved.
            CloseHandle(port_ptr);
        }

    }
}
0

Not really an SO question, but let me dredge up stuff from my long-past (1980s in fact) days as a comms programmer. You normally send a break by holding all bits low or high (depending on your comms hardware). So to cause a break either send the value 0x00 repeatedly for about half a second, or the value 0xFF.

13
  • 1
    You've forgotten about start and stop bits. Sending either 0x00 or 0xff won't be the same as a break.
    – Roddy
    Aug 14, 2009 at 19:08
  • Actually, for most 1980s hardware (IIRC) correctly, it was. The hardware is detecting a prolonged voltage difference, not the individual bits.
    – anon
    Aug 14, 2009 at 19:10
  • 1
    You're right about the prolonged voltage difference, but because of start & stop bits no valid data character can perform a break signal. Aug 14, 2009 at 19:13
  • 1
    @Neil, uarts detect "break" in hardware by the line being permanently in a "space" state for N clocks, without transitions. Sending loads of zero bytes MUST be distinguishable from break, otherwise you can't send loads of zero bytes... Even old-skool uarts like the early 80's 6850 datasheetcatalog.org/datasheet/motorola/MC6850.pdf have special register bits to enable/disable the BREAK condition.
    – Roddy
    Aug 14, 2009 at 19:50
  • 1
    @Neil - Maybe it's just a nomenclature issue: I think some protocols refer to a "break character" (which could typically be 00 or FF) to perform the same re-synchronization function that a 'true' break can be used for.
    – Roddy
    Aug 14, 2009 at 21:06
0

You should be able to see the data that the port is sending. You'll need a null-modem cable, a computer with a serial port (or a serial-USB dongle) and a terminal program (as HyperTerminal on Windows -- not included in Vista). If you configure your terminal program adequately (correct speed, number of bits for data, the correct setting of start-stop, and correct port) all the data will be show on screen. Sometimes it is requiered to hit the enter key to start seeing the data. You can toggle the setting for the terminal program during the test to see if something changes ("noise" to data).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.