Legacy Forum: Preserving Nearly 20 Years of Community History - A Time Capsule of Discussions, Memories, and Shared Experiences.

English translation of PC -> RCB1 protocol

KHR-1, KHR-2HV, KHR-3HV, ICS servos, RCB controllers and other Kondo products
14 postsPage 1 of 1
14 postsPage 1 of 1

English translation of PC -> RCB1 protocol

Post by limor » Sun Mar 06, 2005 12:25 pm

Post by limor
Sun Mar 06, 2005 12:25 pm

:!: Finally you can write your own software for controlling KHR-1 :!:

This protocol is the same one used by HeartToHeat.

The document, originally in Japanese, was translated by Dan Albertof the South Jersey Robotics Group (SJRobotics.org).

http://www.robosavvy.com/Support/images/RCB1stdraft.doc
:!: Finally you can write your own software for controlling KHR-1 :!:

This protocol is the same one used by HeartToHeat.

The document, originally in Japanese, was translated by Dan Albertof the South Jersey Robotics Group (SJRobotics.org).

http://www.robosavvy.com/Support/images/RCB1stdraft.doc
limor
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 1845
Joined: Mon Oct 11, 2004 1:00 am
Location: London, UK

Post by Meltdown » Sun Mar 06, 2005 6:50 pm

Post by Meltdown
Sun Mar 06, 2005 6:50 pm

So this can be used for lets say Visual Basic?
So this can be used for lets say Visual Basic?
Meltdown
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 133
Joined: Fri Nov 26, 2004 1:00 am
Location: Holland

Post by limor » Sun Mar 06, 2005 10:36 pm

Post by limor
Sun Mar 06, 2005 10:36 pm

Meltdown wrote:So this can be used for lets say Visual Basic?


Yes. The protocol describes the messages sent and received by the PC over the serial port. Dan Albert sent me a program that he wrote. He has connected a Serial-to-WiFi board to his KHR-1. More info will be uploaded to the forum over the next couple of days.
Meltdown wrote:So this can be used for lets say Visual Basic?


Yes. The protocol describes the messages sent and received by the PC over the serial port. Dan Albert sent me a program that he wrote. He has connected a Serial-to-WiFi board to his KHR-1. More info will be uploaded to the forum over the next couple of days.
limor
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 1845
Joined: Mon Oct 11, 2004 1:00 am
Location: London, UK

Post by limor » Tue Mar 08, 2005 8:38 pm

Post by limor
Tue Mar 08, 2005 8:38 pm

I asked Dan Albert the following:
    how’s the wifi-serial connection working out ?
    which one did you use ?
    Any software you’d like to share ?

Dan asked me to post his reply on the forum.


Here are some source code project files :

I have the ATOP ABLELink GW21SW-MAXI.
Image
It seems to work very well. I have written an embedded C++ app for my Axim that can send some basic commands to the KHR-1. I can put it into sleep mode, wake it up, and have it play 1 of the 40 stored motions.
The GW21SW-MAXI draws a bit to much current. At 450 mAmps that will drain the original pack fairly rapidly.
I've switched to using 1200mAh NiMH packs I got from www.onlybatteries.com They are much better than
the 600mAh Nicad pack that came with the KHR-1.
The other nice thing about the WiFi solution is that the serial port is high speed and bidirectional. It is my understanding,
although not yet tested or proven, that the 2400 baud remote port is only input.

I have included my VB program that I use to monitor the serial line between the PC and the bot. I had to build up a special serial cable.

Code: Select all
 PC                                          Robot
                   MonTX
                       |         
TX --------------------------------------------RX
RX -------------------------------------------TX
                       |
                    MonRX


The Monitor lines go to com1 and com2 of a separate PC that runs the VB program.
You could use one PC if it had three serial ports.
Don't forget to tie the signal grounds together too.
There is a resistor in the PC connector of the original cable that needs to be there.
I forget which pin it is on, I think it was one of the handshaking lines.

The program displays the TXed and RXed data in Hex in two separate windows.
The program is not yet complete. There is a form for changing the port speed and com numbers but I haven't
had time to finish it. It also has a tendency to get some errors as the text window gets too full.
There is a clear window button that helps that problem.
Also, I could have made it format the responses a bit better in the receive data window.

You will need VB6 to compile and run it.
If you don't have it I can compile a runtime for you.

dan
I asked Dan Albert the following:
    how’s the wifi-serial connection working out ?
    which one did you use ?
    Any software you’d like to share ?

Dan asked me to post his reply on the forum.


Here are some source code project files :

I have the ATOP ABLELink GW21SW-MAXI.
Image
It seems to work very well. I have written an embedded C++ app for my Axim that can send some basic commands to the KHR-1. I can put it into sleep mode, wake it up, and have it play 1 of the 40 stored motions.
The GW21SW-MAXI draws a bit to much current. At 450 mAmps that will drain the original pack fairly rapidly.
I've switched to using 1200mAh NiMH packs I got from www.onlybatteries.com They are much better than
the 600mAh Nicad pack that came with the KHR-1.
The other nice thing about the WiFi solution is that the serial port is high speed and bidirectional. It is my understanding,
although not yet tested or proven, that the 2400 baud remote port is only input.

I have included my VB program that I use to monitor the serial line between the PC and the bot. I had to build up a special serial cable.

Code: Select all
 PC                                          Robot
                   MonTX
                       |         
TX --------------------------------------------RX
RX -------------------------------------------TX
                       |
                    MonRX


The Monitor lines go to com1 and com2 of a separate PC that runs the VB program.
You could use one PC if it had three serial ports.
Don't forget to tie the signal grounds together too.
There is a resistor in the PC connector of the original cable that needs to be there.
I forget which pin it is on, I think it was one of the handshaking lines.

The program displays the TXed and RXed data in Hex in two separate windows.
The program is not yet complete. There is a form for changing the port speed and com numbers but I haven't
had time to finish it. It also has a tendency to get some errors as the text window gets too full.
There is a clear window button that helps that problem.
Also, I could have made it format the responses a bit better in the receive data window.

You will need VB6 to compile and run it.
If you don't have it I can compile a runtime for you.

dan
Last edited by limor on Thu Dec 14, 2006 2:27 am, edited 3 times in total.
limor
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 1845
Joined: Mon Oct 11, 2004 1:00 am
Location: London, UK

Post by limor » Mon Mar 14, 2005 8:28 pm

Post by limor
Mon Mar 14, 2005 8:28 pm

check out what Dan uploaded :

Image
check out what Dan uploaded :

Image
Last edited by limor on Tue Sep 26, 2006 11:25 am, edited 2 times in total.
limor
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 1845
Joined: Mon Oct 11, 2004 1:00 am
Location: London, UK

Visual Basic Monitor program

Post by DanAlbert » Wed Mar 16, 2005 8:55 pm

Post by DanAlbert
Wed Mar 16, 2005 8:55 pm

Some added info:

My original cable split the lines into two additional cables. One for TX and one for RX. It occurs to me that you should need only one cable with both signals. The program would need to be changed to use only COMM1. It currently uses COMM1 and COMM2. I will do this in the future after the RoboGames in SF.
Also, the resister inside the cable that comes with KHR-1 is used to pull down the TX line from the robot to DTR. Therefor DTR must be a low voltage. I found this out because the 802.11 device I was using had DTR always high. IE. commands were getting thru but responses were not coming back.

dan
Some added info:

My original cable split the lines into two additional cables. One for TX and one for RX. It occurs to me that you should need only one cable with both signals. The program would need to be changed to use only COMM1. It currently uses COMM1 and COMM2. I will do this in the future after the RoboGames in SF.
Also, the resister inside the cable that comes with KHR-1 is used to pull down the TX line from the robot to DTR. Therefor DTR must be a low voltage. I found this out because the 802.11 device I was using had DTR always high. IE. commands were getting thru but responses were not coming back.

dan
DanAlbert
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 70
Joined: Fri Feb 04, 2005 1:00 am

Post by Guest » Sun Aug 07, 2005 2:53 am

Post by Guest
Sun Aug 07, 2005 2:53 am

Image

has anyone disected RoboSapien's receiver board and plugged it into KHR-1 as shown in this site?

http://www.alberts-equation.net/Robotics.shtml
Image

has anyone disected RoboSapien's receiver board and plugged it into KHR-1 as shown in this site?

http://www.alberts-equation.net/Robotics.shtml
Guest

Post by DanAlbert » Wed Aug 17, 2005 2:59 pm

Post by DanAlbert
Wed Aug 17, 2005 2:59 pm

Sorry,
This RoboSapien Hack is custom programmed for the RoboSapien.
It is also built to plug into the RoboSapien's control board.
In addition the pic12F675 chip doesn't come with a UART to connect to the KHR-1.(However a good assembly programmer could make it do RS232)

However, you could build an I/R receiver and controller board to control the KHR-1 with the RoboSapien remote.
If I wanted to do this I might use a more powerful Pic that has a UART interface built in and then it is just a matter of preprogramming the RoboSapien moves into the KHR-1. (SMOP) Simple Matter of Prpramming)

Of course the KHR-1 is not as stable as the RS so some extra moves like "stand up" are in order. An IMU or at least a gyro would help too.

Anyway, it was a cool idea and I may add an I/R interface to the custom Robo-1 I am now constructing.

Dan Albert
Albert's Equation Inc.
Sorry,
This RoboSapien Hack is custom programmed for the RoboSapien.
It is also built to plug into the RoboSapien's control board.
In addition the pic12F675 chip doesn't come with a UART to connect to the KHR-1.(However a good assembly programmer could make it do RS232)

However, you could build an I/R receiver and controller board to control the KHR-1 with the RoboSapien remote.
If I wanted to do this I might use a more powerful Pic that has a UART interface built in and then it is just a matter of preprogramming the RoboSapien moves into the KHR-1. (SMOP) Simple Matter of Prpramming)

Of course the KHR-1 is not as stable as the RS so some extra moves like "stand up" are in order. An IMU or at least a gyro would help too.

Anyway, it was a cool idea and I may add an I/R interface to the custom Robo-1 I am now constructing.

Dan Albert
Albert's Equation Inc.
DanAlbert
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 70
Joined: Fri Feb 04, 2005 1:00 am

VB Sample missing file?

Post by Guest » Tue Sep 06, 2005 2:35 am

Post by Guest
Tue Sep 06, 2005 2:35 am

I downloaded all the files and tried opening the visual basic program posted above, but I get an error message when opening the project that the file w32comms.bas could not be found. :cry: Can this file be made available? Doesn't work without whatever is in that module. Thanks
I downloaded all the files and tried opening the visual basic program posted above, but I get an error message when opening the project that the file w32comms.bas could not be found. :cry: Can this file be made available? Doesn't work without whatever is in that module. Thanks
Guest

Post by DanAlbert » Wed Sep 14, 2005 2:46 am

Post by DanAlbert
Wed Sep 14, 2005 2:46 am

Sorry about the missing file.
I have uploaded it.
If you have any questions about the program, don't hesitate to contact me directly.

dan@sjrobotics.org
Sorry about the missing file.
I have uploaded it.
If you have any questions about the program, don't hesitate to contact me directly.

dan@sjrobotics.org
DanAlbert
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 70
Joined: Fri Feb 04, 2005 1:00 am

Post by Pygmy » Mon Aug 14, 2006 11:11 am

Post by Pygmy
Mon Aug 14, 2006 11:11 am

Dear Dan
May ask you that where you upload the mising file?
Dear Dan
May ask you that where you upload the mising file?
Pygmy
Newbie
Newbie
User avatar
Posts: 1
Joined: Mon Aug 14, 2006 1:00 am

Post by DanAlbert » Mon Aug 14, 2006 2:35 pm

Post by DanAlbert
Mon Aug 14, 2006 2:35 pm

try under KHR-1 documentation.

There is a big "W" link that will download a word file.

It is the first draft and I may have a second draft lying around somewhere.

Let me know if it was helpful.

Dan
try under KHR-1 documentation.

There is a big "W" link that will download a word file.

It is the first draft and I may have a second draft lying around somewhere.

Let me know if it was helpful.

Dan
DanAlbert
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 70
Joined: Fri Feb 04, 2005 1:00 am

Post by animetown9 » Sat Sep 23, 2006 10:54 pm

Post by animetown9
Sat Sep 23, 2006 10:54 pm

Hi all.

I try to download the above file and load into VB6
However, when I load it in...

it says...
file not fould "w32comms.bas"

Did I do something wrong.

Thanks in advance.
Hi all.

I try to download the above file and load into VB6
However, when I load it in...

it says...
file not fould "w32comms.bas"

Did I do something wrong.

Thanks in advance.
animetown9
Robot Builder
Robot Builder
User avatar
Posts: 18
Joined: Wed Sep 13, 2006 1:00 am

Post by DanAlbert » Tue Sep 26, 2006 2:29 am

Post by DanAlbert
Tue Sep 26, 2006 2:29 am

Copy and paste the code below and save it as w32comms.bas

Attribute VB_Name = "W32Comms"
Option Explicit
Global RXByteBuffer() As Byte
Global TXByteBuffer() As Byte
Global RXByteTemp() As Byte
Global TXByteTemp() As Byte
Global intNextRX As Integer
Global intNextTX As Integer
Global intRXptr As Integer
Global intTXptr As Integer
Global variantRXBuffer As Variant
Global variantTXBuffer As Variant
Global variantOutData As Variant
Global ChkSum As Byte
Global Cmd As Byte
Global bytePortVal As Byte
Global intRXforCnt As Integer
Global intTXforCnt As Integer
Global CommPort As Integer
Global CurrentServo As Integer
' COMM stuff from WIN32API.TXT

' Serial provider type.
Const SP_SERIALCOMM = &H1&

' Provider SubTypes
Const PST_UNSPECIFIED = &H0&
Const PST_RS232 = &H1&
Const PST_PARALLELPORT = &H2&
Const PST_RS422 = &H3&
Const PST_RS423 = &H4&
Const PST_RS449 = &H5&
Const PST_FAX = &H21&
Const PST_SCANNER = &H22&
Const PST_NETWORK_BRIDGE = &H100&
Const PST_LAT = &H101&
Const PST_TCPIP_TELNET = &H102&
Const PST_X25 = &H103&

' Provider capabilities flags.
Const PCF_DTRDSR = &H1&
Const PCF_RTSCTS = &H2&
Const PCF_RLSD = &H4&
Const PCF_PARITY_CHECK = &H8&
Const PCF_XONXOFF = &H10&
Const PCF_SETXCHAR = &H20&
Const PCF_TOTALTIMEOUTS = &H40&
Const PCF_INTTIMEOUTS = &H80&
Const PCF_SPECIALCHARS = &H100&
Const PCF_16BITMODE = &H200&

' Comm provider settable parameters.
Const SP_PARITY = &H1&
Const SP_BAUD = &H2&
Const SP_DATABITS = &H4&
Const SP_STOPBITS = &H8&
Const SP_HANDSHAKING = &H10&
Const SP_PARITY_CHECK = &H20&
Const SP_RLSD = &H40&

' Settable baud rates in the provider.
Const BAUD_075 = &H1&
Const BAUD_110 = &H2&
Const BAUD_134_5 = &H4&
Const BAUD_150 = &H8&
Const BAUD_300 = &H10&
Const BAUD_600 = &H20&
Const BAUD_1200 = &H40&
Const BAUD_1800 = &H80&
Const BAUD_2400 = &H100&
Const BAUD_4800 = &H200&
Const BAUD_7200 = &H400&
Const BAUD_9600 = &H800&
Const BAUD_14400 = &H1000&
Const BAUD_19200 = &H2000&
Const BAUD_38400 = &H4000&
Const BAUD_56K = &H8000&
Const BAUD_128K = &H10000
Const BAUD_115200 = &H20000
Const BAUD_57600 = &H40000
Const BAUD_USER = &H10000000

' Settable Data Bits
Const DATABITS_5 = &H1&
Const DATABITS_6 = &H2&
Const DATABITS_7 = &H4&
Const DATABITS_8 = &H8&
Const DATABITS_16 = &H10&
Const DATABITS_16X = &H20&

' Settable Stop and Parity bits.
Const STOPBITS_10 = &H1&
Const STOPBITS_15 = &H2&
Const STOPBITS_20 = &H4&
Const PARITY_NONE = &H100&
Const PARITY_ODD = &H200&
Const PARITY_EVEN = &H400&
Const PARITY_MARK = &H800&
Const PARITY_SPACE = &H1000&

Type COMMPROP
wPacketLength As Integer
wPacketVersion As Integer
dwServiceMask As Long
dwReserved1 As Long
dwMaxTxQueue As Long
dwMaxRxQueue As Long
dwMaxBaud As Long
dwProvSubType As Long
dwProvCapabilities As Long
dwSettableParams As Long
dwSettableBaud As Long
wSettableData As Integer
wSettableStopParity As Integer
dwCurrentTxQueue As Long
dwCurrentRxQueue As Long
dwProvSpec1 As Long
dwProvSpec2 As Long
wcProvChar(1) As Integer
End Type

'Type COMSTAT
' fCtsHold As Long
' fDsrHold As Long
' fRlsdHold As Long
' fXoffHold As Long
' fXoffSent As Long
' fEof As Long
' fTxim As Long
' fReserved As Long
' cbInQue As Long
' cbOutQue As Long
'End Type

Type COMSTAT
fBitFields As Long 'See Comment in Win32API.Txt
cbInQue As Long
cbOutQue As Long
End Type
' The eight actual COMSTAT bit-sized data fields within the four bytes of fBitFields can be manipulated by bitwise logical And/Or operations.
' FieldName Bit # Description
' --------- ----- ---------------------------
' fCtsHold 1 Tx waiting for CTS signal
' fDsrHold 2 Tx waiting for DSR signal
' fRlsdHold 3 Tx waiting for RLSD signal
' fXoffHold 4 Tx waiting, XOFF char rec'd
' fXoffSent 5 Tx waiting, XOFF char sent
' fEof 6 EOF character sent
' fTxim 7 character waiting for Tx
' fReserved 8 reserved (25 bits)

' DTR Control Flow Values.
Const DTR_CONTROL_DISABLE = &H0
Const DTR_CONTROL_ENABLE = &H1
Const DTR_CONTROL_HANDSHAKE = &H2

' RTS Control Flow Values
Const RTS_CONTROL_DISABLE = &H0
Const RTS_CONTROL_ENABLE = &H1
Const RTS_CONTROL_HANDSHAKE = &H2
Const RTS_CONTROL_TOGGLE = &H3

'Type DCB
' DCBlength As Long
' BaudRate As Long
' fBinary As Long
' fParity As Long
' fOutxCtsFlow As Long
' fOutxDsrFlow As Long
' fDtrControl As Long
' fDsrSensitivity As Long
' fTXContinueOnXoff As Long
' fOutX As Long
' fInX As Long
' fErrorChar As Long
' fNull As Long
' fRtsControl As Long
' fAbortOnError As Long
' fDummy2 As Long
' wReserved As Integer
' XonLim As Integer
' XoffLim As Integer
' ByteSize As Byte
' Parity As Byte
' StopBits As Byte
' XonChar As Byte
' XoffChar As Byte
' ErrorChar As Byte
' EofChar As Byte
' EvtChar As Byte
'End Type

Type dcb
DCBlength As Long
BaudRate As Long
fBitFields As Long 'See Comments in Win32API.Txt
wReserved As Integer
XonLim As Integer
XoffLim As Integer
ByteSize As Byte
Parity As Byte
StopBits As Byte
XonChar As Byte
XoffChar As Byte
ErrorChar As Byte
EofChar As Byte
EvtChar As Byte
wReserved1 As Integer 'Reserved; Do Not Use
End Type
' The fourteen actual DCB bit-sized data fields within the four bytes of fBitFields can be manipulated by bitwise logical And/Or operations.
' FieldName Bit # Description
' ----------------- ----- ------------------------------
' fBinary 1 binary mode, no EOF check
' fParity 2 enable parity checking
' fOutxCtsFlow 3 CTS output flow control
' fOutxDsrFlow 4 DSR output flow control
' fDtrControl 5 DTR flow control type (2 bits)
' fDsrSensitivity 7 DSR sensitivity
' fTXContinueOnXoff 8 XOFF continues Tx
' fOutX 9 XON/XOFF out flow control
' fInX 10 XON/XOFF in flow control
' fErrorChar 11 enable error replacement
' fNull 12 enable null stripping
' fRtsControl 13 RTS flow control (2 bits)
' fAbortOnError 15 abort reads/writes on error
' fDummy2 16 reserved

Type COMMTIMEOUTS
ReadIntervalTimeout As Long
ReadTotalTimeoutMultiplier As Long
ReadTotalTimeoutConstant As Long
WriteTotalTimeoutMultiplier As Long
WriteTotalTimeoutConstant As Long
End Type


' COMM declarations
Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As dcb) As Long
Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB As dcb) As Long
Declare Function GetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Declare Function PurgeComm Lib "kernel32" (ByVal hFile As Long, ByVal dwFlags As Long) As Long
Declare Function BuildCommDCB Lib "kernel32" Alias "BuildCommDCBA" (ByVal lpDef As String, lpDCB As dcb) As Long
Declare Function BuildCommDCBAndTimeouts Lib "kernel32" Alias "BuildCommDCBAndTimeoutsA" (ByVal lpDef As String, lpDCB As dcb, lpCommTimeouts As COMMTIMEOUTS) As Long
Declare Function TransmitCommChar Lib "kernel32" (ByVal nCid As Long, ByVal cChar As Byte) As Long
Declare Function SetCommBreak Lib "kernel32" (ByVal nCid As Long) As Long
Declare Function SetCommMask Lib "kernel32" (ByVal hFile As Long, ByVal dwEvtMask As Long) As Long
Declare Function ClearCommBreak Lib "kernel32" (ByVal nCid As Long) As Long
Declare Function ClearCommError Lib "kernel32" (ByVal hFile As Long, lpErrors As Long, lpStat As COMSTAT) As Long
Declare Function SetupComm Lib "kernel32" (ByVal hFile As Long, ByVal dwInQueue As Long, ByVal dwOutQueue As Long) As Long
Declare Function EscapeCommFunction Lib "kernel32" (ByVal nCid As Long, ByVal nFunc As Long) As Long
Declare Function GetCommMask Lib "kernel32" (ByVal hFile As Long, lpEvtMask As Long) As Long
Declare Function GetCommProperties Lib "kernel32" (ByVal hFile As Long, lpCommProp As COMMPROP) As Long
Declare Function GetCommModemStatus Lib "kernel32" (ByVal hFile As Long, lpModemStat As Long) As Long
'Declare Function WaitCommEvent Lib "kernel32" (ByVal hFile As Long, lpEvtMask As Long, lpOverlapped As OVERLAPPED) As Long

Public Sub sendData()
For intTXforCnt = 0 To intNextTX - 1
TXByteBuffer(intTXptr) = TXByteTemp(intTXforCnt)
intTXptr = intTXptr + 1
Next intTXforCnt

End Sub

' Set baud rate using Win32 API.
' The PortOpen property should be set to True before calling.
' May raise the following errors:
' comPortNotOpen the PortOpen property has not been set to True
' comDCBError failed to read current state of the port
' comSetCommStateFailed failed to set new baud rate
Sub SetBaudRate(Com As MSComm, baud As Long)
Dim ComDcb As dcb
Dim ret As Long

' Check port is open
If Not Com.PortOpen Then
Err.Raise comPortNotOpen, Com.Name, _
"Operation valid only when the port is open"
Exit Sub
End If

' Get existing Comm state
ret = GetCommState(Com.CommID, ComDcb)
If ret = 0 Then
Err.Raise comDCBError, Com.Name, _
"Could not read current state of the port"
Exit Sub
End If

' Modify state with new baud rate
ComDcb.BaudRate = baud
' Set the new Comm state
ret = SetCommState(Com.CommID, ComDcb)
If ret = 0 Then
Err.Raise comSetCommStateFailed, Com.Name, _
"Could not set port to specified baud rate" + Str(baud)
Exit Sub
End If
End Sub

' Get baud rate using Win32 API
' The PortOpen property should be set to True before calling.
' May raise the following errors:
' comPortNotOpen the PortOpen property has not been set to True
' comDCBError failed to read current state of the port
Function GetBaudRate(Com As MSComm) As Long
Dim ComDcb As dcb
Dim ret As Long

GetBaudRate = 0

' Check port is open
If Not Com.PortOpen Then
Err.Raise comPortNotOpen, Com.Name, _
"Operation valid only when the port is open"
Exit Function
End If

' Get Comm state
ret = GetCommState(Com.CommID, ComDcb)
If ret = 0 Then
Err.Raise comDCBError, Com.Name, _
"Could not read current state of the port"
Exit Function
End If
' Extract baud rate
GetBaudRate = ComDcb.BaudRate
End Function
Copy and paste the code below and save it as w32comms.bas

Attribute VB_Name = "W32Comms"
Option Explicit
Global RXByteBuffer() As Byte
Global TXByteBuffer() As Byte
Global RXByteTemp() As Byte
Global TXByteTemp() As Byte
Global intNextRX As Integer
Global intNextTX As Integer
Global intRXptr As Integer
Global intTXptr As Integer
Global variantRXBuffer As Variant
Global variantTXBuffer As Variant
Global variantOutData As Variant
Global ChkSum As Byte
Global Cmd As Byte
Global bytePortVal As Byte
Global intRXforCnt As Integer
Global intTXforCnt As Integer
Global CommPort As Integer
Global CurrentServo As Integer
' COMM stuff from WIN32API.TXT

' Serial provider type.
Const SP_SERIALCOMM = &H1&

' Provider SubTypes
Const PST_UNSPECIFIED = &H0&
Const PST_RS232 = &H1&
Const PST_PARALLELPORT = &H2&
Const PST_RS422 = &H3&
Const PST_RS423 = &H4&
Const PST_RS449 = &H5&
Const PST_FAX = &H21&
Const PST_SCANNER = &H22&
Const PST_NETWORK_BRIDGE = &H100&
Const PST_LAT = &H101&
Const PST_TCPIP_TELNET = &H102&
Const PST_X25 = &H103&

' Provider capabilities flags.
Const PCF_DTRDSR = &H1&
Const PCF_RTSCTS = &H2&
Const PCF_RLSD = &H4&
Const PCF_PARITY_CHECK = &H8&
Const PCF_XONXOFF = &H10&
Const PCF_SETXCHAR = &H20&
Const PCF_TOTALTIMEOUTS = &H40&
Const PCF_INTTIMEOUTS = &H80&
Const PCF_SPECIALCHARS = &H100&
Const PCF_16BITMODE = &H200&

' Comm provider settable parameters.
Const SP_PARITY = &H1&
Const SP_BAUD = &H2&
Const SP_DATABITS = &H4&
Const SP_STOPBITS = &H8&
Const SP_HANDSHAKING = &H10&
Const SP_PARITY_CHECK = &H20&
Const SP_RLSD = &H40&

' Settable baud rates in the provider.
Const BAUD_075 = &H1&
Const BAUD_110 = &H2&
Const BAUD_134_5 = &H4&
Const BAUD_150 = &H8&
Const BAUD_300 = &H10&
Const BAUD_600 = &H20&
Const BAUD_1200 = &H40&
Const BAUD_1800 = &H80&
Const BAUD_2400 = &H100&
Const BAUD_4800 = &H200&
Const BAUD_7200 = &H400&
Const BAUD_9600 = &H800&
Const BAUD_14400 = &H1000&
Const BAUD_19200 = &H2000&
Const BAUD_38400 = &H4000&
Const BAUD_56K = &H8000&
Const BAUD_128K = &H10000
Const BAUD_115200 = &H20000
Const BAUD_57600 = &H40000
Const BAUD_USER = &H10000000

' Settable Data Bits
Const DATABITS_5 = &H1&
Const DATABITS_6 = &H2&
Const DATABITS_7 = &H4&
Const DATABITS_8 = &H8&
Const DATABITS_16 = &H10&
Const DATABITS_16X = &H20&

' Settable Stop and Parity bits.
Const STOPBITS_10 = &H1&
Const STOPBITS_15 = &H2&
Const STOPBITS_20 = &H4&
Const PARITY_NONE = &H100&
Const PARITY_ODD = &H200&
Const PARITY_EVEN = &H400&
Const PARITY_MARK = &H800&
Const PARITY_SPACE = &H1000&

Type COMMPROP
wPacketLength As Integer
wPacketVersion As Integer
dwServiceMask As Long
dwReserved1 As Long
dwMaxTxQueue As Long
dwMaxRxQueue As Long
dwMaxBaud As Long
dwProvSubType As Long
dwProvCapabilities As Long
dwSettableParams As Long
dwSettableBaud As Long
wSettableData As Integer
wSettableStopParity As Integer
dwCurrentTxQueue As Long
dwCurrentRxQueue As Long
dwProvSpec1 As Long
dwProvSpec2 As Long
wcProvChar(1) As Integer
End Type

'Type COMSTAT
' fCtsHold As Long
' fDsrHold As Long
' fRlsdHold As Long
' fXoffHold As Long
' fXoffSent As Long
' fEof As Long
' fTxim As Long
' fReserved As Long
' cbInQue As Long
' cbOutQue As Long
'End Type

Type COMSTAT
fBitFields As Long 'See Comment in Win32API.Txt
cbInQue As Long
cbOutQue As Long
End Type
' The eight actual COMSTAT bit-sized data fields within the four bytes of fBitFields can be manipulated by bitwise logical And/Or operations.
' FieldName Bit # Description
' --------- ----- ---------------------------
' fCtsHold 1 Tx waiting for CTS signal
' fDsrHold 2 Tx waiting for DSR signal
' fRlsdHold 3 Tx waiting for RLSD signal
' fXoffHold 4 Tx waiting, XOFF char rec'd
' fXoffSent 5 Tx waiting, XOFF char sent
' fEof 6 EOF character sent
' fTxim 7 character waiting for Tx
' fReserved 8 reserved (25 bits)

' DTR Control Flow Values.
Const DTR_CONTROL_DISABLE = &H0
Const DTR_CONTROL_ENABLE = &H1
Const DTR_CONTROL_HANDSHAKE = &H2

' RTS Control Flow Values
Const RTS_CONTROL_DISABLE = &H0
Const RTS_CONTROL_ENABLE = &H1
Const RTS_CONTROL_HANDSHAKE = &H2
Const RTS_CONTROL_TOGGLE = &H3

'Type DCB
' DCBlength As Long
' BaudRate As Long
' fBinary As Long
' fParity As Long
' fOutxCtsFlow As Long
' fOutxDsrFlow As Long
' fDtrControl As Long
' fDsrSensitivity As Long
' fTXContinueOnXoff As Long
' fOutX As Long
' fInX As Long
' fErrorChar As Long
' fNull As Long
' fRtsControl As Long
' fAbortOnError As Long
' fDummy2 As Long
' wReserved As Integer
' XonLim As Integer
' XoffLim As Integer
' ByteSize As Byte
' Parity As Byte
' StopBits As Byte
' XonChar As Byte
' XoffChar As Byte
' ErrorChar As Byte
' EofChar As Byte
' EvtChar As Byte
'End Type

Type dcb
DCBlength As Long
BaudRate As Long
fBitFields As Long 'See Comments in Win32API.Txt
wReserved As Integer
XonLim As Integer
XoffLim As Integer
ByteSize As Byte
Parity As Byte
StopBits As Byte
XonChar As Byte
XoffChar As Byte
ErrorChar As Byte
EofChar As Byte
EvtChar As Byte
wReserved1 As Integer 'Reserved; Do Not Use
End Type
' The fourteen actual DCB bit-sized data fields within the four bytes of fBitFields can be manipulated by bitwise logical And/Or operations.
' FieldName Bit # Description
' ----------------- ----- ------------------------------
' fBinary 1 binary mode, no EOF check
' fParity 2 enable parity checking
' fOutxCtsFlow 3 CTS output flow control
' fOutxDsrFlow 4 DSR output flow control
' fDtrControl 5 DTR flow control type (2 bits)
' fDsrSensitivity 7 DSR sensitivity
' fTXContinueOnXoff 8 XOFF continues Tx
' fOutX 9 XON/XOFF out flow control
' fInX 10 XON/XOFF in flow control
' fErrorChar 11 enable error replacement
' fNull 12 enable null stripping
' fRtsControl 13 RTS flow control (2 bits)
' fAbortOnError 15 abort reads/writes on error
' fDummy2 16 reserved

Type COMMTIMEOUTS
ReadIntervalTimeout As Long
ReadTotalTimeoutMultiplier As Long
ReadTotalTimeoutConstant As Long
WriteTotalTimeoutMultiplier As Long
WriteTotalTimeoutConstant As Long
End Type


' COMM declarations
Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As dcb) As Long
Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB As dcb) As Long
Declare Function GetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Declare Function PurgeComm Lib "kernel32" (ByVal hFile As Long, ByVal dwFlags As Long) As Long
Declare Function BuildCommDCB Lib "kernel32" Alias "BuildCommDCBA" (ByVal lpDef As String, lpDCB As dcb) As Long
Declare Function BuildCommDCBAndTimeouts Lib "kernel32" Alias "BuildCommDCBAndTimeoutsA" (ByVal lpDef As String, lpDCB As dcb, lpCommTimeouts As COMMTIMEOUTS) As Long
Declare Function TransmitCommChar Lib "kernel32" (ByVal nCid As Long, ByVal cChar As Byte) As Long
Declare Function SetCommBreak Lib "kernel32" (ByVal nCid As Long) As Long
Declare Function SetCommMask Lib "kernel32" (ByVal hFile As Long, ByVal dwEvtMask As Long) As Long
Declare Function ClearCommBreak Lib "kernel32" (ByVal nCid As Long) As Long
Declare Function ClearCommError Lib "kernel32" (ByVal hFile As Long, lpErrors As Long, lpStat As COMSTAT) As Long
Declare Function SetupComm Lib "kernel32" (ByVal hFile As Long, ByVal dwInQueue As Long, ByVal dwOutQueue As Long) As Long
Declare Function EscapeCommFunction Lib "kernel32" (ByVal nCid As Long, ByVal nFunc As Long) As Long
Declare Function GetCommMask Lib "kernel32" (ByVal hFile As Long, lpEvtMask As Long) As Long
Declare Function GetCommProperties Lib "kernel32" (ByVal hFile As Long, lpCommProp As COMMPROP) As Long
Declare Function GetCommModemStatus Lib "kernel32" (ByVal hFile As Long, lpModemStat As Long) As Long
'Declare Function WaitCommEvent Lib "kernel32" (ByVal hFile As Long, lpEvtMask As Long, lpOverlapped As OVERLAPPED) As Long

Public Sub sendData()
For intTXforCnt = 0 To intNextTX - 1
TXByteBuffer(intTXptr) = TXByteTemp(intTXforCnt)
intTXptr = intTXptr + 1
Next intTXforCnt

End Sub

' Set baud rate using Win32 API.
' The PortOpen property should be set to True before calling.
' May raise the following errors:
' comPortNotOpen the PortOpen property has not been set to True
' comDCBError failed to read current state of the port
' comSetCommStateFailed failed to set new baud rate
Sub SetBaudRate(Com As MSComm, baud As Long)
Dim ComDcb As dcb
Dim ret As Long

' Check port is open
If Not Com.PortOpen Then
Err.Raise comPortNotOpen, Com.Name, _
"Operation valid only when the port is open"
Exit Sub
End If

' Get existing Comm state
ret = GetCommState(Com.CommID, ComDcb)
If ret = 0 Then
Err.Raise comDCBError, Com.Name, _
"Could not read current state of the port"
Exit Sub
End If

' Modify state with new baud rate
ComDcb.BaudRate = baud
' Set the new Comm state
ret = SetCommState(Com.CommID, ComDcb)
If ret = 0 Then
Err.Raise comSetCommStateFailed, Com.Name, _
"Could not set port to specified baud rate" + Str(baud)
Exit Sub
End If
End Sub

' Get baud rate using Win32 API
' The PortOpen property should be set to True before calling.
' May raise the following errors:
' comPortNotOpen the PortOpen property has not been set to True
' comDCBError failed to read current state of the port
Function GetBaudRate(Com As MSComm) As Long
Dim ComDcb As dcb
Dim ret As Long

GetBaudRate = 0

' Check port is open
If Not Com.PortOpen Then
Err.Raise comPortNotOpen, Com.Name, _
"Operation valid only when the port is open"
Exit Function
End If

' Get Comm state
ret = GetCommState(Com.CommID, ComDcb)
If ret = 0 Then
Err.Raise comDCBError, Com.Name, _
"Could not read current state of the port"
Exit Function
End If
' Extract baud rate
GetBaudRate = ComDcb.BaudRate
End Function
DanAlbert
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 70
Joined: Fri Feb 04, 2005 1:00 am


14 postsPage 1 of 1
14 postsPage 1 of 1