PDA

View Full Version : Buffer(2) is supposed to be sending DeviceID(2) but is sending zero??



jellis00
- 31st May 2010, 06:41
I have a USB interface operating between an 18F4550 and a PC where I have a VB6 program that is capturing the data. I have posted both the 18F4550 and the VB6 code excerpts that apply to my current problem. I use nibbles 7 and 6 of the TBLAD for writing the serial no. of the microcontroller unit to the MCU. In my tests I have nibble 7 of the TBLAD representing the SerialNoMSB in the variable DeviceID1 and nibble 6 of the TBLAD representing SerialNoLSB in the variable DeviceID1. I use WRITE statements to write both variables to EEPROM so I can READ the EEPROM on a post run basis to confirm the correct values were in the variables that were sent by the MCU. However, what is being captured by the VB6 code in the BufferIn(2) variable is always zero when it should be equal to the DeviceID2 variable that was sent by the MCU.
Can anyone look at my code for both ends of this USB interface and tell me why a zero would be showing up in the BufferIn(2) value on the VB6 side??

Here is the code for the main loop on the MCU side of the USB interface that contains the TX array containing the DeviceID1 and DeviceID2 variables that are sent.


Main:
' Read current time/date
HIGH ext_pwr ' Temporarily turn on power to SRF02 for I2C bus use
PAUSE 5 ' Delay to let I2C bus stabilize after SRF02 turn on
I2CREAD SDA, SCL, RTCdevice, SecReg,[sec,MINs,hr,day,date,mon,yr]
'PAUSE 20
LOW ext_pwr ' Turn off power to the SRF02 at end of routine.
' This statement will distort the I2C bus & ext_pwr must be set
' HIGH again before any attempts are made to use the I2C bus.
' Store current date/time in EEPROM
WRITE 242, Mon
WRITE 243, Date
WRITE 244, Yr
WRITE 245, hr
WRITE 246, MINs
Write 247, sec
IF LCD_Flag = 1 Then ' This code block for testing only
'Clr LCD & display date/time
PAUSE 20
LCDOut $fe,1' Clear Display
LCDOUT $fe,Line1,Hex2 mon,"/",hex2 date,"/", hex2 yr
LCDOUT $fe,Line2,hex2 hr,":",hex2 MINs,":",hex2 sec
Pause 200
ENDIF

' BufferOut (PIC to PC) Data Structure
' BufferOut(0) = 0, must always be zero as valid Report ID
' BufferOut(1) = DeviceID1 or MSB of Device Serial Number
' BufferOut(2) = DeviceID2 or LSB of Device Serial Number
' BufferOut(3) = mon
' BufferOut(4) = date
' BufferOut(5) = yr
' BufferOut(6) = hr (Pic hours time)
' BufferOut(7) = MINs (Pic minutes time)
' BufferOut(8) = sec (Pic seconds time)
' BufferOut(9) = stamp
' BufferOut(10) = rng0
' BufferOut(11) = rng1
' BufferOut(12) = BATTLOW (Low Battery Flag)
' BufferOut(13) = n/a ..0
' BufferOut(14) = n/a ..0
' BufferOut(15) = n/a ..0
' BufferOut(15) must be zero for sending data buffers (1) thru (12)
' ...else must send a text string with Buffer(15) = 1

FOR X = 0 to 1000 ; Check for incomming USB data while waiting
@ ON_USBRX_GOSUB _HandleRX
PAUSE 1
New_PORTB2 = PORTB.2 ; Check PORTB.2 pin for change
IF New_PORTb2 != Old_PORTB2 THEN ' PortB.2 changed
Old_PORTb2 = New_PORTb2
ARRAYWRITE USBTXBuffer, ["USBDemo"]
GOSUB WaitToSend ' Send data set about one/sec
' Send device ID to PC
' Send current Date/Time to PC
' Send each EEPROM stored date stamped range measurement to PC
' Send current BATTLOW indicator to PC
ARRAYWRITE USBTXBuffer,[0,deviceID1,deviceID2,Mon,date,Yr, _
hr,MINs,sec,stamp,rng0,rng1,BATTLOW,0,0,0]
GOSUB WaitToSend
ENDIF
NEXT X

IF J >= 237 THEN ' If reached maximum EEPROM usage location...
J = 16 ' ..reset pointer to start of range data
Endif
READ j, stamp ' Read date stamp of measurment
READ J+1, rng0 ' Read range measurement from EEPROM
READ J+2, rng1
Pause 10
J = J + 3

' Send Data to PC
' Send device ID to PC
' Send current Date/Time to PC
' Send each EEPROM stored range measurement to PC
' Send current BATTLOW indicator to PC
ARRAYWRITE USBTXBuffer,[0,deviceID1,deviceID2,mon,date,yr, _
hr,MINs,sec,stamp,rng0,rng1,BATTLOW,0,0,0]
GOSUB SendIfReady

' If FMReset = 1, reset EEPROM memory index for new logging epoch
GOTO Main


Here is the process on the VB6 side that captures the sent variables:


Public Sub OnRead(ByVal pHandle As Long)
'
' There's some incomming data here :o)
' read it and have fun.
' Don't forget Bufferin(0) is the ReportID
'
Dim Index As Byte
Dim strSerialNumMSB As String
Dim strSerialNumLSB As String
Dim strSerialNum As String
Dim intSerialNum As Integer
Dim lngSerialNum As Long
Dim strMon As String
Dim strDay As String
Dim strYr As String
Dim strHr As String
Dim strMINs As String
Dim strSecs As String
Dim MyInt As Integer
Dim MyStr As String
Dim strTime As String
Dim intDateStamp As Integer
Dim intFillLevel As Integer
Dim intRangeMax As Integer
Dim intRange0 As Integer
Dim intRange1 As Integer

' ReadBuffer Structure:
' ' Read data from FillMonitor via USB
' BufferIn(0) = Report id => always 0
' BufferIn(1) = DeviceID1, MSB of SerialNum ' first data item, etc etc
' BufferIn(2) = DeviceID2, LSB of SerialNum ' first data item, etc etc
' BufferIn(3) = Month
' BufferIn(4) = Date
' BufferIn(5) = Year
' BufferIn(6) = Time_Hr (PicHr)
' BufferIn(7) = Time_MINs (PicMINs)
' BufferIn(8) = Time_Secs (PicSecs)
' BufferIn(9) = intDateStamp
' BufferIn(10) = RangeLSB
' BufferIn(11) = RangeMSB
' BufferIn(12) = BATTLOW flag for low battery (PORTA.5)
' BufferIn(13) = n/a = 0
' BufferIn(14) = n/a = 0
' BufferIn(15) = n/a = 0
' BufferIn(15) = must be zero for reading buffers (1) thru (12)
' else BufferIn is a text string when In(15)= 1
' When m_blnFlagRX is set and both BufferIn(0) = 0 and BufferIn(15) = 0 then,
' we display the PIC transmitted data: the device ID, the PIC clock date/time, range, & Battery state
' ....else when BufferIn(15) = 1 we receive a text string to be displayed continuously.

If hidRead(pHandle, BufferIn(0)) Then ' valid ReportID?
' first byte is the report ID, e.g. BufferIn(0)
' the other bytes are the data payload from the USB PIC
If BufferIn(15) = 0 And m_blnFlagRX = True Then ' There is incoming data set from PIC
'\\ Read & display inbound data from USB PIC

'\\ Display DeviceID as Serial Number
strSerialNumMSB = Hex$(BufferIn(2))
strSerialNumLSB = Hex$(BufferIn(1))
strSerialNum = strSerialNumMSB & strSerialNumLSB
intSerialNum = CLng("&H" & strSerialNum)
lSerialNum.Caption = intSerialNum

jellis00
- 4th June 2010, 03:34
The problem was not being caused by the buffer transfers at all. It turned out it was caused by how I read the buffers and converted the data on the VB6 side of the interface for display. First of all the BufferIn(2) on the PC side of the USB interface is actually the BufferOut(1) on the PIC side of the interface. That was part of the problem. Then it required a specific type conversion process to get it right. For anyone who might read this thread and want to know here is how the DeviceID1 and DeviceID2 values in BufferIn(2) and BufferIn(3) on the PC side mujst be handled in VB6:



If hidRead(pHandle, BufferIn(0)) Then ' valid ReportID?
' first byte is the report ID, e.g. BufferIn(0)
' the other bytes are the data payload from the USB PIC
If BufferIn(15) = 0 And m_blnFlagRX = True Then ' There is incoming data set from PIC
'\\ Read & display inbound data from USB PIC

' NOTE: BufferIn(1) here = USBBuffer(0) from PIC...VERIFIED TRUE FOR VB6!
'Response = Chr$(BufferIn(1))

'\\ Display DeviceID as Serial Number
lSerialNum.Caption = BufferIn(2) * 256& & BufferIn(3)
'intSerialNum = CInt(BufferIn(2) * 256& & BufferIn(3))


Hope this might help someone.