473,698 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("COM 6", 9600)
ComPort.Parity = Parity.None
ComPort.StopBit s = StopBits.One
ComPort.DataBit s = 8
ComPort.Handsha ke = 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 CacheDrawerStat usPort 0x404a
#define CacheDrawerCtrl Port 0x404d
#define CacheDrawerStat usBitOffset 0x10
#define CacheDrawerCtrl BitOffset 0x80
//
void ShowCacheDrawer Status(void);
//
void main(void)
{
unsigned char CacheDrawerCtrl Bit;
int Key;
//
ShowCacheDrawer Status();
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:
CacheDrawerCtrl Bit=inportb(Cac heDrawerCtrlPor t);
CacheDrawerCtrl Bit^=CacheDrawe rCtrlBitOffset;
outportb(CacheD rawerCtrlPort,C acheDrawerCtrlB it);
delay(1000);
ShowCacheDrawer Status();
break;
case 0x11:
exit(0);
break;
}
}
}

void ShowCacheDrawer Status(void)
{
unsigned char CacheDrawerStat usBit;
CacheDrawerStat usBit=inportb(C acheDrawerStatu sPort);
if((CacheDrawer StatusBit&Cache DrawerStatusBit Offset) ==
CacheDrawerStat usBitOffset)
printf("Cache drawer is closed now.\n");
else
printf("Cache drawer is opened now.\n");
}
Jul 11 '06 #1
3 3532
Numbers prefixed with 0x usually represent a hexidecimal value, in this case Hex
07.

Try replacing that line with ComPort.Write(c hr(7)).

"Stu Lock" <ne**@cergis.co mwrote in message
news:%2******** **********@TK2M SFTNGP04.phx.gb l...
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("COM 6", 9600)
ComPort.Parity = Parity.None
ComPort.StopBit s = StopBits.One
ComPort.DataBit s = 8
ComPort.Handsha ke = 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 CacheDrawerStat usPort 0x404a
#define CacheDrawerCtrl Port 0x404d
#define CacheDrawerStat usBitOffset 0x10
#define CacheDrawerCtrl BitOffset 0x80
//
void ShowCacheDrawer Status(void);
//
void main(void)
{
unsigned char CacheDrawerCtrl Bit;
int Key;
//
ShowCacheDrawer Status();
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:
CacheDrawerCtrl Bit=inportb(Cac heDrawerCtrlPor t);
CacheDrawerCtrl Bit^=CacheDrawe rCtrlBitOffset;
outportb(CacheD rawerCtrlPort,C acheDrawerCtrlB it);
delay(1000);
ShowCacheDrawer Status();
break;
case 0x11:
exit(0);
break;
}
}
}

void ShowCacheDrawer Status(void)
{
unsigned char CacheDrawerStat usBit;
CacheDrawerStat usBit=inportb(C acheDrawerStatu sPort);
if((CacheDrawer StatusBit&Cache DrawerStatusBit Offset) ==
CacheDrawerStat usBitOffset)
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(C hr(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_grierNOSP AM@.msn.comwrot e in message
news:uA******** ********@TK2MSF TNGP04.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(C hr(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
16705
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 WriteFile I have written C code which works fine, just the VB translation/port
4
13987
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 this socket just like you would enter things on the keyboard if you telneted to this server). In a normal telnet session you can enter some commands which take a long time to complete, and they run in the background and you get the command line...
3
11332
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 necessary. I am writing some python to replace proprietary software that talks to a timeclock via UDP. The timeclock extracts the sending port from the UDP header and uses that for all response messages.
2
12120
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 the code to calculate <LRC>. now i dont understand how to make the packet string. should i call writefile with STX, command , ETX and LRC characters differently or in a single string. can anyone give me the exact string for a command say
7
2683
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. So, I used a packet sniffer and saw the protocol (TCP), the port, IP address, etc. All is good. I tried 2 different versions of .NET code to duplicate the requests to the remote server. Again, I used the packet sniffer and my packets seemed...
3
4274
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 from my PC, everything seems right, but when I publish the website, and send it to my real website, everything works but the mail sending. I have red some groups and topics, and everywhere I can found: - Use the following syntax: <system.net>
1
15079
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 protocol. There are a few open source programs that do this already (http://romraider.com/ , http://jdash.sourceforge.net/ , http://tari.co.za/downloads/software/source/ ), but they are written in Java or C++. I have never done anything with serial...
0
2877
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 mapped to a serial port. The application I'm writing runs on a Windows Mobile 5 device, so there's no built in printer support and I have to use a serial port. When I try to write the zpl code to the serial port, the printer simply prints the code...
0
3773
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 mapped to a serial port. The application I'm writing runs on a Windows Mobile 5 device, so there's no built in printer support and I have to use a serial port.
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.