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

Sending a command to a COM port...

P: n/a
Hi,

I am trying to write a script that sends a code to a COM port to open a till
draw. The code at the bottom of the page (written in C) is what the
suppliers sent as an example. I have used the SerialPort class in .Net 2.0
to try and send the same message. So far I have this on a button click:

-------------------------------------------------------------
Dim ComPort As New SerialPort("COM6", 9600)
ComPort.Parity = Parity.None
ComPort.StopBits = StopBits.One
ComPort.DataBits = 8
ComPort.Handshake = Handshake.None
ComPort.Open()
ComPort.Write("0x07")
ComPort.Close()
ComPort.Dispose()
-------------------------------------------------------------

The code runs without error (unless I run it on a machine that doesn't have
COM6 installed on it) - but nothing happens.

Am I passing the command in the right format?

Any ideas?

Thanks in advance,
Stu

-------------------------------------------------------------
The C code example sent:
-------------------------------------------------------------
#include <dos.h>
#include <stdio.h>
#include <bios.h>
#include <process.h>
//
#define CacheDrawerStatusPort 0x404a
#define CacheDrawerCtrlPort 0x404d
#define CacheDrawerStatusBitOffset 0x10
#define CacheDrawerCtrlBitOffset 0x80
//
void ShowCacheDrawerStatus(void);
//
void main(void)
{
unsigned char CacheDrawerCtrlBit;
int Key;
//
ShowCacheDrawerStatus();
printf("Press Space key to toggle cache drawer control bit.\n");
printf("Press Ctrl+Q key to exit.\n");
while(1)
{
while(!bioskey(1));
Key=bioskey(0);
Key&=0x00ff;
switch(Key)
{
case 0x20:
CacheDrawerCtrlBit=inportb(CacheDrawerCtrlPort);
CacheDrawerCtrlBit^=CacheDrawerCtrlBitOffset;
outportb(CacheDrawerCtrlPort,CacheDrawerCtrlBit);
delay(1000);
ShowCacheDrawerStatus();
break;
case 0x11:
exit(0);
break;
}
}
}

void ShowCacheDrawerStatus(void)
{
unsigned char CacheDrawerStatusBit;
CacheDrawerStatusBit=inportb(CacheDrawerStatusPort );
if((CacheDrawerStatusBit&CacheDrawerStatusBitOffse t) ==
CacheDrawerStatusBitOffset)
printf("Cache drawer is closed now.\n");
else
printf("Cache drawer is opened now.\n");
}
Jul 11 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Numbers prefixed with 0x usually represent a hexidecimal value, in this case Hex
07.

Try replacing that line with ComPort.Write(chr(7)).

"Stu Lock" <ne**@cergis.comwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
Hi,

I am trying to write a script that sends a code to a COM port to open a till
draw. The code at the bottom of the page (written in C) is what the suppliers
sent as an example. I have used the SerialPort class in .Net 2.0 to try and
send the same message. So far I have this on a button click:

-------------------------------------------------------------
Dim ComPort As New SerialPort("COM6", 9600)
ComPort.Parity = Parity.None
ComPort.StopBits = StopBits.One
ComPort.DataBits = 8
ComPort.Handshake = Handshake.None
ComPort.Open()
ComPort.Write("0x07")
ComPort.Close()
ComPort.Dispose()
-------------------------------------------------------------

The code runs without error (unless I run it on a machine that doesn't have
COM6 installed on it) - but nothing happens.

Am I passing the command in the right format?

Any ideas?

Thanks in advance,
Stu

-------------------------------------------------------------
The C code example sent:
-------------------------------------------------------------
#include <dos.h>
#include <stdio.h>
#include <bios.h>
#include <process.h>
//
#define CacheDrawerStatusPort 0x404a
#define CacheDrawerCtrlPort 0x404d
#define CacheDrawerStatusBitOffset 0x10
#define CacheDrawerCtrlBitOffset 0x80
//
void ShowCacheDrawerStatus(void);
//
void main(void)
{
unsigned char CacheDrawerCtrlBit;
int Key;
//
ShowCacheDrawerStatus();
printf("Press Space key to toggle cache drawer control bit.\n");
printf("Press Ctrl+Q key to exit.\n");
while(1)
{
while(!bioskey(1));
Key=bioskey(0);
Key&=0x00ff;
switch(Key)
{
case 0x20:
CacheDrawerCtrlBit=inportb(CacheDrawerCtrlPort);
CacheDrawerCtrlBit^=CacheDrawerCtrlBitOffset;
outportb(CacheDrawerCtrlPort,CacheDrawerCtrlBit);
delay(1000);
ShowCacheDrawerStatus();
break;
case 0x11:
exit(0);
break;
}
}
}

void ShowCacheDrawerStatus(void)
{
unsigned char CacheDrawerStatusBit;
CacheDrawerStatusBit=inportb(CacheDrawerStatusPort );
if((CacheDrawerStatusBit&CacheDrawerStatusBitOffse t) ==
CacheDrawerStatusBitOffset)
printf("Cache drawer is closed now.\n");
else
printf("Cache drawer is opened now.\n");
}


Jul 11 '06 #2

P: n/a
Hi,

Two problems

You cannot close the port IMMEDIATELY after (attempting) to send data. The
close will happen before the data have been sent -- Windows buffers the
data, then puts it in the serial UART (hardware), and this data will be
discarded before it can be sent.

The best (IMO) way to handle port open and close is to open the port when
you start your program, and close it when the program terminates. You do
not have to call Dispose.

However, in addition... Your write should look like this (or some variant):

ComPort.Write(Chr(7))

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
Jul 12 '06 #3

P: n/a
Many thanks that did it.

We don't seem to be having any problems with closing the port immediately -
works every time.

"Dick Grier" <dick_grierNOSPAM@.msn.comwrote in message
news:uA****************@TK2MSFTNGP04.phx.gbl...
Hi,

Two problems

You cannot close the port IMMEDIATELY after (attempting) to send data.
The close will happen before the data have been sent -- Windows buffers
the data, then puts it in the serial UART (hardware), and this data will
be discarded before it can be sent.

The best (IMO) way to handle port open and close is to open the port when
you start your program, and close it when the program terminates. You do
not have to call Dispose.

However, in addition... Your write should look like this (or some
variant):

ComPort.Write(Chr(7))

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.

Jul 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.