Serial Port communication (OpenInsight 32-Bit)
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_COMMONDECLARE FUNCTION BLANK_STRUCT,GET_PROPERTY,STRUCT_TO_VAR,VAR_TO_STRUCTDECLARE FUNCTION OpenComm,CloseComm,BuildCommDCB,SetCommState,ReadComm,WriteCommDECLARE FUNCTION CreateFileCSSP32,CloseCommCSSP32,BuildCommDCBCSSP32,SetCommStateCSSP32,ReadFileCSSP32,WriteFileCSSP32DECLARE FUNCTION GetLastError,GetLastErrorCSSP32BEGIN CASECASE MODE=OPEN" ;GOSUB OPEN_PORTCASE MODE=CLOSE" ;GOSUB CLOSE_PORTCASE MODE=READ" ;GOSUB READ_FROM_PORTCASE MODE=WRITE" ;GOSUB WRITE_TO_PORTEND CASERETURN
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 =COMMDATABAUD_RATE=COMMDATADATA_BITS=COMMDATASTOP_BITS=COMMDATAPARITY =COMMDATA1,1FLOW_CONTROL=COMMDATA1,1PORT_SETTINGS=PORT:":":BAUD_RATE:",":PARITY:",":DATA_BITS:",":STOP_BITS:\00\INQSIZE=1024OUTQSIZE=128IF BITSIZE=16 THENPORT=PORT:\00\COMM_PORT_HANDLE=OpenComm(PORT,INQSIZE,OUTQSIZE)END ELSEShareMode=1CreationDisposition=3COMM_PORT_HANDLE=CreateFileCSSP32(PORT,"",ShareMode,"",CreationDisposition,"","")ENDLOCATE COMM_PORT_HANDLE IN COMM_ERRORS USING @VM SETTING POS THENCOMM_PORT_HANDLE=COMM_ERRORSRETURNENDDCBCode=DCB"DCB_STRUCT=BLANK_STRUCT(DCBCode)IF BITSIZE=16 THENRETVAL=BuildCommDCB(PORT_SETTINGS,DCB_STRUCT)END ELSERETVAL=BuildCommDCBCSSP32(PORT_SETTINGS,DCB_STRUCT)ENDCOMM_PARAMS=STRUCT_TO_VAR(DCB_STRUCT,DCBCode)BEGIN CASECASE FLOW_CONTROL=X"COMM_PARAMS=3CASE FLOW_CONTROL=H"COMM_PARAMS=5COMM_PARAMS=9COMM_PARAMS=64END CASEDCB_STRUCT=VAR_TO_STRUCT(COMM_PARAMS,DCBCode)IF BITSIZE=16 THENRETVAL=SetCommState(DCB_STRUCT)END ELSERETVAL=SetCommStateCSSP32(DCB_STRUCT)ENDRETURN
CLOSE_PORT:
IF BITSIZE=16 THENRETVAL=CloseComm(COMM_PORT_HANDLE)END ELSERETVAL=CloseCommCSSP32(COMM_PORT_HANDLE)ENDRETURN
READ_FROM_PORT:
IF BITSIZE=16 THENBUFF_LEN=50COMMDATA=STR(\00\,BUFF_LEN)RETVAL=ReadComm(COMM_PORT_HANDLE,COMMDATA,BUFF_LEN)COMMDATA=FIELD(COMMDATA,\00\,1)END ELSEBytesToRead=1024BytesRead=0BUFF_LEN=50COMMDATA=50RETVAL=ReadFileCSSP32(COMM_PORT_HANDLE,COMMDATA,BytesToRead,BytesRead,"")ERROR="RETVAL2=GetLastErrorCSSP32(ERROR)ENDRETURN
WRITE_TO_PORT:
COMMDATA=COMMDATA:\00\BUFF_LEN=LEN(COMMDATA)IF BITSIZE=16 THENRETVAL=WriteComm(COMM_PORT_HANDLE,COMMDATA,BUFF_LEN)END ELSERETVAL=WriteFileCSSP32(COMM_PORT_HANDLE,COMMDATA,BUFF_LEN)ENDRETURN
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 StringDim sCommand As StringDim sArgs() As StringDim InBuff As StringDim sText As StringDim LastBuff As StringDim SoundFile As StringPrivate Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal Filename As String, ByVal Flags As Long) As LongPrivate Sub Form_Load()sCommand=Command & ",,,,,,,"If sCommand=" ThensCommand=Comm1,9600,N,8,1,C:\,"'sCommand=Comm2,9600,N,8,1,C:\,"End IfsArgs()=Split(sCommand, ",")sPath=sArgs(5)SoundFile=sArgs(6)Text.Text="LastBuff="If sPath=" Then sPath=C:\"If Dir(sPath & "digit.log") "" ThenKill sPath & "digit.log"End IfOpen sPath & "digit.log" For Append As #99WriteToLog "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=1End WithWriteToLog "Comm port opened"WriteToLog "RTS Enabled"WriteToLog "Comm port buffer cleared"WriteToLog "Ready to receive data..."InBuff="End SubPrivate Sub Form_Unload(Cancel As Integer)If Dir(sPath & "dinfo.txt") "" ThenKill sPath & "dinfo.txt"End IfMSComm1.PortOpen=FalseWriteToLog "Comm port closed"WriteToLog "End of session"Close #99End SubPrivate Sub MSComm1_OnComm()Dim InChar As StringDim Counter As IntegerSelect Case MSComm1.CommEventCase 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 ErrorCase 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 comEvSendCase comEvEOFCase comEvReceive ' Received RThreshold # of chars.InChar=MSComm1.InputIf InChar=Chr$(10) Then InChar="If InChar=Chr$(13) ThenOpen sPath & "digit.txt" For Append As #1Print #99, "Data received: " & InBuffPrint #1, InBuffClose #1sText=Text.TextText.Text=sText & InBuff & Chr(13) & Chr(10)LastBuff=InBuffInBuff="If SoundFile "" ThenCall sndPlaySound(SoundFile, 1)End IfElseInBuff=InBuff & InCharEnd IfEnd SelectEnd SubSub WriteToLog(sString As String)Print #99, Now & " - " & sStringsText=Text.TextText.Text=sText & sString & Chr(13) & Chr(10)End SubPrivate Sub Timer1_Timer()
If Dir(sPath & "dinfo.txt") "" Then Unload MeEnd Sub