Join The Works program to have access to the most current content, and to be able to ask questions and get answers from Revelation staff and the Revelation community

At 16 MAY 2007 03:16:01PM Ana Melisa Heras wrote:

Hi,

Has anyone got a sample code of how to Open/Close/Read/Write from COM Port with these functions:

CreateFile,ReadFile,WriteFile,GetCommProperties,GetLastError,GetCommState,SetCommState

or using MSCOMM.OCX

which one is easier to use?

Thanks


At 16 MAY 2007 08:48PM Richard Bright wrote:

Ana,

I have done some work on mmcomm and have it working - created something like a HyperTerminal form to provide a test bed. The trick of serial comm is that you need to 'listen' to the comm buffer and take out whats there and re-assemble into useable chunks of data. Contact me for more info.

One of my next projects to change to the Win API which should be much more robust than an a pretty dated ocx. I know Revelation was looking at typing the Win API - you might like to check with Mike as to progress.

Richard Bright

r.bright@ark.co.nz


At 17 MAY 2007 09:52PM Colin Rule wrote:

For what its worth, we have some code, but its work in progress at the moment. Works fine for 16bit, but with 32 bit we had to change it all as Microsoft use different methods.

Code is below, but user beware…

You will of course need to declare the functions for the DLLs, we have applied our own extensions to avoid risk of Revelation clashes.

Having said all that there are issues at the moment (ie it doesnt work) which we have not resolved, and we may need to use a different approach.

In the meantime, I use a VB program to do the comms, and then pass this to OpenInsight, there is a slight delay but its livable.

If you can make it work, wonderful, if not, Richards suggestions may be a better alternative, and I am just posting on the off chance it might help.

You will need to adjust the code to suit, my common and variables for passing etc.

Colin


COMPILE SUBROUTINE COMM_ACCESS(MODE,COMM_PORT_HANDLE,COMMDATA)

* STANDARD

* Open/Close Comm port, Read/Write to port

* AG - May 96

*

$INSERT ICEPAC_COMMON
DECLARE FUNCTION BLANK_STRUCT,GET_PROPERTY,STRUCT_TO_VAR,VAR_TO_STRUCT
DECLARE FUNCTION OpenComm,CloseComm,BuildCommDCB,SetCommState,ReadComm,WriteComm
DECLARE FUNCTION CreateFileCSSP32,CloseCommCSSP32,BuildCommDCBCSSP32,SetCommStateCSSP32,ReadFileCSSP32,WriteFileCSSP32
DECLARE FUNCTION GetLastError,GetLastErrorCSSP32
BEGIN CASE
	CASE MODE=OPEN"	;GOSUB OPEN_PORT
	CASE MODE=CLOSE"	;GOSUB CLOSE_PORT
	CASE MODE=READ"	;GOSUB READ_FROM_PORT
	CASE MODE=WRITE"	;GOSUB WRITE_TO_PORT
END CASE

RETURN

OPEN_PORT:

COMM_ERRORS="
COMM_ERRORS=-1 ;COMM_ERRORS=Invalid or unsupported device"
COMM_ERRORS=-2 ;COMM_ERRORS=Device already in use"
COMM_ERRORS=-10;COMM_ERRORS=Device already in use"
COMM_ERRORS=-12;COMM_ERRORS=Device baud rate is invalid"
PORT	 =COMMDATA
BAUD_RATE=COMMDATA
DATA_BITS=COMMDATA
STOP_BITS=COMMDATA
PARITY	 =COMMDATA1,1
FLOW_CONTROL=COMMDATA1,1
PORT_SETTINGS=PORT:":":BAUD_RATE:",":PARITY:",":DATA_BITS:",":STOP_BITS:\00\
INQSIZE=1024
OUTQSIZE=128
IF BITSIZE=16 THEN
	PORT=PORT:\00\
	COMM_PORT_HANDLE=OpenComm(PORT,INQSIZE,OUTQSIZE)
END ELSE
	ShareMode=1
	CreationDisposition=3
	COMM_PORT_HANDLE=CreateFileCSSP32(PORT,"",ShareMode,"",CreationDisposition,"","")
END
LOCATE COMM_PORT_HANDLE IN COMM_ERRORS USING @VM SETTING POS THEN
	COMM_PORT_HANDLE=COMM_ERRORS
	RETURN
END
DCBCode=DCB"
DCB_STRUCT=BLANK_STRUCT(DCBCode)
IF BITSIZE=16 THEN
	RETVAL=BuildCommDCB(PORT_SETTINGS,DCB_STRUCT)
END ELSE
	RETVAL=BuildCommDCBCSSP32(PORT_SETTINGS,DCB_STRUCT)
END
COMM_PARAMS=STRUCT_TO_VAR(DCB_STRUCT,DCBCode)
BEGIN CASE
	CASE FLOW_CONTROL=X"
		COMM_PARAMS=3
	CASE FLOW_CONTROL=H"
		COMM_PARAMS=5
		COMM_PARAMS=9
		COMM_PARAMS=64
END CASE
DCB_STRUCT=VAR_TO_STRUCT(COMM_PARAMS,DCBCode)
IF BITSIZE=16 THEN
	RETVAL=SetCommState(DCB_STRUCT)
END ELSE
	RETVAL=SetCommStateCSSP32(DCB_STRUCT)
END

RETURN

CLOSE_PORT:

IF BITSIZE=16 THEN
	RETVAL=CloseComm(COMM_PORT_HANDLE)
END ELSE
	RETVAL=CloseCommCSSP32(COMM_PORT_HANDLE)
END

RETURN

READ_FROM_PORT:

IF BITSIZE=16 THEN
	BUFF_LEN=50
	COMMDATA=STR(\00\,BUFF_LEN)
	RETVAL=ReadComm(COMM_PORT_HANDLE,COMMDATA,BUFF_LEN)
	COMMDATA=FIELD(COMMDATA,\00\,1)
END ELSE
	BytesToRead=1024
	BytesRead=0
	BUFF_LEN=50
	COMMDATA=50
	RETVAL=ReadFileCSSP32(COMM_PORT_HANDLE,COMMDATA,BytesToRead,BytesRead,"")
	ERROR="
	RETVAL2=GetLastErrorCSSP32(ERROR)
END

RETURN

WRITE_TO_PORT:

COMMDATA=COMMDATA:\00\
BUFF_LEN=LEN(COMMDATA)
IF BITSIZE=16 THEN
	RETVAL=WriteComm(COMM_PORT_HANDLE,COMMDATA,BUFF_LEN)
END ELSE
	RETVAL=WriteFileCSSP32(COMM_PORT_HANDLE,COMMDATA,BUFF_LEN)
END

RETURN


At 23 MAY 2007 09:11PM Ana Melisa Heras wrote:

Thank for your help with the code, but How can i get your program in VB , at this moment is a great option.

I need to comunicate with a MS2200 series StratosS™

This is the link :

http://www.metrologic.com/corporate/products/pos/MS2200.htm

Can you give some options , what can You do for me?

Thanks


At 30 MAY 2007 11:59PM Colin Rule wrote:

I presume you intend to use the RS232 interfaces.

You no doubt have a cable for this, we used to solder them up ourselves in the old days.

Below is my VB program, there is stuff in it which wont be relevant, and I use to pass info back and forwards, log files and the like.

I have a small VB form with a timer control and the MSComm control on it, as well as a edit box to advise whats going on, but this EXE is run as a background program normally.

It will need to have many changes to work with this scanner, as there may be a whole range of things it needs. It probably has the ability to send and receive information, but you need to know the procotols for these.

If you give me your Email address I can send some screen shots and examples.

Good luck

Colin

—– VB CODE FOLLOWS ———

Option Explicit

Dim sPath   As String
Dim sCommand As String
Dim sArgs()  As String
Dim InBuff As String
Dim sText As String
Dim LastBuff As String
Dim SoundFile As String
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal Filename As String, ByVal Flags As Long) As Long
   Private Sub Form_Load()
       sCommand=Command & ",,,,,,,"
       If sCommand=" Then
           sCommand=Comm1,9600,N,8,1,C:\,"
          'sCommand=Comm2,9600,N,8,1,C:\,"
       End If
       sArgs()=Split(sCommand, ",")
       sPath=sArgs(5)
       SoundFile=sArgs(6)
       Text.Text="
       LastBuff="
       If sPath=" Then sPath=C:\"
       If Dir(sPath & "digit.log")  "" Then
          Kill sPath & "digit.log"
       End If
       Open sPath & "digit.log" For Append As #99
       WriteToLog "Command line=" & sCommand & "'"
       WriteToLog "Path=" & sPath & "'"
       WriteToLog "Comm port=" & Right(sArgs(0), 1) & "'"
       WriteToLog "Comm port settings=" & sArgs(1) & "," & sArgs(2) & "," & sArgs(3) & "," & sArgs(4) & "'"
       WriteToLog "Sound File=" & SoundFile & "'"
       With MSComm1
          .CommPort=Right(sArgs(0), 1)
          .Handshaking=2 - comRTS
          .RThreshold=1
          .RTSEnable=True
          .Settings=sArgs(1) & "," & sArgs(2) & "," & sArgs(3) & "," & sArgs(4)
          .SThreshold=1
          .PortOpen=True
          .InBufferCount=0
          .InputLen=1
       End With
       WriteToLog "Comm port opened"
       WriteToLog "RTS Enabled"
       WriteToLog "Comm port buffer cleared"
       WriteToLog "Ready to receive data..."
       InBuff="
    End Sub
  Private Sub Form_Unload(Cancel As Integer)
      If Dir(sPath & "dinfo.txt")  "" Then
          Kill sPath & "dinfo.txt"
      End If
      MSComm1.PortOpen=False
      WriteToLog "Comm port closed"
      WriteToLog "End of session"
      Close #99
  End Sub
   Private Sub MSComm1_OnComm()
      Dim InChar As String
      Dim Counter As Integer
      Select Case MSComm1.CommEvent
          Case comEventBreak     ' A Break was received.
              Unload (Form1)
          Case comEventCDTO      ' CD (RLSD) Timeout.
              Unload (Form1)
          Case comEventCTSTO     ' CTS Timeout.
              Unload (Form1)
          Case comEventDSRTO     ' DSR Timeout.
              Unload (Form1)
          Case comEventTxFull    ' Transmit buffer full.
              Unload (Form1)
          Case comEventFrame     ' Framing Error
          Case comEventOverrun   ' Data Lost.
          Case comEventRxOver    ' Receive buffer overflow.
          Case comEventRxParity  ' Parity Error.
          Case comEventDCB       ' Unexpected error retrieving DCB]
          Case comEvCD           ' Change in the CD line.
          Case comEvCTS          ' Change in the CTS line.
          Case comEvDSR          ' Change in the DSR line.
          Case comEvRing         ' Change in the Ring Indicator.
          Case comEvSend
          Case comEvEOF
          Case comEvReceive      ' Received RThreshold # of chars.
              InChar=MSComm1.Input
              If InChar=Chr$(10) Then InChar="
              If InChar=Chr$(13) Then
                  Open sPath & "digit.txt" For Append As #1
                  Print #99, "Data received: " & InBuff
                  Print #1, InBuff
                  Close #1
                  sText=Text.Text
                  Text.Text=sText & InBuff & Chr(13) & Chr(10)
                  LastBuff=InBuff
                  InBuff="
                  If SoundFile  "" Then
                      Call sndPlaySound(SoundFile, 1)
                  End If
              Else
                  InBuff=InBuff & InChar
              End If
       End Select
    End Sub
  Sub WriteToLog(sString As String)
      Print #99, Now & " - " & sString
      sText=Text.Text
      Text.Text=sText & sString & Chr(13) & Chr(10)
  End Sub

Private Sub Timer1_Timer()

  If Dir(sPath & "dinfo.txt")  "" Then Unload Me

End Sub

View this thread on the Works forum...

  • third_party_content/community/commentary/forums_works/c330ed74d942ac1e852572dd0069d655.txt
  • Last modified: 2023/12/30 11:57
  • by 127.0.0.1