Virtual Com Port
Virtual Com Port
I am experiencing a problem that makes no sense. I am attempting to communicate with an external instrument module via USB interface (a Virtual Com Port), and I am receiving an error code #3 (File I/O Error). Given the code I am running (see below), the error suggests the file handle is somehow being corrupted, please advise…
ComPort = FREEFILE
Open ("COM5:19200,N,8,1") AS #ComPort
IF Err > 0 THEN
PRINT "found a com err #", Err;
ELSE
A_str = "The Love I Lost"
A_num = 1
Do
A_char = Mid(A_str, A_num, 1)
PRINT #ComPort, A_char;
Sleep 10
A_num = A_num + 1
Loop Until A_num = Len(A_str) + 1
END IF
PRINT Err
ComPort = FREEFILE
Open ("COM5:19200,N,8,1") AS #ComPort
IF Err > 0 THEN
PRINT "found a com err #", Err;
ELSE
A_str = "The Love I Lost"
A_num = 1
Do
A_char = Mid(A_str, A_num, 1)
PRINT #ComPort, A_char;
Sleep 10
A_num = A_num + 1
Loop Until A_num = Len(A_str) + 1
END IF
PRINT Err
Re: Virtual Com Port
Hi, and welcome to FB.
Before diving into code, can you be a bit more specific, about circumstances:
e.g.
Before diving into code, can you be a bit more specific, about circumstances:
e.g.
- Operating System (DOS/WIN/LIN etc.)?
bitness of: OS (above)?
bitness of: Compiler (FBC), you are currently using?
Is the VCP (without any problems) seen, in the OS configuration?
Is the Device connected, prior to starting the code?
Re: Virtual Com Port
Great questions, each one. I am running W10 32bit, and tried W7, 32bit. I am running FB compiler version 1.05.0. The end device is a instrument module with a Silabs VCP chip. I can use TeraTerm (terminal emulator), with great success. I ran a USB sniffer, (with TeraTerm) and had great success. I configured the end device for loopback, and achieved great success with TeraTerm. But when I run the FB program - no such luck...
Re: Virtual Com Port
OK, went over the code, you've posted ...
What was clearly missing: used variables, must be declared first (similar to below):
What was clearly missing: used variables, must be declared first (similar to below):
Code: Select all
Dim As Long ComPort = FreeFile, A_num = 1
Dim As String A_str = "The Love I Lost", A_char = ""
Open ("COM5:19200,N,8,1") AS #ComPort
IF Err > 0 Then
Print "found a com err #"; Err;
Beep : Sleep : End 1
End If
Do
A_char = Mid(A_str, A_num, 1)
Print #ComPort, A_char;
Sleep 10
A_num += 1
Loop Until A_num = Len(A_str) + 1
Print "press a key to EXIT prog. ";
Sleep
End 0
Last edited by MrSwiss on Dec 07, 2017 19:28, edited 1 time in total.
Re: Virtual Com Port
Yes, not shown, didn't think I needed to present that level of detail, I wish it were that simple...
Re: Virtual Com Port
Instead of:
try:
Code: Select all
Open ("COM5:19200,N,8,1") AS #ComPort
Code: Select all
Open Com("COM5: 19200, N, 8, 1", As #ComPort)
Re: Virtual Com Port
No Go, I got the following error - MAIN.BAS(233) error 7: Expected ')', found ',' in 'Open Com ("COM5:,19200,N,8,1", As #ComPort)'. But when I tried the following configuration - Open Com "COM5:19200,N,8,1" As #ComPort, the program at least compiled but there was a strange oddity. The text color of 'Com' did not change (like Open) to indicate it was a real statement. Needless to say, I am still having no success...
Re: Virtual Com Port
@MrSwiss, when you use 'Open Com' in the IDE, does it look odd, meaning does the text change appropriately...
Re: Virtual Com Port
@MrSwiss, I ran the following configuration Open Com "COM5:19200,N,8,1" As #ComPort, and performed a 'Print Err' right after and receive an error #1 code - Illegal function call. I deduce the 'Open Com' function is somehow not working, can you verify and let me know your results. Thanks...
Re: Virtual Com Port
Simply OPEN [parameters] is for File I/O ...FB-Manual wrote:Opens a serial port for input and output
Syntax
Declare Function Open Com ( byref options As String, As filenum As Long ) As Long
Usage
result = Open Com( options[,] As[#] filenum )
OPEN COM [parameters] is for Serial I/O ...
Yes, it does look "funny" (IDE coloring).
The following code produces a ERR 2 (but, I've nothing connected to the port!),
also using COM3 (which is my VCP):
Code: Select all
Dim As Long ComPort = FreeFile, A_num = 1, res
Dim As String A_str = "The Love I Lost", A_char = ""
res = Open Com("COM3: 19200, N, 8, 1", As #ComPort)
If res > 0 Then
Print "found a com err #"; Str(res)
Beep : Sleep : End 1
End If
Do
A_char = Mid(A_str, A_num, 1)
Print #ComPort, A_char;
Sleep 10
A_num += 1
Loop Until A_num = Len(A_str) + 1
End 0
Re: Virtual Com Port
@MrSwiss, thanks for assistance and persistence, I think I may have discovered a 'bug'. How does one go about reporting such things...
-
- Posts: 3
- Joined: May 19, 2015 4:04
Re: Virtual Com Port
This is an example how to communicate with an Arduino using a USB port. Maybe it will help...
Code: Select all
DIM AS LONG com_port_file_handle
DIM AS STRING * 1 read_char
DIM AS SINGLE StartTime = 0
DIM AS SINGLE TimeOut
DIM AS INTEGER counter
DIM AS INTEGER t
PRINT "Attempting to open a port to the Arduino"
com_port_file_handle = freefile
OPEN COM "COM4:115200,N,8,1,BIN,CS0,DS0" FOR RANDOM AS 1 'change the baud rate as required
'Wait for Arduino to wake up and send "RESET" - this needs to be made into a sub or function
'--------------------------------------------------------------------------------------------
StartTime = TIMER
TimeOut = 4
DO
GET #com_port_file_handle,,read_char
' IF read_char=CHR$(13) THEN EXIT DO 'Arduino sends chr$(13) then chr$(10)
IF read_char=CHR$(10) THEN EXIT DO
IF read_char<>"" THEN PRINT read_char;
IF TIMER-StartTime>TimeOut THEN EXIT DO
LOOP
PRINT
'--------------------------------------------------------------------------------------------
'Send a command to the Arduino
'-----------------------------
PRINT "Sending command 00"
PUT #com_port_file_handle,,"00" 'PUT does not append chr$(13) or chr$(10); PRINT does
'Wait for reply with timeout; Arduino should reply with "DONE"
'-------------------------------------------------------------
StartTime = TIMER
TimeOut = 4 'seconds
DO
GET #com_port_file_handle,,read_char
' IF read_char=CHR$(13) THEN EXIT DO
IF read_char=CHR$(10) THEN EXIT DO
IF read_char<>"" THEN PRINT read_char;
IF TIMER-StartTime>TimeOut THEN EXIT DO
LOOP
PRINT
PRINT "And We Be Done - Any key to exit..."
SLEEP
CLOSE #com_port_file_handle
END
Re: Virtual Com Port
@OvertonSr,
well, before reporting 'anything' we should first, make certain, that it really is: 'a bug'!
Then:
FB's Bug-Tracker, is on: "sf.net" (aka: SourceForge).
well, before reporting 'anything' we should first, make certain, that it really is: 'a bug'!
Then:
FB's Bug-Tracker, is on: "sf.net" (aka: SourceForge).
Re: Virtual Com Port
@MrSwiss, I am at a loss for pursuing the next troubleshooting step - any ideas...
Re: Virtual Com Port
@OvertonSr,
the device in question, might require additional (optional) parameters, in order to
work correctly. You'll have to consult the device manual, to figure out such details.
the device in question, might require additional (optional) parameters, in order to
work correctly. You'll have to consult the device manual, to figure out such details.