COM Ports in Smartphone Emulator

F

Felix

Hi,
I'm writing a smartphone application that connects to a com port for
data. (The com port is a bluetooth outgoing port) The emulator has an
option where you can map Serial Ports on the emulated device to serial
ports on your computer.

I hooked up a serial device to my COM 1 on my desktop and used
Hyperterm to get data from it..All is fine COM1 4800 bps..etc...i'm
able to see data from the serial device.

So I mapped Serial Port 0 on the emulator to Com1 on my desktop and I
created a project in VS. Here is the code:

SerialPort sp = new SerialPort("COM0");
sp.BaudRate = 4800;
sp.Open();

byte[] rawByte = new byte[256];
sp.Read(rawByte, 0, 256);

label1.Text = rawByte.ToString();

Very simple stuff just gets data from the serial port once 256 bytes
and writes them to a label on a form.

No matter what I try I can't get this to work. Has anyone been
successful doing this?

Thank you,
Felix
 
F

Felix

Sorry forgot to mention it's actually SerialPort sp = new
SerialPort("COM1"); Not COM0 because I read somewhere that Serial Port
0 on the emulator is actually "COM1".
 
D

Dick Grier

Hi,

You have to wait to read, until the reply actually is there -- you cannot
read it immediatly after sending.

Either use a loop (you can check the BytesToRead property, or simply append
all new data to a buffer, until that buffer has the expected response), or
you the ReceiveData event to do the equivalent (a loop is easier, IMO,
because it requires passing less data back and forth, but there isn't much
difference).

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.
 
D

Dick Grier

BTW, if you are interested, I have example code (VB, not C# -- but there is
very little difference in syntax) on the CD-ROM that accompanies my book.
See below.

--
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top