Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 17th, 2005, 11:30 AM   #1
SerialGuy
Newbie
 
Join Date: Mar 2005
Posts: 5
Rep Power: 0 SerialGuy is on a distinguished road
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.
SerialGuy is offline   Reply With Quote
Old Mar 17th, 2005, 3:39 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You could add support for escape characters, and parse them accordingly.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 17th, 2005, 4:24 PM   #3
SerialGuy
Newbie
 
Join Date: Mar 2005
Posts: 5
Rep Power: 0 SerialGuy is on a distinguished road
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.
SerialGuy is offline   Reply With Quote
Old Mar 17th, 2005, 4:25 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Make up your own: \01, \02...
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 17th, 2005, 4:28 PM   #5
SerialGuy
Newbie
 
Join Date: Mar 2005
Posts: 5
Rep Power: 0 SerialGuy is on a distinguished road
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?
SerialGuy is offline   Reply With Quote
Old Mar 17th, 2005, 5:18 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Basically, you allow the user to type \01, and change it to 0x01 yourself.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 17th, 2005, 5:23 PM   #7
SerialGuy
Newbie
 
Join Date: Mar 2005
Posts: 5
Rep Power: 0 SerialGuy is on a distinguished road
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.
SerialGuy is offline   Reply With Quote
Old Mar 17th, 2005, 7:16 PM   #8
SerialGuy
Newbie
 
Join Date: Mar 2005
Posts: 5
Rep Power: 0 SerialGuy is on a distinguished road
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.
SerialGuy is offline   Reply With Quote
Old Mar 18th, 2005, 10:50 AM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Congrats.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC