![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 5
Rep Power: 0
![]() |
C++ Serial Programming: Transmission of Unprintable Characters with CSerial Library
Hello all, I'm new to the forums.
I'm developing a program that uses serial communication, and I searched for a library to make things easier. I found this one: http://www.codeproject.com/system/se...mid=1996&fr=26 It looks very helpful and useful, but there is something that is stopping me from being able to use it. My communications protocol involves unprintable character bytes such as 0x00, 0x05 (ACK), and 0x15 (NAK), among other things. The Read and Write functions (which receive and transmit bytes through the serial port) both take in a LPCSTR parameter. This is fine if you want to pass strings like "Hello World", etc, but how can I get the Read and Write functions to recognize unprintable characters and send them? The 0x00 null character is especially a problem - if it is passed, the transmission ends at that point because the LPCSTR reaches the "end" of the string. I need to be able to transmit a byte with the value of 0x00. I tried using ALT+___ on the numpad, where you hold down ALT and press the DECIMAL equivalent of the hex value to get the character (eg ALT+5 for ACK, let go of ALT, ALT+21 for NAK) but MS Visual C++ (using version 6.0) just put a question mark for them as if to say it can't print them on the screen (I tested this and the actual character being transferred was '?'). What can I do to solve this problem? I need to be able to transfer bytes as unsigned chars freely, with 0x00 to 0xFF all being possibilities, sort of like how outportb and inportb worked back in the good ol' DOS days. I appreciate any help anyone can provide. Thank you. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You could add support for escape characters, and parse them accordingly.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 5
Rep Power: 0
![]() |
Thanks for the reply.
Escape characters - as in, \n, \t, and the like? That covers only a few of the unprintable characters. I did manage to succeed in sending 0x09, for example, by setting the value of the byte to '\t', but I don't believe there is an escape character that will send 0x06, for example. As well, I need to be able to receive these characters (especially 0x00, which I _think_ causes the array to eliminate, making the function think that it's the end of the transmission) using the Read function in the library or the ReadFile function in the Win32 API. I cannot alter the protocol that I am supposed to use -- my application is supposed to receive data packets that contain bytes with values such as 0x00, and especially for the handshaking part, ACK and NAK (after a message is received, send an ACK, if message is bad or times out, send a NAK). Essentially what it comes down to is that I need to be able to send and receive bytes with these values over the serial port? Is this just impossible to do under Windows with the Win32 API? Thanks in advance for any further help you guys can provide. |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Make up your own: \01, \02...
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2005
Posts: 5
Rep Power: 0
![]() |
I'm not sure I understand. If for example I need to make the program transmit 0x01, how does placing \01 in the array alleviate the problem?
And furthermore, what can be done on the receiving end? |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Basically, you allow the user to type \01, and change it to 0x01 yourself.
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Mar 2005
Posts: 5
Rep Power: 0
![]() |
Could you elaborate on that? The problem is that if I'm supposed to receive 0x01 through the serial port, I need to be able to have it read as part of the string packet. And vice versa - I need a way to to send the hex value 0x01 out -- I cannot change this to sending \01 because the other end is not expecting that. The problem I'm encountering is all in how the character array (LPCSTR or char*, whatever) is set up for the Read function - I can't seem to assign a hex value to it - Visual C++ and the Win32 API seem to want only printable characters transferred, which is a problem for me because I need to send ACKs and NAKs and I need to be able to send null bytes without having them interrupt the string.
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Mar 2005
Posts: 5
Rep Power: 0
![]() |
After hours and hours of dicking around (I'm still in the office at past 8PM) with different libraries and different hacks to try to make this work, I finally made it work (*knock on wood*).
The key was in the whole LPCSTR parameter of the Write function for the class. I changed it to a BYTE* (pointer to a byte array). The thing now is though that whereas before it was called by doing port.Write("Hello World"), you can now say BYTE sendthis[] = "Hello World"; BYTE[5] = 0x00; //just for fun ![]() port.Write(sendthis, 11); // <== specify number of BYTES to send to force it to send past the null character Man, that was a relief. Now, I have to test to make sure that it can RECEIVE null bytes too. For a second there, I almost thought I was done, heh. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|