PDA

View Full Version : USB to Serial emulator



zak
01-09-2011, 17:13
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... (http://en.wikipedia.org/wiki/Serial_port)
i have interested in this topic when Eros posted here ... (http://www.thinbasic.com/community/showthread.php?11324-New-forum-option-Similar-Threads) a reference to PB Source code foru (http://www.powerbasic.com/support/pbforums/forumdisplay.php?f=12)m 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:
7443

the LED is OFF:
7444

the LED is ON:
7445

many of the thinbasic code are copied from here. (http://www.thinbasic.com/community/showthread.php?11085-can-t-opening-comm/page2).

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)










(http://www.powerbasic.com/support/pbforums/forumdisplay.php?f=12)

kryton9
01-09-2011, 23:02
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.