Sending a command to a COM port...

  • Thread starter Thread starter Stu Lock
  • Start date Start date
S

Stu Lock

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&CacheDrawerStatusBitOffset) ==
CacheDrawerStatusBitOffset)
printf("Cache drawer is closed now.\n");
else
printf("Cache drawer is opened now.\n");
}
 
Numbers prefixed with 0x usually represent a hexidecimal value, in this case Hex
07.

Try replacing that line with ComPort.Write(chr(7)).
 
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.
 
Back
Top