Author |
Topic: COM Lighting Control. (Read 589 times) |
|
thefamouscash
New Member
member is offline


Posts: 17
|
 |
COM Lighting Control.
« Thread started on: Aug 23rd, 2010, 11:55pm » |
|
I have programmed in BBC Basic since first on market and can usually get it to do what I want. I have written a prog for snooker halls which is successful but need to control each table light ON/OFF through COM port. I have purchased a UDIN-8R device which will control 8 switches to experiment but cannot see how it works. (No CD or instructions).
I understand all I need to do is pass ascii codes through the USB. Not getting anywhere.
I cannot get the advice under Modem control input/output help to work. Any assistance, please?
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: COM Lighting Control.
« Reply #1 on: Aug 24th, 2010, 08:13am » |
|
on Aug 23rd, 2010, 11:55pm, thefamouscash wrote:I have purchased a UDIN-8R device which will control 8 switches to experiment but cannot see how it works. (No CD or instructions). |
|
Google found me this: http://www.audon.co.uk/udin.html
It would appear that you can drive it either as a virtual COM port or using direct DLL calls, both of which should be straightforward from BB4W. Various software drivers and examples are available for download from that site.
Richard.
|
|
Logged
|
|
|
|
thefamouscash
New Member
member is offline


Posts: 17
|
 |
Re: COM Lighting Control.
« Reply #2 on: Aug 24th, 2010, 12:56pm » |
|
Thank you for your response. Beyond me still at the moment. Guess I will just have to keep experimenting. Regards.
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: COM Lighting Control.
« Reply #3 on: Aug 24th, 2010, 1:03pm » |
|
on Aug 24th, 2010, 12:56pm, thefamouscash wrote:Beyond me still at the moment. |
|
There are many examples of accessing external devices via DLL calls that you can find in the various sources of BB4W code. It should be a relatively simple matter of adapting one of those (e.g. the K8055D drivers would be a good starting point). So long as you have the DLL itself, its documentation and code examples in, say, Visual Basic or C it should be plain sailing.
Richard.
|
|
Logged
|
|
|
|
thefamouscash
New Member
member is offline


Posts: 17
|
 |
Re: COM Lighting Control.
« Reply #4 on: Aug 25th, 2010, 12:11pm » |
|
I really am trying but it's all too much for me. Is there no way to spell it out for me? According to the manual I just need to send "N1" to a port to switch on relay 1. I have tried using all code I could fing in "Help" (Set RTS et cetera) in association but no success. Just can't get basic understanding of process.
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: COM Lighting Control.
« Reply #5 on: Aug 25th, 2010, 1:17pm » |
|
on Aug 25th, 2010, 12:11pm, thefamouscash wrote:According to the manual I just need to send "N1" to a port to switch on relay 1. |
|
That's presumably using the 'virtual COM port' method. Personally I would prefer to use the DLL method instead, because it seems more straightforward and gives a more 'direct' communication path than going through a serial data channel that doesn't really exist.
Quote:Is there no way to spell it out for me? |
|
Download the DLL and store it somewhere convenient for your BASIC program to access it (e.g. the BB4W \LIB folder). Look up the documentation for the DLL, noting the names, prototypes and descriptions of the various calls you need to make. Check out any example code provided, e.g. in Visual Basic or C. Either write BBC BASIC code from scratch, based on the DLL documentation, or translate the example code, adapting it to do what you need. The automated translator VB2BBC may help here. Pull it all together in your program. The code should loosely resemble that listed under 'Input/output using InpOut32' in the main BB4W documentation. Test and debug your program. Richard.
|
|
Logged
|
|
|
|
thefamouscash
New Member
member is offline


Posts: 17
|
 |
Re: COM Lighting Control.
« Reply #6 on: Aug 25th, 2010, 1:22pm » |
|
Thank you once again. If I can find the "DLL" (Once I find out what it is!) I will try to follow your advice.
|
|
Logged
|
|
|
|
thefamouscash
New Member
member is offline


Posts: 17
|
 |
Re: COM Lighting Control.
« Reply #7 on: Aug 25th, 2010, 10:37pm » |
|
Hi Richard, Thanks for all your help to date. I've been working through your suggestions with some (!) success. Have the DLL implemented and being accessed through Basic CODE: <SNIPPET> SYS "LoadLibrary", "ftd2xx.dll" TO virtpor0% </SNIPPET>
Have confirmed that this is being read in: <SNIPPET> IF virtpor0% = 0 ERROR 100, "Cannot load DLL0" </SNIPPET>
However....don't seem to be successfully making calls to the dll functions. I have some VB and .NET code however I'm not able to successfully manually convert this to BASIC code that is working. You note in your previous post that there is a VB2BBC application - I've googled unsuccessfully for this - are you able to provide a link/copy etc. Failing that can I post a more extensive code snippet - sure it is something obvious that I'm just missing.
Many thanks again.
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: COM Lighting Control.
« Reply #8 on: Aug 26th, 2010, 09:49am » |
|
on Aug 25th, 2010, 10:37pm, thefamouscash wrote:You note in your previous post that there is a VB2BBC application - I've googled unsuccessfully for this |
|
Because of its somewhat 'experimental' nature there's a closed user group for the VB2BBC application, which you would be welcome to join. See for example this message on the discussion group: http://tech.groups.yahoo.com/group/bb4w/message/13029
Quote:Failing that can I post a more extensive code snippet |
|
That's what I suggest you do. Unlike the discussion group, where posting significant amounts of code is discouraged, it's OK to do so here (remember to use 'code' tags so it is properly displayed and formatted).
Richard.
|
|
Logged
|
|
|
|
thefamouscash
New Member
member is offline


Posts: 17
|
 |
Re: COM Lighting Control.
« Reply #9 on: Aug 27th, 2010, 01:02am » |
|
Richard, Trying to follow your advice within my limited knowledge. I trust I am not contravening law by copying the sample below. Hope this is what is required. Bob.
Example – How to program the device The following Visual Basic uses the FTDI DLL to directly interface the hardware. Public Class AudonUSB Private Sub SetUSB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetUSB.Click Dim BytesWritten As Integer Dim TempStringData As String Dim BytesRead As Integer 'Open device by serial number FT_Status = FT_OpenBySerialNumber(FT_Serial_Number, 1, FT_Handle) If FT_Status <> FT_OK Then MsgBox("Failed to open device.", , ) Exit Sub End If ' Reset device FT_Status = FT_ResetDevice(FT_Handle) If FT_Status <> FT_OK Then Exit Sub End If ' Purge buffers FT_Status = FT_Purge(FT_Handle, FT_PURGE_RX Or FT_PURGE_TX) If FT_Status <> FT_OK Then Exit Sub End If ' Set Baud Rate FT_Status = FT_SetBaudRate(FT_Handle, 9600) If FT_Status <> FT_OK Then Exit Sub End If ' Set parameters FT_Status = FT_SetDataCharacteristics(FT_Handle, FT_DATA_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE) If FT_Status <> FT_OK Then Exit Sub End If ' Set Flow Control FT_Status = FT_SetFlowControl(FT_Handle, FT_FLOW_NONE, 0, 0) If FT_Status <> FT_OK Then Exit Sub End If ' Display in a message box all the items that are checked. Dim indexChecked As Integer Dim iRelays As Integer = 0 ' First show the index and check state of all selected items. For Each indexChecked In SetBits.CheckedIndices iRelays = iRelays + 2 ^ Val(indexChecked.ToString()) Next ' Write string data to device Dim sOutput As String 'If iRelays < 16 Then 'sOutput = "r0" + Hex(iRelays) + Chr(13) 'Else 'sOutput = "r" + Hex(iRelays) + Chr(13) 'End If 'sOutput = Me.TextBox1.Text + Chr(13) sOutput = "r" + Trim(Str(iRelays)) + Chr(13) FT_Status = FT_Write_String(FT_Handle, sOutput, Len(sOutput), BytesWritten) If FT_Status <> FT_OK Then Exit Sub End If ' Wait Sleep(100) ' Get number of bytes waiting to be read FT_Status = FT_GetQueueStatus(FT_Handle, FT_RxQ_Bytes) If FT_Status <> FT_OK Then Exit Sub End If ' Read number of bytes waiting ' Allocate string to recieve data TempStringData = Space(FT_RxQ_Bytes + 1) FT_Status = FT_Read_String(FT_Handle, TempStringData, FT_RxQ_Bytes, BytesRead) If FT_Status <> FT_OK Then Exit Sub End If ' Close device FT_Status = FT_Close(FT_Handle) If FT_Status <> FT_OK Then Exit Sub End If End Sub Private Sub AudonUSB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim DeviceCount As Integer Dim DeviceIndex As Integer Dim TempDevString As String ' Get the number of device attached FT_Status = FT_GetNumberOfDevices(DeviceCount, vbNullChar, FT_LIST_NUMBER_ONLY) If FT_Status <> FT_OK Then Exit Sub End If ' Exit if no device connected If DeviceCount = 0 Then MsgBox("No Device Connected", MsgBoxStyle.Critical) Exit Sub End If ' Clear device list DeviceList.Items.Clear() ' List devices in dropdown For DeviceIndex = 0 To DeviceCount - 1 ' Get serial number of device with index 0 ' Allocate space for string variable TempDevString = Space(16) FT_Status = FT_GetDeviceString(DeviceIndex, TempDevString, FT_LIST_BY_INDEX Or FT_OPEN_BY_SERIAL_NUMBER) If FT_Status <> FT_OK Then Exit Sub End If FT_Serial_Number = Microsoft.VisualBasic.Left(TempDevString, InStr(1, TempDevString, vbNullChar) - 1) ' Get description of device with index 0 ' Allocate space for string variable TempDevString = Space(64) FT_Status = FT_GetDeviceString(DeviceIndex, TempDevString, FT_LIST_BY_INDEX Or FT_OPEN_BY_DESCRIPTION) If FT_Status <> FT_OK Then Exit Sub End If FT_Description = Microsoft.VisualBasic.Left(TempDevString, InStr(1, TempDevString, vbNullChar) - 1) ' Add to dropdown DeviceList.Items.Add(FT_Description + " " + FT_Serial_Number) Next ' Set first device DeviceList.SelectedIndex = 0 End Sub Private Sub DeviceList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeviceList.SelectedIndexChanged Dim sDevice As String Dim iSpace As Integer sDevice = DeviceList.Items.Item(DeviceList.SelectedIndex).ToString iSpace = InStrRev(sDevice, " ") If iSpace > 0 Then FT_Description = Trim(Microsoft.VisualBasic.Left(sDevice, iSpace - 1)) FT_Serial_Number = Trim(Microsoft.VisualBasic.Mid(sDevice, iSpace + 1)) End If End Sub Private Sub ReadUSB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadUSB.Click Dim BytesWritten As Integer Dim TempStringData As String Dim BytesRead As Integer 'Open device by serial number FT_Status = FT_OpenBySerialNumber(FT_Serial_Number, 1, FT_Handle) If FT_Status <> FT_OK Then MsgBox("Failed to open device.", , ) Exit Sub End If ' Reset device FT_Status = FT_ResetDevice(FT_Handle) If FT_Status <> FT_OK Then Exit Sub End If ' Purge buffers FT_Status = FT_Purge(FT_Handle, FT_PURGE_RX Or FT_PURGE_TX) If FT_Status <> FT_OK Then Exit Sub End If ' Set Baud Rate FT_Status = FT_SetBaudRate(FT_Handle, 9600) If FT_Status <> FT_OK Then Exit Sub End If ' Set parameters FT_Status = FT_SetDataCharacteristics(FT_Handle, FT_DATA_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE) If FT_Status <> FT_OK Then Exit Sub End If ' Set Flow Control FT_Status = FT_SetFlowControl(FT_Handle, FT_FLOW_NONE, 0, 0) If FT_Status <> FT_OK Then Exit Sub End If ' Write string data to device Dim sOutput As String If ReadInputs.Checked = True Then ' Read inputs sOutput = "i0" + Chr(13) Else ' Relay Status sOutput = "s0" + Chr(13) End If FT_Status = FT_Write_String(FT_Handle, sOutput, Len(sOutput), BytesWritten) If FT_Status <> FT_OK Then Exit Sub End If ' Wait Sleep(100) ' Get number of bytes waiting to be read FT_Status = FT_GetQueueStatus(FT_Handle, FT_RxQ_Bytes) If FT_Status <> FT_OK Then Exit Sub End If ' Read number of bytes waiting ' Allocate string to recieve data TempStringData = Space(FT_RxQ_Bytes + 1) FT_Status = FT_Read_String(FT_Handle, TempStringData, FT_RxQ_Bytes, BytesRead) If FT_Status <> FT_OK Then Exit Sub End If ' Change LED color depending on input Dim iInput As Integer TempStringData = Replace(TempStringData, "i0", " ") TempStringData = Replace(TempStringData, "s0", " ") TempStringData = Replace(TempStringData, Chr(13), " ") TempStringData = Trim(Replace(TempStringData, Chr(10), " ")) iInput = Val(TempStringData) If (iInput And 1) Then LED0.BackColor = Color.LightGreen Else LED0.BackColor = Color.DarkGreen End If If (iInput And 2) Then LED1.BackColor = Color.LightGreen Else LED1.BackColor = Color.DarkGreen End If If (iInput And 4) Then LED2.BackColor = Color.LightGreen Else LED2.BackColor = Color.DarkGreen End If If (iInput And 8) Then LED3.BackColor = Color.LightGreen Else LED3.BackColor = Color.DarkGreen End If If (iInput And 16) Then LED4.BackColor = Color.LightGreen Else LED4.BackColor = Color.DarkGreen End If If (iInput And 32) Then LED5.BackColor = Color.LightGreen Else LED5.BackColor = Color.DarkGreen End If If (iInput And 64) Then LED6.BackColor = Color.LightGreen Else LED6.BackColor = Color.DarkGreen End If If (iInput And 128) Then LED7.BackColor = Color.LightGreen Else LED7.BackColor = Color.DarkGreen End If ' Close device FT_Status = FT_Close(FT_Handle) If FT_Status <> FT_OK Then Exit Sub End If End Sub End Class
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: COM Lighting Control.
« Reply #10 on: Aug 27th, 2010, 09:05am » |
|
on Aug 27th, 2010, 01:02am, thefamouscash wrote:Hope this is what is required. |
|
I asked you to use code tags to ensure that your listing was correctly formatted, yet you didn't do so. As a result many of the lines have 'wrapped', and indentation has been lost, making it much harder to read and interpret the code. Please make full use of the facilities of this board, such as being able to embed code within messages, so that people can help you more easily.
I'll get back to you when I've managed to unscramble the code you listed.
Richard.
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: COM Lighting Control.
« Reply #11 on: Aug 27th, 2010, 09:36am » |
|
I've managed to convert the code into BBC BASIC, with the help of VB2BBC, but really it's so massively overcomplicated for what you want to achieve it seems ridiculous.
Maybe using the virtual COM port interface would be easier after all. Please list any example code provided for that mode of operation. And don't forget those code tags!
Richard.
|
|
Logged
|
|
|
|
thefamouscash
New Member
member is offline


Posts: 17
|
 |
Re: COM Lighting Control.
« Reply #12 on: Aug 27th, 2010, 09:58am » |
|
I will try to obtain that code. I noticed someone else had mentioned VB2BBC so I sent a direct email requesting a link for VB2BBC and still feel this may be of use. The code I attempted to give is (I believe) the entire code for communicating with the unit. Sorry for inconvenience, I do not know how add code tags but will find out. Bob.
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: COM Lighting Control.
« Reply #13 on: Aug 27th, 2010, 10:31am » |
|
on Aug 27th, 2010, 09:58am, thefamouscash wrote:I noticed someone else had mentioned VB2BBC so I sent a direct email requesting a link for VB2BBC and still feel this may be of use. |
|
You were sent the 'Welcome to the VB2BBC user group' email yesterday, containing the download link. However your code caused VB2BBC to crash, therefore I had to modify it to produce any usable results. That's the trouble with experimental software!
I've had very little feedback from members of the VB2BBC User Group (I think none) so I am not motivated to put much effort into an application in which so few people seem to have any interest.
Quote:I do not know how add code tags |
|
Do you not see Add tags: when you are entering your message? The 'insert code' tag is the one with a # symbol.
Richard.
|
« Last Edit: Aug 27th, 2010, 10:34am by admin » |
Logged
|
|
|
|
thefamouscash
New Member
member is offline


Posts: 17
|
 |
Re: COM Lighting Control.
« Reply #14 on: Aug 27th, 2010, 2:03pm » |
|
Apologies that my lack of skill is causing undue effort. I now see the # to insert code and will use that if I can find suitable code. I regret I do not appear to have received the welcome to BV2BBC email. Is it possible to resend? I believe I may be able to translate a piece at a time and find relevant sections.
With thanks,
Bob.
|
|
Logged
|
|
|
|
|