Author Topic: Serial Port Receiving Bytes and Strings. Where could find examples  (Read 4533 times)

0 Members and 1 Guest are viewing this topic.

Offline acjacques

  • Newbie
  • Posts: 33
    • View Profile
Where I could find simple examples or a good tutorial for UART serial port receiving strings and bytes ?
The included help in the program  does not help too much.
Thanks

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Serial Port Receiving Bytes and Strings. Where could find examples
« Reply #1 on: October 13, 2019, 10:54:54 am »
Programming isn't difficult, only it's  consuming time and coffee

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Serial Port Receiving Bytes and Strings. Where could find examples
« Reply #2 on: October 13, 2019, 12:13:45 pm »
you mean like opening a com port?
my reply to a topic on COM port comunicaiton
Original topic: https://www.qb64.org/forum/index.php?topic=1066.msg104282#msg104282
all you really need is OPEN, GET #, and PUT #

Quote
you need to use GET #, and PUT # when communing with the COM ports with QB64

little example from my robotics control, controlling a remote fired static mounted firearm.
in Simulation it, sends fire command PUT #1, and checks weapon status with GET #1 over and over checking for weapon jam, ammo depleted and weapon mounting secured.

Code: QB64: [Select]
  1.     OPEN "COM1:9600,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1
  2.     B1 = 197 'Command byte
  3.     B2 = 21 'Motor control byte \ Trigger solinoid
  4.     B12$ = CHR$(B1) + CHR$(B2)
  5.     PUT #1, , B12$
  6.     DO
  7.      PUT #1, , B12$
  8.      _DELAY .03
  9.      GET #1, , B3~%% 'did bolt return to fire pos?
  10.      GET #1, , B4~%% 'is the magazine at empty pos?
  11.      GET #1, , B5~%% 'is front and rear mount switch still down?
  12.      LOCATE 1, 1: PRINT B3~%%
  13.      PRINT B4~%%; B5~%%
  14.     LOOP UNTIL INKEY$ = CHR$(27)
  15.     CLOSE
  16.  
     


On a side note, I would move away from using GOSUB and RETURN
Granted after becoming radioactive I only have a half-life!

Offline acjacques

  • Newbie
  • Posts: 33
    • View Profile
Re: Serial Port Receiving Bytes and Strings. Where could find examples
« Reply #3 on: October 14, 2019, 12:21:43 pm »
Hi;

I have tested the code posted by Cobalt but I have error message #68 Line 1 (in main module)  Device unavailable.
A am using to test two USB TTL converters . One to be linked to program was changed to  COM1  in the W10 device manager and it is available and could be opened by a terminal program. It seems does not work also with other C OM ports numbers.

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Serial Port Receiving Bytes and Strings. Where could find examples
« Reply #4 on: October 14, 2019, 12:52:18 pm »
Code: QB64: [Select]
  1. 'works faster than strings: FT201X I2C to USB transfer
  2. '******Connect the USB cable first!!!!!!!!!!!!!!
  3. '******then press F5****************************
  4. WIDTH 120, 70
  5. ON ERROR GOTO handler
  6. k = 1
  7. addr = 0
  8. OPEN "c:\cw1\pressout4.txt" FOR OUTPUT AS #2
  9. OPEN "com5:115200,N,8,1,CS0,DS0" FOR RANDOM AS #1 LEN = 2 '115200 19200 9600
  10. 'the baud rate does not matter the FT201 is determining the speed
  11.     WHILE LOC(1) < 2: WEND
  12.     addr = addr + 1
  13.     GET #1, , x1
  14.     PRINT #2, USING "######"; x1;
  15.     PRINT USING "######"; x1;
  16.     IF addr MOD 16 = 0 THEN PRINT #2, " "
  17.     IF addr MOD 16 = 0 THEN PRINT addr
  18.     IF addr > 524287 THEN EXIT DO 'for 1MB
  19. handler:
  20. SLEEP (1)
I've been using this code (above) to transfer data using a "virtual COM port". As it says at the top you need to connect the cable first (to initialize the USB I presume) before any transfer can be attempted (wait 10 seconds to be safe).
My device uses a different COM ports you need to look up using "control panel" to see what number port it is using (in the example it was COM5). The program is transfering  unsigned 16bit integers you need to change that to the data you are expecting.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Serial Port Receiving Bytes and Strings. Where could find examples
« Reply #5 on: October 14, 2019, 06:04:34 pm »
Hi;

I have tested the code posted by Cobalt but I have error message #68 Line 1 (in main module)  Device unavailable.
A am using to test two USB TTL converters . One to be linked to program was changed to  COM1  in the W10 device manager and it is available and could be opened by a terminal program. It seems does not work also with other C OM ports numbers.

Could be an OS issue.
that code was written with 32bit QB64 on Windows Vista.
Granted after becoming radioactive I only have a half-life!

Offline acjacques

  • Newbie
  • Posts: 33
    • View Profile
Re: Serial Port Receiving Bytes and Strings. Where could find examples
« Reply #6 on: October 14, 2019, 11:38:30 pm »
Finally I could connect to COM1   running the COMM  CHECKING:
ON ERROR GOTO Handler
FF = FREEFILE
comPort$ = "COM1:"                         'try a COM port number that does not exist
CONST comMode$ = "9600,N,8,1,CS0,DS0"      'Use 0 to avoid timeouts
OPEN comPort$ + comMode$ FOR RANDOM AS FF
IF errnum = 0 THEN PRINT "COM exists!

K$ = INPUT$(1)
END

Handler:
errnum = ERR
PRINT "Error:"; errnum
RESUME NEXT

It does not works with other COM ports like COM14 , COM15.... I believe works only with lower numbers....


But I am far away to understand how get my sensor data into a  INT variable

The sensor  data is serial 9600 8N1 stream  with sentences like:

"1955mm"     (ASCII without the  quotes,  followed by LF + CR)

I need to get only the numbers in a variable 

I know that i need use the GET command  but i do not understand what syntax use..... and also about synchronizing the data. May be  waiting the LF+CR characters ...then GET the four characters...

When trying to send   data to port   with:

i=mynumber%
a$ = STR$(i) 'number to string
PUT #1, , a$ 'send string to serial port

the received characters are added with   STX (start of text) and EOT (end of transmission).... (ASCII 02 and 04)

Any help will be very appreciated


 




« Last Edit: October 14, 2019, 11:40:51 pm by acjacques »