Forum Index > XonXoff handshaking

By: Guest - Bill Posted on: Sep 19 2010 at 06:31:22 AM
To Whom It May Concern:

I want to use your OCX in an application to send to, and receive from a CNC machine tool. I have been using MSComm32 with varying degrees of success. Hardware handshaking seems to work well, but the results are sketchy using XonXoff handshaking.

I have included part of the code Iím using for the Initialisation, Send, and Receive.

In the Comm1_OnComm routine, I use the comEvReceive event to receive data when the application is set up to receive, when blnReceive is True, and the same event to capture the control character to toggle the blnXonXoff flag, when the application is set to send, when blnReceive is True.

1. I have seen examples on the Microsoft web site for capturing XonXoff characters and they are very close to how I poll for an incoming control character in my comEvReceive event code. However, if polling for control characters in this way, why is it necessary to set the handshaking to XonXoff? Would it still not work if handshaking was set to None, as Iím polling for the control character, and toggling the blnXonXoff to stop or start transmitting data in Send_To_Machine routine.

2. I am yet to identify an XonXoff character being received in the Comm1.Input buffer through the comEvReceive event. Am I missing something here, or is this control code interpreted by the hardware when XonXoff handshaking is set, and therefore does not appear in the Comm1.Input buffer?

The cable Iím using in the XonXoff setup is as follows, this cable works fine with other software set for XonXoff handshaking.
PC DB9 to Machine DB25
2 to 2
3 to 3
5 to 7
4 and 5 bridged
6, 8 and 20 bridged

I want to be able to set the machine tool control to send a file and then start the transmission by starting the receive part of my application from the PC, and I want to be able to set the PC application to send, and then start the transmitting by initiating a program input from the machine tool. All of what I want to do is possible with other software Iím aware of.

I look forward to your reply.

Best regards,


Public Sub Init_Comms(Receive As Boolean)

'Close the Port if open to avoid an error when initialising
If Comm1.PortOpen Then
Comm1.PortOpen = False
End If

'Initialise MSComm1
With Comm1
.CommPort = intCommPort
.Handshaking = intHandShake
.InBufferSize = 1024
.OutBufferSize = 512
.RThreshold = 1
.SThreshold = 1
.RTSEnable = False
.DTREnable = False
.Settings = strCommSetting
.InputLen = 0
.EOFEnable = False
.PortOpen = True
End With

'Set the blnReceive flag for use in the comEvReceive event in Comm1_OnComm
blnReceive = Receive

'If blnReceive is true then the application is set to receive.
If blnReceive Then
With Comm1
'If the handshaking is Xon Xoff send a control
'character to the machine to start sending
If .Handshaking = comXOnXoff Then
If (.InBufferSize - .InBufferCount) / .InBufferSize * 100 > 80 Then
.Output = Chr(17)
End If
End If
End With
'If blnReceive is false then the application is set to send.
Send_To_Machine (strFileName2Send)
End If

End Sub

Sub Send_To_Machine()

'This is the internal loop of a loop that gets a string of data
'upto an EOB from a text file. This data is loaded into the string
'variable "strOutString"
'Abort Send_To_Machine if Excape flag is set
If blnExcape Then Exit Sub
'Execute if the hardware handshake line
'is high, or the XonXoff flag is set
If MSComm1.CTSHolding Or blnXonXoff Then
If i = 0 Then
'Transmit Header detail if it exists
For j = 0 To UBound(strHeader)
Delay (intDelayCharacters)
MSComm1.Output = strHeader(j)
Next j
i = 1
End If
'Delay after each character
Delay (intDelayCharacters)
'Get 1 character from strOutString
temp = Mid$(strOutString, 1, 1)
MSComm1.Output = temp
'A delay after each character, if required,
'can be set with intDelayCharacters
Delay (intDelayCharacters)
'This marks the end of the file being sent
If temp = "%" And pos1 > 0 And Not blnSendComplete Then
blnSendComplete = True
Exit Do
End If
'strOutString is truncated by the one character just sent
If Len(strOutString) > 1 Then
strOutString = Right(strOutString, Len(strOutString) - 1)
Exit Do
End If
'End If
MSComm1.RTSEnable = True
End If

End Sub

Private Sub Comm1_OnComm()

Select Case Comm1.CommEvent
' Events
Case comEvReceive ' Received RThreshold # of chars.
'Retrieve the data from the Input buffer
Buffer = Comm1.Input
'Print to immediate window to see what
'the unprintable characters are
Debug.Print Asc(Buffer)

'Handle according to how the blnReceive flag is set
If blnReceive Then
'Handle this way if the application is set to receive data.
'Send to the ShowData routine to process and store the data.
ShowData Buffer
'Handle this way if the application is set to send data.
If (InStr(Buffer, Chr$(0)) > 0) Or (InStr(Buffer, Chr$(intStartVal)) > 0) Then
blnXonXoff = True
ElseIf InStr(Buffer, Chr$(intStopVal)) > 0 Then
blnXonXoff = False
End If
End If
'Used to illuminate fake leds on the
'form when using hardware handshaking
Case comEvCTS ' Change in the CTS line.

If Comm1.CTSHolding Then
End If

'Used to illuminate fake leds on the
'form when using hardware handshaking
Case comEvDSR ' Change in the DSR line.
If Comm1.DSRHolding Then
End If

End Select

End Sub

By: Support Posted on: Sep 19 2010 at 04:37:33 PM
You said that you're using xOn xOff flow control.

But you appear to be trying to detect the xOn/xOff control codes in your application.

That can not work.

If flow control is set to xOnxOff flow control then Windows uses the xonxoff control codes for flow control so they get stripped out of the data by the windows com driver so your application would never see them.

By: Guest Posted on: Sep 20 2010 at 02:13:14 AM
Thankyou for your prompt reply, I suspected that was the case, but Iíve not been able to get information that states the fact as definitively as you have.

I just took another look at the example on the MS web site and note that their example is using comRTS as the handshake method; I missed seeing that when I first looked at it some time ago.

If using XonXoff handshaking, given that I want to be able to set the application to send, and then invoke the machine control to call the file to be sent to it, should I remove the polling for all control characters in the comEvReceive event, where it toggles the blnXonXoff flag, and remove that flag from the DO LOOP in the Send_To_Machine routine? Will the sending of the file by the PC application be started by an Xon signal being sent by the machine tool? When I was debugging this code whilst connected to the machine, I saw that when the machineís program input button was pressed, chr(201) was being captured by the comEvReceive event and I was using this character to toggle blnXonXoff flag True and start LOOP in the Send_To_Machine routine. However, when I compiled the program and installed it on the two other computers that are used to send programs to the machine, this event seemed not to be triggered when the program input button was pressed. The OS of the other two computers was Win2000 and XP respectively. The OS of my development computer is XP and, the compiled program still captured this character on this computer.

The parameter listing for the machine tool control indicates that a parameter bit is to be set so that the control uses XonXoff data control. Accordingly, I assume that the machine outputs this signal when the program input button is pressed.

Is there any example code using XonXoff handshaking that comes with your Comm32 OCX package? I hope that you are able to set me straight with matter.

Best regards,


By: Support Posted on: Sep 20 2010 at 09:04:38 AM
Why not take a look at something like

Using DNCRemo the dnc software sits waiting and users can use the cnc control to 'request' specific cnc files, by name, from the dnc server.

That's if you want to request file by name without having to keep walking from the cnc to the PC.

If you're drip feeding the same file to the cnc then I'd suggest which has flow control mode "xonxoff+wait" as well as autorepeat.

Those are not free programs but at just !90USD might still be cheaper than building it yourself from scratch.

By: Bill Posted on: Sep 20 2010 at 09:20:49 AM
Thanks for the reply.

The editor and customer data base components of this application are already finished and work extremely well, and I purchased your Comm32 Communication OCX today; accordingly, I want to finish this project.

Communication using hardware handshaking with this application works without a hitch, but some machine controls donít have this mode of handshake and only software handshaking can be used. Further, I donít want to give up before mastering this aspect of communication with a Windows app. Accordingly, if I could get some guidance with regards to using XonXoff handshaking I would appreciate it.

Best regards,

By: Support Posted on: Sep 20 2010 at 10:46:59 AM
There's no special coding required to use xonxoff.

But I think your problem is that you're trying to make the transfer wait until the CNC operator hits the read or cycle start button on the CNC. That would work with RTS/CTS because the CNC holds the computer's cts low until the cnc user hits the start button on the cnc.

With RTS/CTS the flow of data is always blocked until the CNC raises its RTS which happens when the user hits the cnc start button.

But using xonxoff it's the other way round. The flow of data is open until the cnc sends xoff and it won't send xoff untill it has a buffer full of data and it won't get a buffer full of data because the cnc isn't listening until you hit the start button on the cnc.

So I'm afraid simple xonxoff isn't going to work for you. You need a kind of xonxoff-wait which does not exist in windows at all but could be emulated in software.

Two methods.

1. Do it all in your application. That's set flow control to NONE and in your application you will get the control codes so you can decide to start/stop sending based on what code you get. This is quite simple. In the oncom event you see xon so set a global variable TX=true and when the oncom sees xoff you set the global variable TX=false and in your application you only send data if TX is true. Be warned though - there's a lot of layers of driver code especially with USB and other addon adapters so by the time yur application sees xoff the cnc has buffer has already overflowed.

2. At the very start of the transfer you have flow control set to NONE so that your application can sit waiting for the xon control code. Because flow is 0 your application will see the dc control codes. Once you get the dc1 code you enable xonxoff flow control and only then do you start sending data and from that point the com driver handles flow control for you. At the end of the transfer you switch flow back off again ready to start waiting for the dc1 code.

By: Bill Posted on: Sep 20 2010 at 11:24:25 AM
Thanks for your explanation and help, this method is very clear to me; Iíll give it a try.

Best regards,

By: Guest Posted on: Aug 17 2011 at 03:24:29 PM
Thanks alot - your awsner solved all my problems after several days struggling


Reply - add comment to this topic
You may enter letters, numbers and standard punctuation only. HTML and other scripts/tags will be rejected
  Topic:- XonXoff handshaking

Your Name


Forum scripts and databases - Copyright (c) 2009 - 2012