Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Netcomm

Status
Not open for further replies.

AdamVerellen

IS-IT--Management
Sep 20, 2002
39
CA
Anyone use the netcomm control from If so, could someone help me a little.
I'm reading data from a SMDR, phone system gadget that sends a data string each time a call is placed and ended.
I got my form to display the text from the unit ok, and ouput that info to a text file kinda sorta ok, except that after the first line that is writen the info isn't kept on one line.

Dim Buffer As String

AxNETComm1.CommPort = 2
AxNETComm1.PortOpen = True
AxNETComm1.RThreshold = 1

Buffer = AxNETComm1.InputData
System.Diagnostics.Debug.WriteLine(Buffer)
FileOpen(1, "c:\TextFile1.txt", OpenMode.Append,_ OpenAccess.Write)
WriteLine(1, Buffer)
FileClose(1)
TextBox1.SelectedText = Buffer

I think I'm almost there, maybe missing a loop?
the text file looks like this

-----this first line comes out ok (appears as one line for me)
"N 006 00 DN3620 T023000 01/03 07:48 00:00:04 6417646655
"
------after that the line is broken up kinda like this (in this example there are suppose to be 2 lines)
"N 007 00"
" DN3620 "
" T023000"
" "
" 01/"
"03 07:48"
" 00:00:0"
"2 565666"
"56565565"
"6
"
"N 008 00"
" DN3620 "
" T023000"
" "
" 01/"
"03 07:48"
" 00:00:0"
"2 656666"
"566565
"

Any help would be greatly appreciated

Thanks
Adam
 
I am assuming you have above beginner level experience.

Well I will attempt to help you - I have not actually used NETComm yet but it has basically the same events etc as the MSComm ocx. I tweaked my VB.NEY code (which uses MSComm) and added NETComm type args (I hope I got it all right). You may have to make some adjustments. This is a little more complex but the flexibility and power of this function for reading serial IO will become apparent and is really a must for serial comm IMHO.

Put all this code EXCEPT what is in between the +++++++
chars which is a sample button type click code snippet based on your original.

The function code is well commented - read it. Have fun and good luck.

Regards,
Terry





'==============================================================================
'==============================================================================
'Global Read_RS232_Port flag
Public glCRFlag As Integer

'A Better DoEvents
'Putting DoEvents in loops to make your app responsive to user input is a
'common but expensive practice. Use GetInputState if possible. GetInputState
'returns 1 when a mouse is clicked or key pressed. It has much less overhead
'and can be called every so often as need be. When an input event occurs,
'then call DoEvents.
Public Declare Function GetInputState Lib "User32" () As Integer

'This API can be used to delay x number of milliseconds
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)

'This API returns the number of milliseconds Windows has been
'running up to 49 days after which it rolls to 0
Public Declare Function timeGetTime Lib "winmm.dll" () As Integer



'===========================================================
'===========================================================
'Read string of chars from RS232 Port.
'WARNING!! This function can not be re-entered while executing
'Arguments:
'iCommNum = The com port 1 to 16
'MSComm1 = The com port control
'sComSettings = The com port settings such as "4800,N,8,1"
'lMaxChars = Max number of chars to read
'sTermChar = The terminating character - typically CR Chr(13) or LF Chr(10)
'lNumTermChars = The number of terminating chars to read
'lMaxWait = The max wait time in milliseconds ie 5000 msec is 5 secs
'iCharMax = The max character code allowed (ASCII systems) usually 127 or 255
'lErrCode = The error code
'sRetstr = The string returned
'boClose = If True Close port after read if False DO NOT Close unless
' user abort.
' NOTE: If True the Inputbuffer is cleared before reading from port.
' NOTE: If reading continuous strings such as NMEA strings boClose
' should be set to False so Inputbuffer is NOT cleared when the
' function is called - If you fail to do this on NMEA type data
' continuous strings you will lose incoming chars on the port.
'Function returns the number of characters read from port
'NOTE!! If the port is closed when this function is called, it will
'be opened automatically

'NOTE: global flag glCRFlag contains a status value that is accessable to all
'events. If this value is >= 0, it is a SUCCESS condition, if less than 0 then
'it is a FAILURE condition.
'If glCRFlag = 1 then SUCCESS_MAX_COUNT
'If glCRFlag = 2 then SUCCESS_TERM_CHAR
'
Public Function Read_RS232_Port(ByVal iCommNum As Short, ByRef NETComm1 As AxNETCommLib.AxNETComm, _
ByVal sComSettings As String, ByVal lMaxChars As Integer, ByVal sTermChar As String, _
ByVal lNumTermChars As Integer, ByVal lMaxwait As Integer, ByRef iCharMax As Short, _
ByRef lErrCode As Integer, ByRef sRetstr As String, ByVal boClose As Boolean) As Integer

Static xChar As String
Dim lRet As Integer
Dim lTermCharCount As Integer
Dim lCurlen As Integer
Dim lCkTimeout As Integer
Dim lCurtime As Integer
Dim lEndtime As Integer

'Warning! You must set the CommPort property before
'opening the port.

On Error GoTo ERRlblx

NETComm1.Settings = sComSettings 'Default is "4800,N,8,1"
If NETComm1.PortOpen = False Then 'Port Not open
NETComm1.CommPort = iCommNum 'Com Port number - integer
NETComm1.PortOpen = True 'Open the port
End If

'YES generate the OnComm event on every char read
NETComm1.RThreshold = 1
'Read one char at a time
NETComm1.InputLen = 1
If boClose = True Then NETComm1.InBufferCount = 0 'Clear input buffer
NETComm1.InputMode = NETCommLib.InputModeConstants.comInputModeText

sRetstr = "" 'Clear char buffer
xChar = "" 'The char that will be appended
lErrCode = 0 'Set error code to 0 if necessary
glCRFlag = -3 'Set default global return flag to TIME_OUT error
lCurtime = timeGetTime 'Get current clock count
lEndtime = lCurtime + lMaxwait 'lMaxWait is in Milliseconds ie 5000 msec=5 seconds

Do Until timeGetTime >= lEndtime
'DO NOT use GetInputState() here, this function is already called in
'the timeGetTime API function, if you use GetInputState() here the
'function becomes less responsive - use DoEvents
System.Windows.Forms.Application.DoEvents()

'Check for manual termination using global flag
If glCRFlag = -22 Then Exit Do 'Close port if user abort

If NETComm1.CommEvent = NETCommLib.OnCommConstants.comEvReceive Then
xChar = CStr(NETComm1.InputData) 'Note: For NETCommOCX this will be .InputData
'The OnComm event fires sometimes without any real data read
'You MUST test for this condition and retry if necessary!!
If Len(xChar) = 0 Then GoTo Lbl01
sRetstr = sRetstr & xChar
If Asc(xChar) > iCharMax Then
glCRFlag = -2001 'Max Char code/NON ASCII chars encountered - Com settings wrong??
Exit Do
End If

lCurlen = lCurlen + 1 'The current char count
If lCurlen = lMaxChars Then 'Total max char count obtained
glCRFlag = 1 'Set default global return flag to SUCCESS_MAX_COUNT
Exit Do
End If
If xChar = sTermChar Then 'Test for Terminating char
lTermCharCount = lTermCharCount + 1
If lTermCharCount = lNumTermChars Then
glCRFlag = 2 'Set default global return flag to SUCCESS_TERM_CHAR
Exit Do
End If
End If
End If

If NETComm1.CommEvent > 1000 Then 'Handle Comm event errors
'ERROR Value Description
'comEventBreak 1001 A Break signal was received.
'comEventFrame 1004 Framing Error. The hardware detected a framing error.
'comEventOverrun 1006 Port Overrun. A character was not read from the hardware before the next character arrived and was lost.
'comEventRxOver 1008 Receive Buffer Overflow. There is no room in the receive buffer.
'comEventRxParity 1009 Parity Error. The hardware detected a parity error.
'comEventTxFull 1010 Transmit Buffer Full. The transmit buffer was full while trying to queue a character.
'comEventDCB 1011 Unexpected error retrieving Device Control Block (DCB) for the port.
glCRFlag = 0 - NETComm1.CommEvent 'Make error code negative
Exit Do
End If
Lbl01:
Loop

If glCRFlag < 0 Then 'If True then error on Comm
lErrCode = glCRFlag 'Negative return value=FAILURE
'Calling function should call the error dialog
'Display_Comm_err NETComm1, glCRFlag, sRetstr
Read_RS232_Port = Len(sRetstr) 'Return length of string read so far anyway
Else 'Positive return value=SUCCESS
lErrCode = glCRFlag
'Buffered string returned in sRetstr
Read_RS232_Port = Len(sRetstr) 'Return length of string read
End If

If boClose = True Or glCRFlag = -22 Then
NETComm1.PortOpen = False 'Close the port after read or user abort
End If

Exit Function

ERRlblx:
MsgBox(&quot;COMM Port &quot; & iCommNum & &quot; Failed to Open&quot; & vbCrLf & &quot;Make sure port is available!&quot;, MsgBoxStyle.Exclamation + MsgBoxStyle.OKOnly, &quot;DC_Comm&quot;)
End Function



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dim iCommPort As Short
Dim lErr As Integer
Dim lNumchars As Integer
Dim sComSettings As String
Dim boClose As Boolean
Dim Buffer As String


iCommPort = 2
sComSettings = &quot;4800,N,8,1&quot;
boClose= True

'This function will read up to 200 chars with an ASCII value up to 127
'However it will return if
'1) A single CR char (10) is reached
'2) 20 seconds (20000 milliseconds) has elapsed
'
'The Comm port will be closed on exit

lNumchars = Read_RS232_Port(iCommPort, AxNETComm1, sComSettings, 200, Chr(10), _
1, 20000, 127, lErr, Buffer, boClose)

System.Diagnostics.Debug.WriteLine(Buffer)
FileOpen(1, &quot;c:\TextFile1.txt&quot;, OpenMode.Append,OpenAccess.Write)
WriteLine(1, Buffer)
FileClose(1)
TextBox1.SelectedText = Buffer

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
I'm not much higher then a beginner. Well new to VB anyway.
I'm able to figure out what the code means easy enough with a little digging around.

Anyway, I tried adding the code.... having some issues.
With NETCommLib part of the code.
I wish there was more documentation without having to buy this book, I'm sure that the book would be usefull for sure.

If you got mscomm working that would problably do just fine. I was under the impression that it wouldn't work properly under .NET

Will keep trying....

Thanks
Adam
 
OK, here is the MSComm version - make sure you have MSCOMM32.OCX and create all the wrapper stuff for VB.NET it should work fine.

Regards,
Terry

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dim iCommPort As Short
Dim lErr As Integer
Dim lNumchars As Integer
Dim sComSettings As String
Dim boClose As Boolean
Dim Buffer As String

iCommPort = 2
sComSettings = &quot;4800,N,8,1&quot;
boClose= True

'This function will read up to 200 chars with an ASCII value up to 127
'However it will return if
'1) A single CR char (10) is reached
'2) 20 seconds (20000 milliseconds) has elapsed
'
'The Comm port will be closed on exit

lNumchars = Read_RS232_Port(iCommPort, MSComm1, sComSettings, 200, Chr(10), _
1, 20000, 127, lErr, Buffer, boClose)

System.Diagnostics.Debug.WriteLine(Buffer)
FileOpen(1, &quot;c:\TextFile1.txt&quot;, OpenMode.Append,OpenAccess.Write)
WriteLine(1, Buffer)
FileClose(1)
TextBox1.SelectedText = Buffer

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++





'======================================================
Public Function Read_RS232_Port(ByVal iCommNum As Short, ByRef MSComm1 As AxMSCommLib.AxMSComm, ByVal sComSettings As String, ByVal lMaxChars As Integer, ByVal sTermChar As String, ByVal lNumTermChars As Integer, ByVal lMaxwait As Integer, ByRef iCharMax As Short, ByRef lErrCode As Integer, ByRef sRetstr As String, ByVal boClose As Boolean) As Integer

Static xChar As String
Dim lRet As Integer
Dim lTermCharCount As Integer
Dim lCurlen As Integer
Dim lCkTimeout As Integer
Dim lCurtime As Integer
Dim lEndtime As Integer

'Warning! You must set the CommPort property before
'opening the port.

On Error GoTo ERRlblx

MSComm1.Settings = sComSettings 'Default is &quot;4800,N,8,1&quot;
If MSComm1.PortOpen = False Then 'Port Not open
MSComm1.CommPort = iCommNum 'Com Port number - integer
MSComm1.PortOpen = True 'Open the port
End If

'YES generate the OnComm event on every char read
MSComm1.RThreshold = 1
'Read one char at a time
MSComm1.InputLen = 1
If boClose = True Then MSComm1.InBufferCount = 0 'Clear input buffer
MSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeText

sRetstr = &quot;&quot; 'Clear char buffer
xChar = &quot;&quot; 'The char that will be appended
lErrCode = 0 'Set error code to 0 if necessary
glCRFlag = -3 'Set default global return flag to TIME_OUT error
lCurtime = timeGetTime 'Get current clock count
lEndtime = lCurtime + lMaxwait 'lMaxWait is in Milliseconds ie 5000 msec=5 seconds

Do Until timeGetTime >= lEndtime
'DO NOT use GetInputState() here, this function is already called in
'the timeGetTime API function, if you use GetInputState() here the
'function becomes less responsive - use DoEvents
System.Windows.Forms.Application.DoEvents()

'Check for manual termination using global flag
If glCRFlag = -22 Then Exit Do 'Close port if user abort

If MSComm1.CommEvent = MSCommLib.OnCommConstants.comEvReceive Then
xChar = CStr(MSComm1.Input)
'The OnComm event fires sometimes without any real data read
'You MUST test for this condition and retry if necessary!!
If Len(xChar) = 0 Then GoTo Lbl01
sRetstr = sRetstr & xChar
If Asc(xChar) > iCharMax Then
glCRFlag = -2001 'Max Char code/NON ASCII chars encountered - Com settings wrong??
Exit Do
End If

lCurlen = lCurlen + 1 'The current char count
If lCurlen = lMaxChars Then 'Total max char count obtained
glCRFlag = 1 'Set default global return flag to SUCCESS_MAX_COUNT
Exit Do
End If
If xChar = sTermChar Then 'Test for Terminating char
lTermCharCount = lTermCharCount + 1
If lTermCharCount = lNumTermChars Then
glCRFlag = 2 'Set default global return flag to SUCCESS_TERM_CHAR
Exit Do
End If
End If
End If

If MSComm1.CommEvent > 1000 Then 'Handle Comm event errors
'ERROR Value Description
'comEventBreak 1001 A Break signal was received.
'comEventFrame 1004 Framing Error. The hardware detected a framing error.
'comEventOverrun 1006 Port Overrun. A character was not read from the hardware before the next character arrived and was lost.
'comEventRxOver 1008 Receive Buffer Overflow. There is no room in the receive buffer.
'comEventRxParity 1009 Parity Error. The hardware detected a parity error.
'comEventTxFull 1010 Transmit Buffer Full. The transmit buffer was full while trying to queue a character.
'comEventDCB 1011 Unexpected error retrieving Device Control Block (DCB) for the port.
glCRFlag = 0 - MSComm1.CommEvent 'Make error code negative
Exit Do
End If
Lbl01:
Loop

If glCRFlag < 0 Then 'If True then error on Comm
lErrCode = glCRFlag 'Negative return value=FAILURE
'Calling function should call the error dialog
'Display_Comm_err MSComm1, glCRFlag, sRetstr
Read_RS232_Port = Len(sRetstr) 'Return length of string read so far anyway
Else 'Positive return value=SUCCESS
lErrCode = glCRFlag
'Buffered string returned in sRetstr
Read_RS232_Port = Len(sRetstr) 'Return length of string read
End If

If boClose = True Or glCRFlag = -22 Then
MSComm1.PortOpen = False 'Close the port after read or user abort
End If

Exit Function

ERRlblx:
MsgBox(&quot;COMM Port &quot; & iCommNum & &quot; Failed to Open&quot; & vbCrLf & &quot;Make sure port is available!&quot;, MsgBoxStyle.Exclamation + MsgBoxStyle.OKOnly, &quot;DC_Comm&quot;)
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top