By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

how to send command to serial ports in C++

P: 5
Hi,

I'm really new to C++. I'd like to know how to send command to serial ports.
Im connecting to a uniwell POS machine and I dont know what commands I'll be using. They gave me this serial communication protocol manual with this format.


1.4 Clear All Request

It is always safest to clear all existing report requests before you request the days reports. This is done by sending the following request.

SOH C - A STX 0 1 CR LF ETX BCC


how do you send commands like that to a serial port?
Thank you.
Mar 11 '09 #1
Share this Question
Share on Google+
9 Replies


100+
P: 687
On windows, open file handle with command like
HANDLE serialhCom = CreateFileA(devname, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
Then write to it using WriteFile().
On other OS it will differ, it depends on OS more that on language.
Mar 11 '09 #2

P: 5
hi newb16.

Thanks for the reply.

Yes i've done the connection part and other stuff.. My concern is how to pass that particular command to a serial port. I know it would be using the WriteFile command..


Shall I do it this way?

char strCmd[] = {'S','O','H','F','-','R','S','T','X','0','1','C','R','L','F','E','T', 'X','B','C','C'};

or in hexadecimal format?

char strCmd[] = {0x01,0x46,0x2D,0x52,0x53,0x54,0x58,0x00,0x01,0x0D ,0x0A,0x03};


and ive called this function:

BOOL bWriteCmd = objCom->sendData(strCmd,sizeof(strCmd));

//where sendData() function is declared as member of a class:

DWORD comport::sendData (const char* data, DWORD size)
{
DWORD numberOfBytesWritten;

WriteFile(_hCom,
data,
size,
&numberOfBytesWritten,
0);

return numberOfBytesWritten;
}

and I've tried to get the machine's reply by calling this function:

DWORD comport::receiveData (char* data, DWORD size)
{
DWORD numberOfBytesRead;

ReadFile(_hCom,
data,
size,
&numberOfBytesRead,
0);

return numberOfBytesRead;
}

coz i've done both strCmd declaration and I haven't received a reply from the pos machine.
Please help. Thank you.
Mar 11 '09 #3

gpraghuram
Expert 100+
P: 1,275
HI,
U need not convert the command to hexadecimal format and send it.
If you do like that then in the receiving box you have to reconvert it back and execute the command.
So send the command as a string and after reading the command execute it on the other box.

Raghu
Mar 11 '09 #4

P: 5
Hi Raghu,

Thanks for the reply. I've tried it just now... but still not working. Is there a way that maybe I could get a reply just so I would be sure that I am really transfering a command to the machine? maybe a generic command that would get the machine's address, name.. etc.
Mar 12 '09 #5

gpraghuram
Expert 100+
P: 1,275
Can you explain in which part u r having issue?
Issue is in the receiving part or in execution of the received command?

raghu
Mar 12 '09 #6

Expert 10K+
P: 11,448
@Cerebro
In ASCII SOH means 'Start Of Heading' (code 0x01) and STX means 'Start of TeXt' (code 0x02), CR means 'Carriage Return' (code 0x0D), LF means 'Line Feed' (code 0x0A), ETX means "End of TeXt' (code 0x03) but I don't know what the BCC means or how to interpret C - A. Check your manual

kind regards,

Jos
Mar 12 '09 #7

P: 5
@gpraghuram
hi,


issue is I can't get the machine to respond to my command. It might be that the machine does not understand my command, which is why I was asking how to send the command to serial port, or I'm lost here. I'll read tutorials on the net. Thank you for the response.
Mar 13 '09 #8

P: 5
@JosAH

Hi Jos,

Yes i know what that means, that's why I was asking how to send those commands to a serial ports..by hex or by string.. coz I still can't get the machine to reply to me.

Thank you for your reply.
Mar 13 '09 #9

Expert 10K+
P: 11,448
@Cerebro
What I saw from your reply #3 is that you were trying to send the symbolic names of the commands; that's not how you do it; if you have to send:

SOH C - A STX 0 1 CR LF ETX BCC

... you actually have to send the byte values

0x01 'C' '-' 'A' 0x01 '0' '1' 0x0d 0x0a 0x02 'B' 'C' 'C'

I'm not sure about that C - A part and the BCC part, check your manual.

kind regards,

Jos
Mar 13 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.