General discussion relating to the library modules supplied with the compiler
Moderators: David Barker, Jerry Messina
-
itheo92
- Posts: 3
- Joined: Sun Apr 24, 2011 9:03 am
Post
by itheo92 » Tue Apr 26, 2011 9:02 am
Hi, this is my first post to this forum after using swordfish for more than two years.
I'm using the HID module. I can send a number of bytes through WriteArray()
and read it back through a host application compiled in VB.Net. Just for an example:
Code: Select all
For Sample_C = 0 To 63
outData(Sample_C) = 0
Next
outData(1) = 1
outData(2) = 5
outData(3) = 10
outData(4) = 15
outData(5) = 0
outData(6) = 30
outData(7) = 100
outData(8) = 199
outData(9) = 215
outData(10)= 1
WriteArray(outData)
The Host code snippet:
Code: Select all
For count = 0 To BufferInSize
txtDataIn.Text = txtDataIn.Text & (BufferIn(count)) & ""
Next
BufferInSize is a array if 64 bytes. The output is:
But it's not a good way to send some word or strings, I want to send some floating point (or integer) data. How to read This report? Sending report is easy:
Code: Select all
txreport.vdata = 1000 'Vdata as integer
txreport.idata = 1500 'Idata too as integr
writereport
In the same host code I'm getting:
Code: Select all
023232205247126510825410959142255199185251223792556125525562193211230255190237115213250127239124223108246452438785183233731022132295239216171932235159255108227239236185252247
-
gramo
- Registered User
- Posts: 200
- Joined: Tue Mar 20, 2007 6:55 am
- Location: Australia
-
Contact:
Post
by gramo » Tue Apr 26, 2011 9:37 am
Slave to Host is not much of an issue for objects larger than byte size, I've found it takes some sideways thinking on the other end to "rebuild" the objects.
dmtulsa at Digital DIY has made a very handy DLL which can be used in Visual Studio to help break the 8-bit wall
http://digital-diy.com/forum/visual-bas ... t1067.html
Some of his notes in the topic:
Two bytes too one integer
Declare Function Bytes1Int Lib "picdll.DLL" Alias "BYTES1INT" (lb As Byte, hb As Byte) As Integer
Purpose: Take two bytes and combine to produce a single signed 16-bit value.
Two bytes too one Long
Declare Function Byte1Lng Lib "picdll.DLL" Alias "BYTE1LNG" (lb As Byte, hb As Byte) As Long
Purpose: Take two bytes and combine to produce a single unsigned 16-bit value. In VB there are no unsigned integers (WORDS) so a long integer is returned.
Get the High byte of an integer
Declare Function InthByte Lib "picdll.DLL" Alias "INTHBYTE" (value As Integer) As Byte
Purpose Extract the most significant (high-order) byte from an Integer or Word value, and return it as an unsigned Byte value.
Get the Low byte of an integer
Declare Function IntLByte Lib "picdll.DLL" Alias "INTLBYTE" (value As Integer) As Byte
Purpose Extract the least significant (low-order) byte from an Integer or Word value, and return it as an unsigned Byte value.
Get the High integer form a long
Declare Function lng2Hint Lib "picdll.DLL" Alias "LNG2HINT" (value As Long) As Integer
Purpose Extract the most significant (high-order) integer from a Long-integer value, and return it as a signed Integer value.
Get the Low integer form a long
Declare Function lng2Lint Lib "picdll.DLL" Alias "LNG2LINT" (value As Long) As Integer
Purpose Extract the least significant (low-order) integer from a Long-integer value, and return it as a signed Integer value.
Two integers too long
Declare Function Makelong Lib "picdll.DLL" Alias "MAKELONG" (lint As long, hint As long) As Long
Purpose Take the low-order 16-bits from each of two integer-class variables and combine to produce a single 32-bit value.
Long too Binary string
'Declare Function retBin Lib "picdll.DLL" Alias "RETBIN" (ByRef val As Long, digits As Integer) As String
Purpose Return a string that is the binary (base 2) representation of its argument.
Remarks numeric_expression must be in the range -2,147,483,648 to +2,147,483,647. Any fractional part of numeric_expression is rounded before the string is created. If digits is specified, then the resulting string will be of length digits. If the resulting string is longer than digits, the result will be truncated from the left as needed. If the resulting string is shorter than digits, then leading zeros will be added to the left to make the final string digits long.
Get bit value form any position in a integer
Declare Function GetBit Lib "picdll.DLL" Alias "GETBIT" (val As Integer, pos As Integer) As Integer
Pos is the position of the bit 0-15
Purpose Return the value of a particular bit in an integer-class variable.
Set bit value form any position in a integer (makes bit a 1)
Declare Function SetBit Lib "picdll.DLL" Alias "SETBIT" (val As Integer, pos As Integer) As Integer
Pos is the position of the bit 0-15
Purpose Manipulate individual bits of an integer-class variable (or in an implied bit-array), for storing values such as TRUE/FALSE (flag) settings quickly and efficiently.
-
Jerry Messina
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Post
by Jerry Messina » Tue Apr 26, 2011 10:53 am
That dll makes a lot of assumptions.
The problem with transferring anything but single 8-bit unsigned bytes between two systems is that the data representation may not be the same on both ends, and it's machine and language dependant.
What's an 'integer' on your host system? 16 bits? 32 bits? Is it stored little-endian or big-endian?
Floating point is even worse. I doubt that you'll ever get the two ends to match up directly without a lot of bit manipulation.
If you want both end to agree, send the data in a format-neutral form, such as ASCII text with delimiters between the fields. It's a lot more work on both sides, but it usually always works.
-
itheo92
- Posts: 3
- Joined: Sun Apr 24, 2011 9:03 am
Post
by itheo92 » Wed Apr 27, 2011 9:15 am
Thanks. I'll try it and let you know the results.
And I really dont know how many times I did thanked digital-diy, formerly Spency's PICBasic, I learned PIC Programming from it through Copy-Paste, then Copy-Improve-Paste and now I can write my own codes. This is my research project on blackbody radiation. I'll post details of it at the end.
In host system, int32 is 32 bit signed 'integer' data type. int16 and Uint (unsigned) is also available.