Donate Now Goal amount for this month: 100 USD, Received: 0 USD (0%)
Donate to support this site and ThinBASIC project development

Results 1 to 2 of 2

Thread: USB to Serial emulator

  1. #1

    USB to Serial emulator

    we can't find the serial port in the most nowadays computers, indeed i thought there is one at the back of my desktop pc, but disappointed when checking it, it is uasualy 9 nine male pins connector look wikipedia...
    i have interested in this topic when Eros posted here ... a reference to PB Source code forum there are many electronics projects using the serial port by Stephane Fonteyne , but it is useless without serial port on the computer, lucky there is a cheap (3 dollar) special cable called USB to SERIAL which have a processor inside to emulate the serial port using the usb, we must also install a driver for the device to work, also we must write the properties of the device in the system properties - hardware - device manager - ports and to reflect this in the code. this is in windows xp.the thinbasic module comm are working okay with this emulator device.
    also if you want to use port.dll as in powerbasic examples download it from here:
    http://www.b-kainka.de/download.htm
    look for Update der Port.DLL für Windows XP (port.zip, 27 KB)
    the author of the book "pc interfaces under windows" , i have downloaded the first 64 complete pages from google books. the google books pages are stored inside temporary internet files using ie, with names begins like book43.png or book53.jpg. use xsearch to copy those files.
    attached the thinbasic code using comm module and a code using port.dll.
    the project attach LED directly to serial port between DTR and GND with the positive lead of the led to DTR and negative to GND , when i press a key the led will light on. pressing b will light of, x will exit.
    i want to add that if we want to trigger relays to be able to trigger more powerfull and real machines like motors then we must use intermediate simple circuits wich are available in powerbasic link above and in many electronics sites. also in the book "pc interfaces under windows"
    the pictures below is from a real experiment i have made myself today.
    9 way D-type port connectors solder side view:
    Click image for larger version. 

Name:	serial.jpg 
Views:	177 
Size:	115.5 KB 
ID:	7443

    the LED is OFF:
    Click image for larger version. 

Name:	led_off.jpg 
Views:	224 
Size:	31.2 KB 
ID:	7444

    the LED is ON:
    Click image for larger version. 

Name:	led_on.jpg 
Views:	209 
Size:	41.3 KB 
ID:	7445

    many of the thinbasic code are copied from here..
    Uses "COMM"
    Uses "CONSOLE"
    Dim hcomm As Long
    Dim sBuffer   As String
    Dim nBytes    As Long
    Dim errnumbr  As Long
     
    '---Get Comms file number and assign 
      hcomm = COMM_FreeFile
     
    '---Open the port
      COMM_Open( "COM3", hcomm )
      If Err <> 0 Then
        PrintL "Open Comms error: " + Err
      Else
    '---Set up all comms parameters 
        COMM_Set( hcomm, %COMM_BAUD, 9600 )
        COMM_Set( hcomm, %COMM_BYTE, 8 )
        COMM_Set( hcomm, %COMM_NULL, FALSE )
        COMM_Set( hcomm, %COMM_PARITY, FALSE )
        COMM_Set( hcomm, %COMM_PARITYTYPE, 0 )
        COMM_Set( hcomm, %COMM_STOP, 0 )
        COMM_Set( hcomm, %COMM_RXBUFFER, 64 )
       
    COMM_Set(hComm, %COMM_DTRLINE, 0)
    PrintL "press a to turn on the led light"
    PrintL "press b to turn off the led light" 
    Dim keyPressed As String
    While keypressed <> "x"
    keyPressed = Console_WaitKey
    keyPressed = Mid$(keyPressed,2,1)
    If keyPressed = "a" Then
    PrintL "pressed a key"
    COMM_Set(hComm, %COMM_DTRLINE, 1)
    ElseIf keyPressed = "b" Then
    PrintL "pressed b key"
    COMM_Set(hComm, %COMM_DTRLINE, 0)
    End If
    Wend
    PrintL "pressed x key" 
    COMM_Set(hComm, %COMM_DTRLINE, 0)
    COMM_Close( hcomm )
    Console_WriteLine("end testing")
    WaitKey(2) 
    End If
    

    the code using port.dll : all of the DLL declaratione are from Stephane Fonteyne
    Declare Function Open_SerialPort  Lib "port.dll" Alias "OPENCOM"(ByRef szSetup As Asciiz) As Long
    Declare Sub      Close_SerialPort Lib "port.dll" Alias "CLOSECOM"()
    Declare Function dsr_in           Lib "port.dll" Alias "DSR"() As Word
    Declare Function cts_in           Lib "port.dll" Alias "CTS"() As Word
    Declare Function dcd_in           Lib "port.dll" Alias "DCD"() As Word
    Declare Function ri_in            Lib "port.dll" Alias "RI"()  As Word
    Declare Sub      rts_out          Lib "port.dll" Alias "RTS"(ByVal iStatus As Word)
    Declare Sub      txd_out          Lib "port.dll" Alias "TXD"(ByVal iStatus As Word)
    Declare Sub      dtr_out          Lib "port.dll" Alias "DTR"(ByVal iStatus As Word)
    
    
    Declare Function Real_Time        Lib "port.dll" Alias "REALTIME"(ByVal iPriority As Word) As Long
    Declare Sub      Delay            Lib "port.dll" Alias "DELAY"(ByVal iDelay As Word)
    Declare Sub      Delay_us         Lib "port.dll" Alias "DELAYUS"(ByVal dwDelay As DWord)
    Declare Sub      Time_Init        Lib "port.dll" Alias "TIMEINIT"()
    Declare Sub      TimeInit_us      Lib "port.dll" Alias "TIMEINITUS"()
    Declare Function Timer_Read       Lib "port.dll" Alias "TIMEREAD"() As DWord
    Declare Function Timer_Read_us    Lib "port.dll" Alias "TIMEREADUS"() As DWord
    
    
    Uses "CONSOLE"
    
    
    Open_SerialPort("COM3:9600,N,8,1")
    DTR_out(0)
    PrintL "press a to turn on the led light"
    PrintL "press b to turn off the led light" 
    Dim keyPressed As String
    While keypressed <> "x"
    keyPressed = Console_WaitKey
    keyPressed = Mid$(keyPressed,2,1)
    If keyPressed = "a" Then
    PrintL "pressed a key"
    DTR_out(1)
    ElseIf keyPressed = "b" Then
    PrintL "pressed b key"
    DTR_out(0)
    End If
    Wend
    PrintL "pressed x key" 
    DTR_out(0)
    Close_SerialPort()
    Console_WriteLine("end testing")
    WaitKey(2)
    






    Attached Files Attached Files

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Clearwater, Florida, USA
    Posts
    3,802
    Rep Power
    388
    That is a really nice post, very informative, thanks Zak. I am still on the drive to the port, while you have embarked on your journey into the wild oceans away from the land of the pc box. Have a fun and keep on sharing your journey logs.

Similar Threads

  1. Activating Problem , cant find serial number
    By Petr Schreiber in forum Installation
    Replies: 4
    Last Post: 17-06-2006, 12:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •