I've stumbled across a problem way beyond my area of expertise, and I don't have a mentor to turn to for help with this.
I have a receipt printer I need to interface with through an iOS app. The printer is located on the same network as the device(s), so I can address it through supported "Line Mode commands"
What I'd like to do is keep the code I have already that works cross-platform – i.e. it's a UIView/NSView, and if you're not familiar with OS X/iOS, it's just a standard vanilla view that I can render into PDF/PNG formats. Thankfully, the printer has a "raster graphics" mode that seems to be what I need.
Unfortunately, be it the broken English of the command spec, or my complete lack of knowledge of anything beyond basic C, or my complete lack of knowledge regarding graphics, I have no idea how to even get started from the command specifications I have. I know the printer and my networking works because I can address it over the network and send it basic feed commands. But, I have no idea how to go from a PNG -> whatever the printer needs to make it's 'raster mode' work.
The specification is available at http://www.star-m.jp/eng/service/usermanual/linemode_cm_en.pdf , and the page you'd want to start reading it if you want to help is 3-68, and the specific commands I'm having trouble even getting started with are on 3-78/3-79.
I can give you nothing but a checkmark but I assure you, you'll have my undying gratitude if you can even provide me even a point in the right direction.
Having written a few printer drivers I can confirm that generally the documentation is confusing because of the way printers work. The document that you refer to doesn't actually seem to bad to me.
I think you're right to be printing in raster mode and that overall this is going to give the best results.
From the Star documentation I reckon you'll need to send :
1. \x1b*rR Initialize raster mode
2. \x1b*rA Enter raster mode
3. \x1b*rC Clear raster data
4. \x1b*rml
4. b\x##\x##\xAA\xAA\xAA....<DATA>..........
5. \x1b\x0C\x00 Raster Form feed(??) - should spit out the data.
6. \x1b*rB Clear raster data
Obv. in the above \x1b is the C encoding of ESC (i.e. character 27 0x1b).
From all of the documentation that I've been reading the following is how the images should be formatted in raster mode. When in line mode it is completely different as the vertical & horizontal are swapped. From THERMAL PRINTER PROGRAMMER'S MANUAL (TSP552,TSP552II,TSP2000)
This equates to the following bytes stream.
On the 4th command line it is effectively 'b' followed by two bytes definining the size. This size is computed as the number of pixels contained in the stream % 256 and / 256. So for 320x1 that'd 0x40,0x01
So, taking the above and plugging it into a simple test program you should test with this:
char rasterImage [] = {
0x1b, '*', 'r', 'R', // Initialize raster mode
0x1b, '*', 'r', 'A', // Enter raster mode
0x1b, '*', 'r', 'C', // Clear raster data
// n1 n2 d1 d2..
0x1b, 'b', 0x2, 0, 0x00, 0x00, // data
0x1b, 'b', 0x2, 0, 0x1F, 0xF8,
0x1b, 'b', 0x2, 0, 0x3F, 0xFC,
0x1b, 'b', 0x2, 0, 0x77, 0xEE,
0x1b, 'b', 0x2, 0, 0xF8, 0x1F,
0x1b, 'b', 0x2, 0, 0xF8, 0x1F,
0x1b, 'b', 0x2, 0, 0xF8, 0x1F,
0x1b, 'b', 0x2, 0, 0x0F, 0xF0,
0x1b, 'b', 0x2, 0, 0x1F, 0xF8,
0x1b, 'b', 0x2, 0, 0x1F, 0xF8,
0x1b, 'b', 0x2, 0, 0x3E, 0x7C,
0x1b, 'b', 0x2, 0, 0x38, 0x1C,
0x1b, 'b', 0x2, 0, 0x79, 0x9E,
0x1b, 'b', 0x2, 0, 0x73, 0xCE,
0x1b, 'b', 0x2, 0, 0x73, 0xCE,
0x1b, 'b', 0x2, 0, 0xF9, 0x9F,
0x1b, 'b', 0x2, 0, 0xF8, 0x1F,
0x1b, 'b', 0x2, 0, 0xFE, 0x7F,
0x1b, 'b', 0x2, 0, 0xFF, 0xFF,
0x1b, 'b', 0x2, 0, 0xFF, 0xFF,
0x1b, 'b', 0x2, 0, 0x00, 0x00,
0x1b, 'b', 0x2, 0, 0x00, 0x00,
0x1b, 'b', 0x2, 0, 0x00, 0x00,
0x1b, 'b', 0x2, 0, 0x00, 0x00};
[self.currentDataBeingSent appendBytes:rasterImage length:sizeof(rasterImage)];
Simply squirt that out to the printer and you should get a picture as above. This is where you can easily tweak and play about with the exact commands to get something that's working. Often this is the only way I've ever managed to figure out what should be done.
rev.3
Ref. comments.
If you have a byte per pixel then you will need to merge these into a series of bits; the following should do the job based on your pastebin code. I've also changed the char* to be unsigned as it is signed can cause problems when bit manipulating.
NSUInteger bitmapBytePerRow = width/8;
NSUInteger bytesPerRow = 3 + bitmapBytePerRow;
[self.currentDataBeingSent = [NSMutableData dataWithLength:bytesPerRow * height];
[self.currentDataBeingSent appendBytes:initializeRaster length:sizeof(initializeRaster)];
[self.currentDataBeingSent appendBytes:enterRaster length:sizeof(enterRaster)];
NSUInteger byteOffset = 0;
for (NSUInteger y = 0; y < height; y++)
{
unsigned char *rasterCommandForRow = (unsigned char *)calloc(bytesPerRow, sizeof(char));
unsigned char *current_raster = rasterCommandForRow;
*current_raster++ = '\x6B';
*current_raster++ = (width*height) % 256;
*current_raster++ = (width*height) / 256;
unsigned char mask = '\x80' ;
unsigned char out = 0 ;
for (NSUInteger x = 0; x < width; x++)
{
if (*(data + (byteOffset * sizeof(char))))
out |= mask ;
byteOffset++;
mask >>= 1 ;
if( 0 == mask )
{
mask = '\x80' ;
*current_raster++ = out ;
if( out )
lastDot = nextOut ;
out = 0 ;
}
}
// handle partially finished byte .
if( ( '\x80' != mask ) && ( 0 != out ) )
*current_raster++ = out ;
[self.currentDataBeingSent appendBytes:rasterCommandForRow length:bytesPerRow];
}
rev.3a
Looking at the Mac CUPS support from Star it's got the source code for the driver which contains a lot of clues about how this should be done. Sometimes code is so much easier to read than documentation.
starcupsdrv-3.1.1_mac_20100423.zip\starcupsdrv-3.1.1_mac\SourceCode\
contains starcupsdrv-src-3.1.1.tar.gz\ sub folder starcupsdrv\src\
View rastertostar.c, the important bit is the calculation of the n1 / n2 values. These aren't at all X & Y but based on the pixel count, lastBlackPixel is the count of pixels from the source.
putchar('b');
putchar((char) ((lastBlackPixel > 0)?(lastBlackPixel % 256):1));
putchar((char) (lastBlackPixel / 256));
I've modified the code above to include the fixes, hopefully that'll be closer. If not post a scan of what comes out of the printer, it will be useful to diagnose what's happening.
For reference The code between 580:650 from jsStarUSB.cpp seems to me to be along the lines of what you need to produce a buffer (stored in nextOut) that contains the raster data in the format to be sent directly to the printer.
rev. 4 (2023)
Joshua May advises in the comments that rasterImage doesn't need the ESC (0x1b) on each row - only the 'b' and that by just removing the 0x1b "worked for me".
I have a hunch this might be the same as the old Seiko printers, only yours is network enabled. If so, have a look at the C code here. It tries to output to a serial port /dev/cua, where it thinks the printer is.
But if the commands are the same, the code should help you. It takes as input the Portable Bitmap Format, which is plain ASCII text.
But I don't know. Microsoft indicates Star Micronics works the same as Epson LQ, in which case there is ample documentation.
Related links:
ESC/POS PDF Documentation, hundreds of pages
Command codes from the STAR website
Update! ;-) Try this, totally untested code:
/* Call with grayscale images of height 256, width 256. */
- (void) outputraster(char* pixels, int rows)
{
const char initializeRaster[] = "\x1B\x2A\x72\x52";
const char enterRaster[] = "\x1B\x2A\x72\x41";
const char formFeed[] = "\x1B\x0C\x00";
const char clearRaster[] = "\x1B\x2A\x72\x43";
const char exitRaster[] = "\x1B\x2A\x72\x42";
/* The FF means 255 lines: */
char setRasterPageLength[] "\x1B\x2A\x72\x50\xFF\x0";
/* The FF FF means 256 lines and 256 rows: */
char sendRasterData[] = "\x62\xFF\xFF";
[self sendBytes:initializeRaster ofLength:sizeof(initializeRaster)];
[self sendBytes:enterRaster ofLength:sizeof(enterRaster)];
[self sendBytes:clearRaster ofLength:sizeof(clearRaster)];
[self sendBytes:setRasterPageLength ofLength:sizeof(setRasterPageLength)];
[self sendBytes:sendRasterData ofLength:sizeof(sendRasterData)];
while (rows)
{
for (int x = 0; x < 255; x++)
{
[self sendBytes:pixels[x] ofLength:256];
}
rows --;
}
}
Update!
I was looking at the docs at night, and stumbled upon how you can print out a prestored logo. Then I looked at how to define that logo, and that part of the documentation looked a lot more thorough:
Explanations of bitmap format for a similar printer:
Also, look at pages 34 and on for an explanation of the bitmap format of a Star printer.
I hope this helps someone, but I was trying to use the code from Richard Harrison above to print the phone raster in Python...
I on my system I can run python3 raster.py > /dev/usb/lp0 and I get the expected output!! Hopefully it shows what you need. I cross refrenced the bytes of a file that would print, and also the graphical mode manual.
import sys
buf = [
0x1b, ord('*'), ord('r'), ord('A'), # enter raster mode
0x1b, ord('*'), ord('r'), ord('P'), ord('0'), 0x00, # continuous mode
ord('b'), 0x2, 0, 0x00, 0x00,
ord('b'), 0x2, 0, 0x1F, 0xF8,
ord('b'), 0x2, 0, 0x3F, 0xFC,
ord('b'), 0x2, 0, 0x77, 0xEE,
ord('b'), 0x2, 0, 0xF8, 0x1F,
ord('b'), 0x2, 0, 0xF8, 0x1F,
ord('b'), 0x2, 0, 0xF8, 0x1F,
ord('b'), 0x2, 0, 0x0F, 0xF0,
ord('b'), 0x2, 0, 0x1F, 0xF8,
ord('b'), 0x2, 0, 0x1F, 0xF8,
ord('b'), 0x2, 0, 0x3E, 0x7C,
ord('b'), 0x2, 0, 0x38, 0x1C,
ord('b'), 0x2, 0, 0x79, 0x9E,
ord('b'), 0x2, 0, 0x73, 0xCE,
ord('b'), 0x2, 0, 0x73, 0xCE,
ord('b'), 0x2, 0, 0xF9, 0x9F,
ord('b'), 0x2, 0, 0xF8, 0x1F,
ord('b'), 0x2, 0, 0xFE, 0x7F,
ord('b'), 0x2, 0, 0xFF, 0xFF,
ord('b'), 0x2, 0, 0xFF, 0xFF,
ord('b'), 0x2, 0, 0x00, 0x00,
ord('b'), 0x2, 0, 0x00, 0x00,
ord('b'), 0x2, 0, 0x00, 0x00,
ord('b'), 0x2, 0, 0x00, 0x00,
0x1b, ord('*'), ord('r'), ord('b') # end raster mode
]
blob = bytearray(buf)
sys.stdout.buffer.write(blob)
EDIT: Off the back of this, I created a Python library that will take an image and convert it into the required raster commands... https://pypi.org/project/StarTSPImage/
Related
I am trying to inflate an object stream in a pdf. While inflating one of the object stream which is compressed with flate decode I am getting data error.
Object stream
1339 0 obj^M<</First 7/Length 36/Filter/FlateDecode/N 1/Type/ObjStm>>stream^M
^ZÖ¬Áã9^<87>^GÎ<89><93>^WÊ<8d>ð<93>yM<96>t0Ò=$Ô<·×^L<84>°¯$<8b>^M
endstream
**Encryption object **
1698 0 obj^M<</Length 128/Filter/Standard/O(¡"ÎÙ¢¬<9d><9c>"\r^S^LÀõeâJý;Î{dTÔÜ<9d>~´>^Tº)/P -1324/R 3/U(É^[®ú^F^C /¬®=ÌæZv5');\r\n view( a)/V 2>>^Mendobj
After decrypting I got this data
{0x68, 0x16, 0x48, 0x59, 0x11, 0xef, 0x34, 0xe9, 0xd0, 0x79, 0xf6, 0xcb, 0x73, 0xf6, 0x8a, 0x50, 0xed, 0xdc, 0x34, 0xe9, 0x60, 0xb1, 0xbf, 0x3, 0xf5, 0x92, 0x5, 0x67, 0x60, 0xd9, 0x84, 0x34, 0x44, 0xad, 0xa9, 0x5c}
Now I have to uncompress this data.
I am getting data error after calling inflate(&zstrm, Z_FINISH);
I am stuck here. Can anyone please help me to understand this error.
Ans :
Here the input I was supplying to zlib was not a valid inflate data. When the input stream was encrypted the first character was new line character. I was skipping that newline character. That was the error.
That's not a zlib stream, nor is it even raw deflate data.
The USB HID spec states
Each top level collection must be an application collection and reports may not
span more than one top level collection.
However, one of the examples provided in the USB HID report descriptor tool, namely the display example, has a top-level collection of type logical with no application collection:
0x05, 0x14, // USAGE_PAGE (Alphnumeric Display)
0x09, 0x01, // USAGE (Alphanumeric Display)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0xa1, 0x02, // COLLECTION (Logical)
0x09, 0x20, // USAGE (Display Attributes Report)
0xa1, 0x02, // COLLECTION (Logical)
0x09, 0x35, // USAGE (Rows)
0x09, 0x36, // USAGE (Columns)
0x09, 0x3d, // USAGE (Character Width)
0x09, 0x3e, // USAGE (Character Height)
0x85, 0x01, // REPORT_ID (1)
0x25, 0x1f, // LOGICAL_MAXIMUM (31)
0x75, 0x05, // REPORT_SIZE (5)
0x95, 0x04, // REPORT_COUNT (4)
0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x03, // REPORT_COUNT (3)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x09, 0x21, // USAGE (ASCII Character Set)
0x09, 0x22, // USAGE (Data Read Back)
0x09, 0x29, // USAGE (Vertical Scroll)
0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
0x95, 0x03, // REPORT_COUNT (3)
0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
0xc0, // END_COLLECTION
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x01, // REPORT_COUNT (1)
0x25, 0x02, // LOGICAL_MAXIMUM (2)
0x09, 0x2d, // USAGE (Display Status)
0xa1, 0x02, // COLLECTION (Logical)
0x09, 0x2e, // USAGE (Stat Not Ready)
0x09, 0x2f, // USAGE (Stat Ready)
0x09, 0x30, // USAGE (Err Not a loadable character)
0x81, 0x40, // INPUT (Data,Ary,Abs,Null)
0xc0, // END_COLLECTION
0x09, 0x32, // USAGE (Cursor Position Report)
0xa1, 0x02, // COLLECTION (Logical)
0x85, 0x02, // REPORT_ID (2)
0x75, 0x04, // REPORT_SIZE (4)
0x95, 0x01, // REPORT_COUNT (1)
0x25, 0x0f, // LOGICAL_MAXIMUM (15)
0x09, 0x34, // USAGE (Column)
0xb1, 0x22, // FEATURE (Data,Var,Abs,NPrf)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x09, 0x33, // USAGE (Row)
0xb1, 0x22, // FEATURE (Data,Var,Abs,NPrf)
0xc0, // END_COLLECTION
0x09, 0x2b, // USAGE (Character Report)
0xa1, 0x02, // COLLECTION (Logical)
0x85, 0x03, // REPORT_ID (3)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x04, // REPORT_COUNT (4)
0x25, 0x7e, // LOGICAL_MAXIMUM (126)
0x09, 0x2c, // USAGE (Display Data)
0xb2, 0x02, 0x01, // FEATURE (Data,Var,Abs,Buf)
0xc0, // END_COLLECTION
0x85, 0x04, // REPORT_ID (4)
0x09, 0x3b, // USAGE (Font Report)
0xa1, 0x02, // COLLECTION (Logical)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x7e, // LOGICAL_MAXIMUM (126)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x01, // REPORT_COUNT (1)
0x09, 0x2c, // USAGE (Display Data)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
0x95, 0x05, // REPORT_COUNT (5)
0x09, 0x3c, // USAGE (Font Data)
0x92, 0x02, 0x01, // OUTPUT (Data,Var,Abs,Buf)
0xc0, // END_COLLECTION
0xc0, // END_COLLECTION
Is this allowed?
No, it is not allowed.
The example given seems to be quite similar to the example in "Appendix A.8 A Device with a Display" in the HID Usage Tables v1.12 specification which does have a top level application collection defined.
I'd say the example you posted may have some transcription errors - especially since the top level Usage selected (0x00140001) has Usage Type of CA and so is intended to be used on an Application Collection item rather than a Logical Collection (see page 109 of the Usage Tables document).
The first logical collection should be changed to:
0xa1, 0x01, // COLLECTION (Application)
It will then be parsed (albeit with bit alignment issues due to sections having been omitted from the original example) as:
//--------------------------------------------------------------------------------
// Decoded Application Collection
//--------------------------------------------------------------------------------
/*
05 14 (GLOBAL) USAGE_PAGE 0x0014 Alphanumeric Display Page
09 01 (LOCAL) USAGE 0x00140001 Alphanumeric Display (CA=Application Collection)
15 00 (GLOBAL) LOGICAL_MINIMUM 0x00 (0) <-- Redundant: LOGICAL_MINIMUM is already 0 <-- Info: Consider replacing 15 00 with 14
A1 01 (MAIN) COLLECTION 0x00000001 Application (Usage=0x00140001: Page=Alphanumeric Display Page, Usage=Alphanumeric Display, Type=CA)
09 20 (LOCAL) USAGE 0x00140020 Display Attributes Report (CL=Logical Collection)
A1 02 (MAIN) COLLECTION 0x00000002 Logical (Usage=0x00140020: Page=Alphanumeric Display Page, Usage=Display Attributes Report, Type=CL)
09 35 (LOCAL) USAGE 0x00140035 Rows (SV=Static Value)
09 36 (LOCAL) USAGE 0x00140036 Columns (SV=Static Value)
09 3D (LOCAL) USAGE 0x0014003D Character Width (SV=Static Value)
09 3E (LOCAL) USAGE 0x0014003E Character Height (SV=Static Value)
85 01 (GLOBAL) REPORT_ID 0x01 (1)
25 1F (GLOBAL) LOGICAL_MAXIMUM 0x1F (31)
75 05 (GLOBAL) REPORT_SIZE 0x05 (5) Number of bits per field
95 04 (GLOBAL) REPORT_COUNT 0x04 (4) Number of fields
B1 03 (MAIN) FEATURE 0x00000003 (4 fields x 5 bits) 1=Constant 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap
75 01 (GLOBAL) REPORT_SIZE 0x01 (1) Number of bits per field
95 03 (GLOBAL) REPORT_COUNT 0x03 (3) Number of fields
25 01 (GLOBAL) LOGICAL_MAXIMUM 0x01 (1)
09 21 (LOCAL) USAGE 0x00140021 ASCII Character Set (SF=Static Flag)
09 22 (LOCAL) USAGE 0x00140022 Data Read Back (SF=Static Flag)
09 29 (LOCAL) USAGE 0x00140029 Vertical Scroll (SFDF=Static Flag or Dynamic Flag)
B1 03 (MAIN) FEATURE 0x00000003 (3 fields x 1 bit) 1=Constant 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap
95 03 (GLOBAL) REPORT_COUNT 0x03 (3) Number of fields <-- Redundant: REPORT_COUNT is already 3
B1 03 (MAIN) FEATURE 0x00000003 (3 fields x 1 bit) 1=Constant 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap
C0 (MAIN) END_COLLECTION Logical
75 08 (GLOBAL) REPORT_SIZE 0x08 (8) Number of bits per field
95 01 (GLOBAL) REPORT_COUNT 0x01 (1) Number of fields
25 02 (GLOBAL) LOGICAL_MAXIMUM 0x02 (2)
09 2D (LOCAL) USAGE 0x0014002D Display Status (CL=Logical Collection)
A1 02 (MAIN) COLLECTION 0x00000002 Logical (Usage=0x0014002D: Page=Alphanumeric Display Page, Usage=Display Status, Type=CL)
09 2E (LOCAL) USAGE 0x0014002E Stat Not Ready (Sel=Selector)
09 2F (LOCAL) USAGE 0x0014002F Stat Ready (Sel=Selector)
09 30 (LOCAL) USAGE 0x00140030 Err Not a loadable character (Sel=Selector)
81 40 (MAIN) INPUT 0x00000040 (1 field x 8 bits) 0=Data 0=Array 0=Absolute
C0 (MAIN) END_COLLECTION Logical
09 32 (LOCAL) USAGE 0x00140032 Cursor Position Report (CL=Logical Collection)
A1 02 (MAIN) COLLECTION 0x00000002 Logical (Usage=0x00140032: Page=Alphanumeric Display Page, Usage=Cursor Position Report, Type=CL)
85 02 (GLOBAL) REPORT_ID 0x02 (2)
75 04 (GLOBAL) REPORT_SIZE 0x04 (4) Number of bits per field
95 01 (GLOBAL) REPORT_COUNT 0x01 (1) Number of fields <-- Redundant: REPORT_COUNT is already 1
25 0F (GLOBAL) LOGICAL_MAXIMUM 0x0F (15)
09 34 (LOCAL) USAGE 0x00140034 Column (DV=Dynamic Value)
B1 22 (MAIN) FEATURE 0x00000022 (1 field x 4 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 1=NoPrefState 0=NoNull 0=NonVolatile 0=Bitmap
25 01 (GLOBAL) LOGICAL_MAXIMUM 0x01 (1)
09 33 (LOCAL) USAGE 0x00140033 Row (DV=Dynamic Value)
B1 22 (MAIN) FEATURE 0x00000022 (1 field x 4 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 1=NoPrefState 0=NoNull 0=NonVolatile 0=Bitmap
C0 (MAIN) END_COLLECTION Logical
09 2B (LOCAL) USAGE 0x0014002B Character Report (CL=Logical Collection)
A1 02 (MAIN) COLLECTION 0x00000002 Logical (Usage=0x0014002B: Page=Alphanumeric Display Page, Usage=Character Report, Type=CL)
85 03 (GLOBAL) REPORT_ID 0x03 (3)
75 08 (GLOBAL) REPORT_SIZE 0x08 (8) Number of bits per field
95 04 (GLOBAL) REPORT_COUNT 0x04 (4) Number of fields
25 7E (GLOBAL) LOGICAL_MAXIMUM 0x7E (126)
09 2C (LOCAL) USAGE 0x0014002C Display Data (DV=Dynamic Value)
B2 0201 (MAIN) FEATURE 0x00000102 (4 fields x 8 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 1=Buffer
C0 (MAIN) END_COLLECTION Logical
85 04 (GLOBAL) REPORT_ID 0x04 (4)
09 3B (LOCAL) USAGE 0x0014003B Font Report (CL=Logical Collection)
A1 02 (MAIN) COLLECTION 0x00000002 Logical (Usage=0x0014003B: Page=Alphanumeric Display Page, Usage=Font Report, Type=CL)
15 00 (GLOBAL) LOGICAL_MINIMUM 0x00 (0) <-- Redundant: LOGICAL_MINIMUM is already 0 <-- Info: Consider replacing 15 00 with 14
25 7E (GLOBAL) LOGICAL_MAXIMUM 0x7E (126) <-- Redundant: LOGICAL_MAXIMUM is already 126
75 08 (GLOBAL) REPORT_SIZE 0x08 (8) Number of bits per field <-- Redundant: REPORT_SIZE is already 8
95 01 (GLOBAL) REPORT_COUNT 0x01 (1) Number of fields
09 2C (LOCAL) USAGE 0x0014002C Display Data (DV=Dynamic Value)
91 02 (MAIN) OUTPUT 0x00000002 (1 field x 8 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap
95 05 (GLOBAL) REPORT_COUNT 0x05 (5) Number of fields
09 3C (LOCAL) USAGE 0x0014003C Font Data (BB=Buffered Bytes)
92 0201 (MAIN) OUTPUT 0x00000102 (5 fields x 8 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 1=Buffer
C0 (MAIN) END_COLLECTION Logical
C0 (MAIN) END_COLLECTION Application
*/
//--------------------------------------------------------------------------------
// Alphanumeric Display Page featureReport 01 (Device <-> Host)
//--------------------------------------------------------------------------------
typedef struct
{
uint8_t reportId; // Report ID = 0x01 (1)
// Collection: AlphanumericDisplay DisplayAttributesReport
uint8_t AD_AlphanumericDisplayDisplayAttributesReportRows : 5; // Usage 0x00140035: Rows, Value = 0 to 31
uint8_t AD_AlphanumericDisplayDisplayAttributesReportColumns : 5; // Usage 0x00140036: Columns, Value = 0 to 31
uint8_t AD_AlphanumericDisplayDisplayAttributesReportCharacterWidth : 5; // Usage 0x0014003D: Character Width, Value = 0 to 31
uint8_t AD_AlphanumericDisplayDisplayAttributesReportCharacterHeight : 5; // Usage 0x0014003E: Character Height, Value = 0 to 31
uint8_t AD_AlphanumericDisplayDisplayAttributesReportAsciiCharacterSet : 1; // Usage 0x00140021: ASCII Character Set, Value = 0 to 1
uint8_t AD_AlphanumericDisplayDisplayAttributesReportDataReadBack : 1; // Usage 0x00140022: Data Read Back, Value = 0 to 1
uint8_t AD_AlphanumericDisplayDisplayAttributesReportVerticalScroll : 1; // Usage 0x00140029: Vertical Scroll, Value = 0 to 1
uint8_t : 1; // Pad
uint8_t : 1; // Pad
uint8_t : 1; // Pad
} featureReport01_t;
//--------------------------------------------------------------------------------
// Alphanumeric Display Page featureReport 02 (Device <-> Host)
//--------------------------------------------------------------------------------
typedef struct
{
uint8_t reportId; // Report ID = 0x02 (2)
// Collection: AlphanumericDisplay CursorPositionReport
uint8_t AD_AlphanumericDisplayCursorPositionReportColumn : 4; // Usage 0x00140034: Column, Value = 0 to 15
uint8_t AD_AlphanumericDisplayCursorPositionReportRow : 4; // Usage 0x00140033: Row, Value = 0 to 1
} featureReport02_t;
//--------------------------------------------------------------------------------
// Alphanumeric Display Page featureReport 03 (Device <-> Host)
//--------------------------------------------------------------------------------
typedef struct
{
uint8_t reportId; // Report ID = 0x03 (3)
// Collection: AlphanumericDisplay CharacterReport
uint8_t AD_AlphanumericDisplayCharacterReportDisplayData[4]; // Usage 0x0014002C: Display Data, Value = 0 to 126
} featureReport03_t;
//--------------------------------------------------------------------------------
// Alphanumeric Display Page inputReport 01 (Device --> Host)
//--------------------------------------------------------------------------------
typedef struct
{
uint8_t reportId; // Report ID = 0x01 (1)
// Collection: AlphanumericDisplay DisplayStatus
uint8_t AD_AlphanumericDisplayDisplayStatus; // Value = 0 to 2
// Value 0 = Usage 0x0014002E: Stat Not Ready
// Value 1 = Usage 0x0014002F: Stat Ready
// Value 2 = Usage 0x00140030: Err Not a loadable character
} inputReport01_t;
//--------------------------------------------------------------------------------
// Alphanumeric Display Page outputReport 04 (Device <-- Host)
//--------------------------------------------------------------------------------
typedef struct
{
uint8_t reportId; // Report ID = 0x04 (4)
// Collection: AlphanumericDisplay FontReport
uint8_t AD_AlphanumericDisplayFontReportDisplayData; // Usage 0x0014002C: Display Data, Value = 0 to 126
uint8_t AD_AlphanumericDisplayFontReportFontData[5]; // Usage 0x0014003C: Font Data, Value = 0 to 126
} outputReport04_t;
I simply want to send from device to host with two report ID.These reports must have different Report Count(First report id has 4 report count, second report id has 40).This is what I have done so far:
//14 bytes
0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1)
0x09, 0x01, // USAGE (Vendor Usage 1)
0xa1, 0x01, // COLLECTION (Application)
// -------- common global items ---------
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x75, 0x08, // REPORT_SIZE (8)
// 10 bytes | Input message 1 (sent from device to host)
0x85, 5, // Global Report ID (cannot be 0)
0x95, 4, // Global Report Count (number of Report Size fields)
0x19, 0x01, // USAGE_MINIMUM (Vendor Usage 1)
0x29, 5, // USAGE_MAXIMUM (Vendor Usage 64)
0x81, 0x02, // Main Input (data, array, absolute)
// 10 bytes | Input message 1 (sent from device to host)
0x85, 6, // Global Report ID (cannot be 0)
0x95, 40, // Global Report Count (number of Report Size fields)
0x19, 0x01, // USAGE_MINIMUM (Vendor Usage 1)
0x29, 41, // USAGE_MAXIMUM (Vendor Usage 64)
0x81, 0x02, // Main Input (data, array, absolute)
0xC0
But first report id is sending 40 bayt.Where is my mistake?
HID Terminal output:
R 02 0C 16 20 2A 34 3E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
R 01 0B 15 1F 29 34 3E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I believe #Nipo has given you the correct answer: the report descriptor indicates what each report should look like, but it is still the responsibility of your code to send reports with the correct length specified.
For report id 5 that would be 5 (1 for the report id + 4 for the payload), and
for report it 6 it would be 41 (1 for the report id + 40 for the payload).
BTW, your report descriptor may need a little tweaking. As it stands, it decodes as:
//--------------------------------------------------------------------------------
// Decoded Application Collection
//--------------------------------------------------------------------------------
PROGMEM char usbHidReportDescriptor[] =
{
0x06, 0x00, 0xFF, // (GLOBAL) USAGE_PAGE 0xFF00 Vendor-defined
0x09, 0x01, // (LOCAL) USAGE 0xFF000001 <-- Warning: Undocumented usage
0xA1, 0x01, // (MAIN) COLLECTION 0x00000001 Application (Usage=0xFF000001: Page=Vendor-defined, Usage=, Type=)
0x15, 0x00, // (GLOBAL) LOGICAL_MINIMUM 0x00 (0) <-- Redundant: LOGICAL_MINIMUM is already 0 <-- Info: Consider replacing 15 00 with 14
0x26, 0xFF, 0x00, // (GLOBAL) LOGICAL_MAXIMUM 0x00FF (255)
0x75, 0x08, // (GLOBAL) REPORT_SIZE 0x08 (8) Number of bits per field
0x85, 0x05, // (GLOBAL) REPORT_ID 0x05 (5)
0x95, 0x04, // (GLOBAL) REPORT_COUNT 0x04 (4) Number of fields
0x19, 0x01, // (LOCAL) USAGE_MINIMUM 0xFF000001 <-- Warning: Undocumented usage
0x29, 0x05, // (LOCAL) USAGE_MAXIMUM 0xFF000005 <-- Warning: Undocumented usage
0x81, 0x02, // (MAIN) INPUT 0x00000002 (4 fields x 8 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap
0x85, 0x06, // (GLOBAL) REPORT_ID 0x06 (6)
0x95, 0x28, // (GLOBAL) REPORT_COUNT 0x28 (40) Number of fields
0x19, 0x01, // (LOCAL) USAGE_MINIMUM 0xFF000001 <-- Warning: Undocumented usage
0x29, 0x29, // (LOCAL) USAGE_MAXIMUM 0xFF000029 <-- Warning: Undocumented usage
0x81, 0x02, // (MAIN) INPUT 0x00000002 (40 fields x 8 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap
0xC0, // (MAIN) END_COLLECTION Application
};
//--------------------------------------------------------------------------------
// Vendor-defined inputReport 05 (Device --> Host)
//--------------------------------------------------------------------------------
typedef struct
{
uint8_t reportId; // Report ID = 0x05 (5)
uint8_t VEN_VendorDefined0001; // Usage 0xFF000001: , Value = 0 to 255
uint8_t VEN_VendorDefined0002; // Usage 0xFF000002: , Value = 0 to 255
uint8_t VEN_VendorDefined0003; // Usage 0xFF000003: , Value = 0 to 255
uint8_t VEN_VendorDefined0004; // Usage 0xFF000004: , Value = 0 to 255
// Usage 0xFF000005 Value = 0 to 255 <-- Ignored: REPORT_COUNT (4) is too small
} inputReport05_t;
//--------------------------------------------------------------------------------
// Vendor-defined inputReport 06 (Device --> Host)
//--------------------------------------------------------------------------------
typedef struct
{
uint8_t reportId; // Report ID = 0x06 (6)
uint8_t VEN_VendorDefined0001; // Usage 0xFF000001: , Value = 0 to 255
uint8_t VEN_VendorDefined0002; // Usage 0xFF000002: , Value = 0 to 255
uint8_t VEN_VendorDefined0003; // Usage 0xFF000003: , Value = 0 to 255
uint8_t VEN_VendorDefined0004; // Usage 0xFF000004: , Value = 0 to 255
uint8_t VEN_VendorDefined0005; // Usage 0xFF000005: , Value = 0 to 255
uint8_t VEN_VendorDefined0006; // Usage 0xFF000006: , Value = 0 to 255
uint8_t VEN_VendorDefined0007; // Usage 0xFF000007: , Value = 0 to 255
uint8_t VEN_VendorDefined0008; // Usage 0xFF000008: , Value = 0 to 255
uint8_t VEN_VendorDefined0009; // Usage 0xFF000009: , Value = 0 to 255
uint8_t VEN_VendorDefined000A; // Usage 0xFF00000A: , Value = 0 to 255
uint8_t VEN_VendorDefined000B; // Usage 0xFF00000B: , Value = 0 to 255
uint8_t VEN_VendorDefined000C; // Usage 0xFF00000C: , Value = 0 to 255
uint8_t VEN_VendorDefined000D; // Usage 0xFF00000D: , Value = 0 to 255
uint8_t VEN_VendorDefined000E; // Usage 0xFF00000E: , Value = 0 to 255
uint8_t VEN_VendorDefined000F; // Usage 0xFF00000F: , Value = 0 to 255
uint8_t VEN_VendorDefined0010; // Usage 0xFF000010: , Value = 0 to 255
uint8_t VEN_VendorDefined0011; // Usage 0xFF000011: , Value = 0 to 255
uint8_t VEN_VendorDefined0012; // Usage 0xFF000012: , Value = 0 to 255
uint8_t VEN_VendorDefined0013; // Usage 0xFF000013: , Value = 0 to 255
uint8_t VEN_VendorDefined0014; // Usage 0xFF000014: , Value = 0 to 255
uint8_t VEN_VendorDefined0015; // Usage 0xFF000015: , Value = 0 to 255
uint8_t VEN_VendorDefined0016; // Usage 0xFF000016: , Value = 0 to 255
uint8_t VEN_VendorDefined0017; // Usage 0xFF000017: , Value = 0 to 255
uint8_t VEN_VendorDefined0018; // Usage 0xFF000018: , Value = 0 to 255
uint8_t VEN_VendorDefined0019; // Usage 0xFF000019: , Value = 0 to 255
uint8_t VEN_VendorDefined001A; // Usage 0xFF00001A: , Value = 0 to 255
uint8_t VEN_VendorDefined001B; // Usage 0xFF00001B: , Value = 0 to 255
uint8_t VEN_VendorDefined001C; // Usage 0xFF00001C: , Value = 0 to 255
uint8_t VEN_VendorDefined001D; // Usage 0xFF00001D: , Value = 0 to 255
uint8_t VEN_VendorDefined001E; // Usage 0xFF00001E: , Value = 0 to 255
uint8_t VEN_VendorDefined001F; // Usage 0xFF00001F: , Value = 0 to 255
uint8_t VEN_VendorDefined0020; // Usage 0xFF000020: , Value = 0 to 255
uint8_t VEN_VendorDefined0021; // Usage 0xFF000021: , Value = 0 to 255
uint8_t VEN_VendorDefined0022; // Usage 0xFF000022: , Value = 0 to 255
uint8_t VEN_VendorDefined0023; // Usage 0xFF000023: , Value = 0 to 255
uint8_t VEN_VendorDefined0024; // Usage 0xFF000024: , Value = 0 to 255
uint8_t VEN_VendorDefined0025; // Usage 0xFF000025: , Value = 0 to 255
uint8_t VEN_VendorDefined0026; // Usage 0xFF000026: , Value = 0 to 255
uint8_t VEN_VendorDefined0027; // Usage 0xFF000027: , Value = 0 to 255
uint8_t VEN_VendorDefined0028; // Usage 0xFF000028: , Value = 0 to 255
// Usage 0xFF000029 Value = 0 to 255 <-- Ignored: REPORT_COUNT (40) is too small
} inputReport06_t;
You may want to try the following instead:
//--------------------------------------------------------------------------------
// Decoded Application Collection
//--------------------------------------------------------------------------------
PROGMEM char usbHidReportDescriptor[] =
{
0x06, 0x00, 0xFF, // (GLOBAL) USAGE_PAGE 0xFF00 Vendor-defined
0x09, 0x01, // (LOCAL) USAGE 0xFF000001 <-- Warning: Undocumented usage
0xA1, 0x01, // (MAIN) COLLECTION 0x00000001 Application (Usage=0xFF000001: Page=Vendor-defined, Usage=, Type=)
0x15, 0x00, // (GLOBAL) LOGICAL_MINIMUM 0x00 (0) <-- Redundant: LOGICAL_MINIMUM is already 0 <-- Info: Consider replacing 15 00 with 14
0x26, 0xFF, 0x00, // (GLOBAL) LOGICAL_MAXIMUM 0x00FF (255)
0x75, 0x08, // (GLOBAL) REPORT_SIZE 0x08 (8) Number of bits per field
0x85, 0x05, // (GLOBAL) REPORT_ID 0x05 (5)
0x95, 0x04, // (GLOBAL) REPORT_COUNT 0x04 (4) Number of fields
0x19, 0x01, // (LOCAL) USAGE_MINIMUM 0xFF000001 <-- Warning: Undocumented usage
0x29, 0x04, // (LOCAL) USAGE_MAXIMUM 0xFF000004 <-- Warning: Undocumented usage
0x81, 0x00, // (MAIN) INPUT 0x00000000 (4 fields x 8 bits) 0=Data 0=Array 0=Absolute 0=Ignored 0=Ignored 0=PrefState 0=NoNull
0x85, 0x06, // (GLOBAL) REPORT_ID 0x06 (6)
0x95, 0x28, // (GLOBAL) REPORT_COUNT 0x28 (40) Number of fields
0x19, 0x01, // (LOCAL) USAGE_MINIMUM 0xFF000001 <-- Warning: Undocumented usage
0x29, 0x28, // (LOCAL) USAGE_MAXIMUM 0xFF000028 <-- Warning: Undocumented usage
0x81, 0x00, // (MAIN) INPUT 0x00000000 (40 fields x 8 bits) 0=Data 0=Array 0=Absolute 0=Ignored 0=Ignored 0=PrefState 0=NoNull
0xC0, // (MAIN) END_COLLECTION Application
};
//--------------------------------------------------------------------------------
// Vendor-defined inputReport 05 (Device --> Host)
//--------------------------------------------------------------------------------
typedef struct
{
uint8_t reportId; // Report ID = 0x05 (5)
uint8_t VEN_VendorDefined[4]; // Value = 0 to 255
} inputReport05_t;
//--------------------------------------------------------------------------------
// Vendor-defined inputReport 06 (Device --> Host)
//--------------------------------------------------------------------------------
typedef struct
{
uint8_t reportId; // Report ID = 0x06 (6)
uint8_t VEN_VendorDefined[40]; // Value = 0 to 255
} inputReport06_t;
I am a bit new and still learning about socket and RTP stuff. Basically, i want to write a program that can send a jpeg (ultimately gonna be mjpeg) through RTP using UDP protocol.
I have the program for UDP and be able to send files or stream to a local address (127.0.0.1) using openCV. ok. So that is not a problem.
However, when i try to pass the frame that capture through VideoCapture from openCV, it wont work. Or rather i have no idea how to do it to be exact.
So now, i want to break this problem down and decide to test only on the RTP side.
So as you can see in my main function, i tried to test out the functions but it doesn't look like it works.
Can someone point me to the right direction of how to properly test those function, specially the sendFrame one ?
This code mainly just copy and paste from RFC2435 document.
Thanks
`/*
* Table K.1 from JPEG spec.
*/
static const int jpeg_luma_quantizer[64] = {
16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68, 109, 103, 77,
24, 35, 55, 64, 81, 104, 113, 92,
49, 64, 78, 87, 103, 121, 120, 101,
72, 92, 95, 98, 112, 100, 103, 99
};
`
/*
* Table K.2 from JPEG spec.
*/
static const int jpeg_chroma_quantizer[64] = {
17, 18, 24, 47, 99, 99, 99, 99,
18, 21, 26, 66, 99, 99, 99, 99,
24, 26, 56, 99, 99, 99, 99, 99,
47, 66, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
};
int main(int argc, char * argv[]) {
//setup openCV
cvNamedWindow("UDP Video Sender", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCreateCameraCapture(0);
if(!capture){
std::cout<<"No camera found."<< std::endl;
goto DONE;
}
IplImage *frame;
frame = cvQueryFrame(capture);
IplImage *small = cvCreateImage(cvSize(frame->width / 2, frame->height / 2),
frame->depth, 3);
while(1){
//capture frame and resize
frame = cvQueryFrame(capture);
cvResize(frame, small, CV_INTER_LINEAR);
cvShowImage("UDP Video Sender", small);
//MakeHeaders(filename,0,5,5,0,128,0);
//MakeTables(128,frame,0);
//MakeDRIHeader();
// MakeHuffmanHeader();
MakeHuffmanHeader(128,1024,1024,uchar *lum_ac_symbols[],1024,1,1 );
//MakeQuantHeader();
DONE:
cout<<"Press any key to continue."<<endl;
}
}
/*
* Call MakeTables with the Q factor and two u_char[64] return arrays
*/
void
MakeTables(int q, u_char *lqt, u_char *cqt)
{
int i;
int factor = q;
if (q < 1) factor = 1;
if (q > 99) factor = 99;
if (q < 50)
q = 5000 / factor;
else
q = 200 - factor*2;
for (i=0; i < 64; i++) {
int lq = (jpeg_luma_quantizer[i] * q + 50) / 100;
int cq = (jpeg_chroma_quantizer[i] * q + 50) / 100;
/* Limit the quantizers to 1 <= q <= 255 */
if (lq < 1) lq = 1;
else if (lq > 255) lq = 255;
lqt[i] = lq;
if (cq < 1) cq = 1;
else if (cq > 255) cq = 255;
cqt[i] = cq;
}
}
/**The following routines can be used to create the JPEG marker segments
corresponding to the table-specification data that is absent from the
RTP/JPEG body.
*/
u_char lum_dc_codelens[] = {
0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
};
u_char lum_dc_symbols[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
};
u_char lum_ac_codelens[] = {
0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d,
};
u_char lum_ac_symbols[] = {
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa,
};
u_char chm_dc_codelens[] = {
0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
};
u_char chm_dc_symbols[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
};
u_char chm_ac_codelens[] = {
0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77,
};
u_char chm_ac_symbols[] = {
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa,
};
u_char *
MakeQuantHeader(u_char *p, u_char *qt, int tableNo)
{
*p++ = 0xff;
*p++ = 0xdb; /* DQT */
*p++ = 0; /* length msb */
*p++ = 67; /* length lsb */
*p++ = tableNo;
memcpy(p, qt, 64);
return (p + 64);
}
u_char *
MakeHuffmanHeader(u_char *p, u_char *codelens, int ncodes,
u_char *symbols, int nsymbols, int tableNo,
int tableClass)
{
*p++ = 0xff;
*p++ = 0xc4; /* DHT */
*p++ = 0; /* length msb */
*p++ = 3 + ncodes + nsymbols; /* length lsb */
*p++ = (tableClass << 4) | tableNo;
memcpy(p, codelens, ncodes);
p += ncodes;
memcpy(p, symbols, nsymbols);
p += nsymbols;
return (p);
}
u_char *
MakeDRIHeader(u_char *p, u_short dri) {
*p++ = 0xff;
*p++ = 0xdd; /* DRI */
*p++ = 0x0; /* length msb */
*p++ = 4; /* length lsb */
*p++ = dri >> 8; /* dri msb */
*p++ = dri & 0xff; /* dri lsb */
return (p);
}
/*
* Arguments:
* type, width, height: as supplied in RTP/JPEG header
* lqt, cqt: quantization tables as either derived from
* the Q field using MakeTables() or as specified
* in section 4.2.
* dri: restart interval in MCUs, or 0 if no restarts.
*
* p: pointer to return area
*
* Return value:
* The length of the generated headers.
*
* Generate a frame and scan headers that can be prepended to the
* RTP/JPEG data payload to produce a JPEG compressed image in
* interchange format (except for possible trailing garbage and
* absence of an EOI marker to terminate the scan).
*/
int MakeHeaders(u_char *p, int type, int w, int h, u_char *lqt,
u_char *cqt, u_short dri)
{
u_char *start = p;
/* convert from blocks to pixels */
w <<= 3;
h <<= 3;
*p++ = 0xff;
*p++ = 0xd8; /* SOI */
p = MakeQuantHeader(p, lqt, 0);
p = MakeQuantHeader(p, cqt, 1);
if (dri != 0)
p = MakeDRIHeader(p, dri);
*p++ = 0xff;
*p++ = 0xc0; /* SOF */
*p++ = 0; /* length msb */
*p++ = 17; /* length lsb */
*p++ = 8; /* 8-bit precision */
*p++ = h >> 8; /* height msb */
*p++ = h; /* height lsb */
*p++ = w >> 8; /* width msb */
*p++ = w; /* wudth lsb */
*p++ = 3; /* number of components */
*p++ = 0; /* comp 0 */
if (type == 0)
*p++ = 0x21; /* hsamp = 2, vsamp = 1 */
else
*p++ = 0x22; /* hsamp = 2, vsamp = 2 */
*p++ = 0; /* quant table 0 */
*p++ = 1; /* comp 1 */
*p++ = 0x11; /* hsamp = 1, vsamp = 1 */
*p++ = 1; /* quant table 1 */
*p++ = 2; /* comp 2 */
*p++ = 0x11; /* hsamp = 1, vsamp = 1 */
*p++ = 1; /* quant table 1 */
p = MakeHuffmanHeader(p, lum_dc_codelens,
sizeof(lum_dc_codelens),
lum_dc_symbols,
sizeof(lum_dc_symbols), 0, 0);
p = MakeHuffmanHeader(p, lum_ac_codelens,
sizeof(lum_ac_codelens),
lum_ac_symbols,
sizeof(lum_ac_symbols), 0, 1);
p = MakeHuffmanHeader(p, chm_dc_codelens,
sizeof(chm_dc_codelens),
chm_dc_symbols,
sizeof(chm_dc_symbols), 1, 0);
p = MakeHuffmanHeader(p, chm_ac_codelens,
sizeof(chm_ac_codelens),
chm_ac_symbols,
sizeof(chm_ac_symbols), 1, 1);
*p++ = 0xff;
*p++ = 0xda; /* SOS */
*p++ = 0; /* length msb */
*p++ = 12; /* length lsb */
*p++ = 3; /* 3 components */
*p++ = 0; /* comp 0 */
*p++ = 0; /* huffman table 0 */
*p++ = 1; /* comp 1 */
*p++ = 0x11; /* huffman table 1 */
*p++ = 2; /* comp 2 */
*p++ = 0x11; /* huffman table 1 */
*p++ = 0; /* first DCT coeff */
*p++ = 63; /* last DCT coeff */
*p++ = 0; /* sucessive approx. */
return (p - start);
};
/*
* RTP data header from RFC1889
*/
typedef struct {
unsigned int version:2; /* protocol version */
unsigned int p:1; /* padding flag */
unsigned int x:1; /* header extension flag */
unsigned int cc:4; /* CSRC count */
unsigned int m:1; /* marker bit */
unsigned int pt:7; /* payload type */
u_int16_t seq; /* sequence number */
u_int32_t ts; /* timestamp */
u_int32_t ssrc; /* synchronization source */
u_int32_t csrc[1]; /* optional CSRC list */
} rtp_hdr_t;
#define RTP_HDR_SZ 12
/* The following definition is from RFC1890 */
#define RTP_PT_JPEG 26
struct jpeghdr {
unsigned int tspec:8; /* type-specific field */
unsigned int off:24; /* fragment byte offset */
u_int8_t type; /* id of jpeg decoder params */
u_int8_t q; /* quantization factor (or table id) */
u_int8_t width; /* frame width in 8 pixel blocks */
u_int8_t height; /* frame height in 8 pixel blocks */
};
struct jpeghdr_rst {
u_int16_t dri;
unsigned int f:1;
unsigned int l:1;
unsigned int count:14;
};
struct jpeghdr_qtable {
u_int8_t mbz;
u_int8_t precision;
u_int16_t length;
};
#define RTP_JPEG_RESTART 0x40
/* Procedure SendFrame:
*
* Arguments:
* start_seq: The sequence number for the first packet of the current
* frame.
* ts: RTP timestamp for the current frame
* ssrc: RTP SSRC value
* jpeg_data: Huffman encoded JPEG scan data
* len: Length of the JPEG scan data
* type: The value the RTP/JPEG type field should be set to
* typespec: The value the RTP/JPEG type-specific field should be set
* to
* width: The width in pixels of the JPEG image
* height: The height in pixels of the JPEG image
* dri: The number of MCUs between restart markers (or 0 if there
* are no restart markers in the data
* q: The Q factor of the data, to be specified using the Independent
* JPEG group's algorithm if 1 <= q <= 99, specified explicitly
* with lqt and cqt if q >= 128, or undefined otherwise.
* lqt: The quantization table for the luminance channel if q >= 128
* cqt: The quantization table for the chrominance channels if
* q >= 128
*
* Return value:
* the sequence number to be sent for the first packet of the next
* frame.
*
* The following are assumed to be defined:
*
* PACKET_SIZE - The size of the outgoing packet
* send_packet(u_int8_t *data, int len) - Sends the packet to the network
*/
#define PACKET_SIZE 512
u_int16_t SendFrame(u_int16_t start_seq, u_int32_t ts, u_int32_t ssrc,
u_int8_t *jpeg_data, int len, u_int8_t type,
u_int8_t typespec, int width, int height, int dri,
u_int8_t q, u_int8_t *lqt, u_int8_t *cqt) {
rtp_hdr_t rtphdr;
struct jpeghdr jpghdr;
struct jpeghdr_rst rsthdr;
struct jpeghdr_qtable qtblhdr;
u_int8_t packet_buf[PACKET_SIZE];
u_int8_t *ptr;
int bytes_left = len;
int seq = start_seq;
int pkt_len, data_len;
/* Initialize RTP header
*/
rtphdr.version = 2;
rtphdr.p = 0;
rtphdr.x = 0;
rtphdr.cc = 0;
rtphdr.m = 0;
rtphdr.pt = RTP_PT_JPEG;
rtphdr.seq = start_seq;
rtphdr.ts = ts;
rtphdr.ssrc = ssrc;
/* Initialize JPEG header
*/
jpghdr.tspec = typespec;
jpghdr.off = 0;
jpghdr.type = type | ((dri != 0) ? RTP_JPEG_RESTART : 0);
jpghdr.q = q;
jpghdr.width = width / 8;
jpghdr.height = height / 8;
/* Initialize DRI header
*/
if (dri != 0) {
rsthdr.dri = dri;
rsthdr.f = 1; /* This code does not align RIs */
rsthdr.l = 1;
rsthdr.count = 0x3fff;
}
/* Initialize quantization table header
*/
if (q >= 128) {
qtblhdr.mbz = 0;
qtblhdr.precision = 0; /* This code uses 8 bit tables only */
qtblhdr.length = 128; /* 2 64-byte tables */
}
while (bytes_left > 0) {
ptr = packet_buf + RTP_HDR_SZ;
jpghdr.off = htonl(jpghdr.off);
/*convert offset in hdr to network order, copy to packet*/
memcpy(ptr, &jpghdr, sizeof(jpghdr));
jpghdr.off = ntohl(jpghdr.off);
ptr += sizeof(jpghdr);
data_len = PACKET_SIZE - (ptr - packet_buf);
if (data_len >= bytes_left) {
data_len = bytes_left;
rtphdr.m = 1;
}
rtphdr.seq = htons(rtphdr.seq);
memcpy(packet_buf, &rtphdr, RTP_HDR_SZ);
memcpy(ptr, jpeg_data + jpghdr.off, data_len);
if(((ptr-packet_buf)+data_len) &&
send(sock[0], packet_buf, (ptr - packet_buf) + data_len, 0)<0)
perror("hre");
jpghdr.off += data_len;
bytes_left -= data_len;
rtphdr.seq = ntohs(rtphdr.seq);
rtphdr.seq++;
}
free(packet_buf);
return rtphdr.seq;
}
I am programming an RS-485 protocol on a PIC micro controller and Linux computer. I was originally thinking about using CRC8 to check incoming data, however it looks like this would be a processor intensive task.
Instead I was thinking of a more simple PEC algorithm, perhaps XORing all the incoming bytes with a seed to create a very simple single step implementation of CRC.
What downside would having an algorithm such as this be?
A CRC is not processor-intensive. All it adds to your exclusive-or is a table lookup. The operation on each byte is simply: crc = crc8_table[crc ^ *data++]. See below.
The downside of doing just an exclusive-or is that there are many simple errors that cancel each other, resulting in a false-positive check. A CRC is much better.
#include <stddef.h>
/* 8-bit CRC with polynomial x^8+x^6+x^3+x^2+1, 0x14D.
Chosen based on Koopman, et al. (0xA6 in his notation = 0x14D >> 1):
http://www.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf
*/
static unsigned char crc8_table[] = {
0x00, 0x3e, 0x7c, 0x42, 0xf8, 0xc6, 0x84, 0xba, 0x95, 0xab, 0xe9, 0xd7,
0x6d, 0x53, 0x11, 0x2f, 0x4f, 0x71, 0x33, 0x0d, 0xb7, 0x89, 0xcb, 0xf5,
0xda, 0xe4, 0xa6, 0x98, 0x22, 0x1c, 0x5e, 0x60, 0x9e, 0xa0, 0xe2, 0xdc,
0x66, 0x58, 0x1a, 0x24, 0x0b, 0x35, 0x77, 0x49, 0xf3, 0xcd, 0x8f, 0xb1,
0xd1, 0xef, 0xad, 0x93, 0x29, 0x17, 0x55, 0x6b, 0x44, 0x7a, 0x38, 0x06,
0xbc, 0x82, 0xc0, 0xfe, 0x59, 0x67, 0x25, 0x1b, 0xa1, 0x9f, 0xdd, 0xe3,
0xcc, 0xf2, 0xb0, 0x8e, 0x34, 0x0a, 0x48, 0x76, 0x16, 0x28, 0x6a, 0x54,
0xee, 0xd0, 0x92, 0xac, 0x83, 0xbd, 0xff, 0xc1, 0x7b, 0x45, 0x07, 0x39,
0xc7, 0xf9, 0xbb, 0x85, 0x3f, 0x01, 0x43, 0x7d, 0x52, 0x6c, 0x2e, 0x10,
0xaa, 0x94, 0xd6, 0xe8, 0x88, 0xb6, 0xf4, 0xca, 0x70, 0x4e, 0x0c, 0x32,
0x1d, 0x23, 0x61, 0x5f, 0xe5, 0xdb, 0x99, 0xa7, 0xb2, 0x8c, 0xce, 0xf0,
0x4a, 0x74, 0x36, 0x08, 0x27, 0x19, 0x5b, 0x65, 0xdf, 0xe1, 0xa3, 0x9d,
0xfd, 0xc3, 0x81, 0xbf, 0x05, 0x3b, 0x79, 0x47, 0x68, 0x56, 0x14, 0x2a,
0x90, 0xae, 0xec, 0xd2, 0x2c, 0x12, 0x50, 0x6e, 0xd4, 0xea, 0xa8, 0x96,
0xb9, 0x87, 0xc5, 0xfb, 0x41, 0x7f, 0x3d, 0x03, 0x63, 0x5d, 0x1f, 0x21,
0x9b, 0xa5, 0xe7, 0xd9, 0xf6, 0xc8, 0x8a, 0xb4, 0x0e, 0x30, 0x72, 0x4c,
0xeb, 0xd5, 0x97, 0xa9, 0x13, 0x2d, 0x6f, 0x51, 0x7e, 0x40, 0x02, 0x3c,
0x86, 0xb8, 0xfa, 0xc4, 0xa4, 0x9a, 0xd8, 0xe6, 0x5c, 0x62, 0x20, 0x1e,
0x31, 0x0f, 0x4d, 0x73, 0xc9, 0xf7, 0xb5, 0x8b, 0x75, 0x4b, 0x09, 0x37,
0x8d, 0xb3, 0xf1, 0xcf, 0xe0, 0xde, 0x9c, 0xa2, 0x18, 0x26, 0x64, 0x5a,
0x3a, 0x04, 0x46, 0x78, 0xc2, 0xfc, 0xbe, 0x80, 0xaf, 0x91, 0xd3, 0xed,
0x57, 0x69, 0x2b, 0x15};
unsigned crc8(unsigned crc, unsigned char *data, size_t len)
{
unsigned char *end;
if (len == 0)
return crc;
crc ^= 0xff;
end = data + len;
do {
crc = crc8_table[crc ^ *data++];
} while (data < end);
return crc ^ 0xff;
}
/* this was used to generate the table and to test the table-version
#define POLY 0xB2
unsigned crc8_slow(unsigned crc, unsigned char *data, size_t len)
{
unsigned char *end;
if (len == 0)
return crc;
crc ^= 0xff;
end = data + len;
do {
crc ^= *data++;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
} while (data < end);
return crc ^ 0xff;
}
*/
#include <stdio.h>
#define SIZE 16384
int main(void)
{
unsigned char data[SIZE];
size_t got;
unsigned crc;
crc = 0;
do {
got = fread(data, 1, SIZE, stdin);
crc = crc8(crc, data, got);
} while (got == SIZE);
printf("%02x\n", crc);
return 0;
}
The table look up method for generating the PEC value is certainly faster. My tests on a a PIC32 running at 80 MHz for a 4 byte packet indicates that the table method required 2.8us while the algorithm method needed 11.5us. The memory requirements shows the cost of of speed: The table method requires 348 bytes while the algorithm method on;y 216 bytes. So if memory is scarce - consider the algorithm approach shown here. (BYTE is an unsigned char)
/* bit_crc8 FUNCTION DESCRIPTION ************************************
* SYNTAX: BYTE bit_crc8( BYTE *data, BYTE len);
* KEYWORDS: PEC, CRC, error checking
* DESCRIPTION: Returns the PEC for an array of bytes. This method does
* not use a lookup table
* PARAMETER 1: BYTE pointer to data array
* PARAMETER 2: BYTE - Number of bytes in array
* RETURN VALUE: BYTE - PEC value
* NOTES: SMBus limits the number of bytes in the packet to 256
* Primitive polynomial is set by the definition of PEC.
* END DESCRIPTION **********************************************************/
BYTE bit_crc8( BYTE *data, BYTE len)
{
#define PEC 0x07 // Implements Polynomial X^8 + X^2 + X^1 +1
BYTE crc = 0;
BYTE loop, b8;
while (len--)
{
crc ^= *data++;
for(loop=0; loop <8; loop++)
{
b8 = crc & 0x80; /* Test for MSB set to 1 */
crc <<= 1; /* Left shift CRC */
if(b8)
{
crc ^= PEC; /* Divide by PEC if bit 8 was set */
}
}
}
return crc;
}