469,271 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

how to send command to serial ports in C++

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
9 12060
newb16
687 512MB
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
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
1,275 Expert 1GB
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
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
1,275 Expert 1GB
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
JosAH
11,448 Expert 8TB
@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
@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
@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
JosAH
11,448 Expert 8TB
@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.

Similar topics

4 posts views Thread by M. Raab | last post: by
13 posts views Thread by Al the programmer | last post: by
1 post views Thread by David | last post: by
3 posts views Thread by Tom Brown | last post: by
2 posts views Thread by joaquimfpinto | last post: by
3 posts views Thread by naveen.sabapathy | last post: by
2 posts views Thread by Nasif | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.