Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Sending Hex Data to the Serial Port

I've read a lot of the previous topics in this forum involving the RS232 port. The problem I'm having is I can only successfully send ASCII data but not Hex. The device I am connecting to is a ticket printer, like a movie ticket printer. The Hex data I want to send specifies the layout/size/colour of text.

I suspect I am doing something wrong at the WriteToFile part of the code.

[code]

hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);

OVERLAPPED osWrite = {0};
DWORD dwToWrite = 23;
DWORD dwWritten;

osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

unsigned char bytes[23] = {27 , 42 , 29 , 86 , 1 ,
27 , 89 , 0 , 27 , 88 ,
0 , 0 , 27 , 99 , 00 ,
29 , 33 , 34 , 84 , 101,
115, 116, 12 };

//Now for the writing

WriteFile(hComm, bytes, dwToWrite, &dwWritten, &osWrite);

[/code]

Well thanks in advanced everyone.

-Angus

Comments

  • Ed HallEd Hall Posts: 395Member
    I might be confused, but isn't all serial data sent in the form of binary, one bit at a time? And, the conversion is done by the routine? The only necessary step from the programmer is to supply the correct format of the characters, i.e. 0x27 for hex 27, 27 for decimal 27, etc. 27 would mean 27 decimal or 1B in hex. Am I missing something as well?

    Take Care,
    Ed Hall

    : I've read a lot of the previous topics in this forum involving the
    : RS232 port. The problem I'm having is I can only successfully send
    : ASCII data but not Hex. The device I am connecting to is a ticket
    : printer, like a movie ticket printer. The Hex data I want to send
    : specifies the layout/size/colour of text.
    :
    : I suspect I am doing something wrong at the WriteToFile part of the
    : code.
    :
    : [code]:
    :
    : hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
    :
    : OVERLAPPED osWrite = {0};
    : DWORD dwToWrite = 23;
    : DWORD dwWritten;
    :
    : osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    :
    : unsigned char bytes[23] = {27 , 42 , 29 , 86 , 1 ,
    : 27 , 89 , 0 , 27 , 88 ,
    : 0 , 0 , 27 , 99 , 00 ,
    : 29 , 33 , 34 , 84 , 101,
    : 115, 116, 12 };
    :
    : //Now for the writing
    :
    : WriteFile(hComm, bytes, dwToWrite, &dwWritten, &osWrite);
    :
    : [/code]:
    :
    : Well thanks in advanced everyone.
    :
    : -Angus
    :
Sign In or Register to comment.