Results 1 to 2 of 2

Thread: USB to Serial emulator

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Senior Member zak's Avatar
    Join Date
    Dec 2008
    Posts
    637
    Rep Power
    83

    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:
    serial.jpg

    the LED is OFF:
    led_off.jpg

    the LED is ON:
    led_on.jpg

    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

Similar Threads

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

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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