This is a short program that I'd like to use with a project I'm currently working on.
My program should send any message (i.e. "Somebody?") to my microcontroller and if correctly received, the microcontroller sends "Yes!" back to the PC.
The hardware works - I can test it with a serial terminal.
Unfortunately, my program here under, trimmed of all extra code that is not helpful fo now, doesn't work as well.
What I'm trying to achieve is a detection of possible available com ports (I use FTDI RS232 USB cables) and when one is found (here I check ports 1 to 8), the program sends "Somebody?" to the microcontroler. If the microcontroller is really there, it'll answer "Yes!" so I will know I've selected the correct com port.
But, as said, it doesn't work - I never make it to read the incoming data (from the µC to the PC); my DataRead$ var always shows empty.
What am I missing please?
Comport = 1
[TIMED_LOOP] TIMER 500 , [CHECK_COMPORT] WAIT
[CHECK_COMPORT] OPEN "COM";Comport;":9600,n,8,1" FOR random AS #COM 'if an error occurs here, the actual com port is not accessible, goto to error handler (SKIP_THIS_PORT) PRINT "Testing COM Port: "; Comport 'tell me which port is currently in use just for info #COM "Somebody?" 'send the query to the µC DataRead$ = INPUT$(#COM, 4) 'receive µC's response PRINT "DataRead: "; DataRead$ 'show the received message
[SKIP_THIS_PORT] Comport = Comport + 1 'try next com port IF Comport = 8 THEN PRINT "END" : END 'end the program after port 8... GOTO [TIMED_LOOP]
...I found my answer: I wasn't patient enough while waiting for the µC's response.
Comport = 0
[TIMED_LOOP] Comport = Comport + 1 DataRead$ = "" IF Comport = 8 THEN PRINT "END" : END OPEN "COM";Comport;":9600,n,8,1" FOR random AS #COM PRINT "Port successfully opened: "; Comport #COM "Somebody?" ' send message to module
Counter = 0 TIMER 500, [READ_DATA] [READ_DATA] DataRead$ = INPUT$(#COM, 4) ' awaiting the message "Yes!" IF DataRead$ = "Yes!" THEN TIMER 0 PRINT "DataRead: "; DataRead$ CLOSE #COM PRINT "END" : END END IF
Counter = Counter + 1 IF Counter = 3 THEN CLOSE #COM GOTO [TIMED_LOOP] END IF WAIT