473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Sending a command to a COM port...

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
3 3511
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chuck Rittersdorf | last post by:
Hi There I am having a problem using the win32 API from VB6. I am trying to send a command string to a printer(zebra TLP 2742) on LPT1 using the folowing API functions CreateFile and...
4
by: Scott | last post by:
I am using a TCPIP connection to communicate over port 23 (telnet) to a server, and I am having to mimic normal command line interface you owuld see in a telnet session (i.e. I have to write to...
3
by: Sells, Fred | last post by:
I'm using MSW XP Pro with Python 2.4 to develop but production will be Linux with Python 2.3. (could upgrade to 2.4 if absolutely necessary) I can also switch to Linux for development if...
2
by: jyotish.bora | last post by:
Hi , i m trying to send a message packet to a RS232 port. I have created the handle and set the parameters. Now the general format of the packet is <STX>command<ETX><LRC> I have also written...
7
by: D. Patrick | last post by:
I need to duplicate the functionality of a java applet, and how it connects to a remote server. But, I don't have the protocol information or the java source code which was written years ago. ...
3
by: armando perez | last post by:
Hi, this is my first time here, but I was looking for something that may help me, and still, I haven't found something to use. I'm using a website made in .NET with C#, when I'm running my site...
1
by: Rich | last post by:
Hello, I am working on a python library for sending and receiving data from a Subaru's ECU (the fuel injection computer) via the OBD-II port and an OBD to USB cable, with the Subaru Select Monitor...
0
by: Charles Crawford | last post by:
Hi, This apparently is a common problem and I've yet to read a solution that actually works for my specific situation. I have a Zebra RW220 printer that I connect to via Bluetooth connection...
0
by: =?Utf-8?B?Q2hhcmxpZQ==?= | last post by:
Hi, This apparently is a common problem and I've yet to read a solution that actually works for my specific situation. I have a Zebra RW220 printer that I connect to via Bluetooth connection...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.