views:

482

answers:

6

i have a program that send ASCII commands to a device via a serial port. The program is a demo and doesn't do what i want.

I am just trying to get the device to respond in c# and I'm not getting anything back. all the serial port settings are correct. I am sending exactly the same message as the demo software.

//e.g message <STX>ABC<EOT>

byte[] msg = new byte[5];

msg[0] = 0x02;
msg[1] = 0x41;
msg[2] = 0x42;
msg[3] = 0x43;
msg[4] = 0x04;
comport.write(msg, 0,msg.length)

the device is a monitor. The code was only an example. there isn't a heartbeat just a response for a correct message sent or a error message. the settings are standard 9600-8-N-1. "paperclip between pins 2 and 3 (TX and RX)." yeah i know the cable works because its the same one used with the product software that works. and im getting back what i send. I have used a virtual com program and everything seems alright. "6 bytes but initializing only the first 5. " sorry typo.

the SerialPort.DtrEnable and RtsEnable properties to true. are on by default in c#. i have tried the hyper terminal and am not getting a response with that either. I have sent \r and \n with no luck.

+1  A: 

This C# Tutorial on Serial Port Communication should be able to help. I'm not sure if there is an appropriatei intrinsic caste between Hex and Byte through assignment in C#. That may be the source of your problem.

Device Heartbeat

Does the device send a DC2 or DLE response (heartbeat)? If so, try opening a COM port in Hyperterminal using basic 9600-8-N-1 settings and see if you get anything. Is there any STX, SYN, ETX commands sent back when you try to send a command? I'm not sure on the command-set implementation for the hardware you are trying to communicate with.

Checking COM Port Operation

Have you checked if your COM port is working properly? You can create a loopback by inserting a paperclip between pins 2 and 3 (TX and RX). This will loopback your COM port and in Hyper terminal you can open that COM port and type. The characters will echo back.

You can also use a piece of software called com0com to create virtual COM ports to test what you are sending before you actually send it.

It provides pairs of virtual COM ports that are linked via a nullmodem connetion. You can then use your favorite terminal application or whatever you like to send data to one COM port and recieve from the other one

Without anymore specific information, we'll be grasping at straws.

0A0D
+1  A: 

You're allocating and sending 6 bytes but initializing only the first 5.

Also, according to what you write, you're doing everything correctly, exactly as the demo program. Right? Then I can see only one solution: The device is pulling your leg!

Seriously: If one program works and your doesn't, there MUST be some difference.

Serge - appTranslator
I'd say the "allocating 6 bytes but initializing only 5" is the first problem to fix; the device might be interpreting all six bytes as the message and deciding it's invalid.
qid
Sure it doesn't, it's given `msg.Length`, which will be 6, so it'll send 6 bytes regardless of their values.
Pavel Minaev
EOT will indicate end of message.. rest is garbage. Any device worth its weight will ignore the rest.
0A0D
+1  A: 

By far the most common mistake is forgetting to turn on the hardware handshake signals. The device won't send anything if it thinks the host is turned off. Make sure you set the SerialPort.DtrEnable and RtsEnable properties to true.

As mentioned before, fix the array size. Although it probably won't help, the STX character ensures that junk is thrown away.

Check if basic hardware is okay with the Windows Hyperterminal applet. You can send the message you are trying to transmit by typing Ctrl+B, ABC, Ctrl+D

Hans Passant
A: 

As others have suggested, it is good idea to check what is being sent and received with some kind of terminal software. I have had success with "realterm"-- it has nice displays of ascii or binary/hex views.

Angelo
A: 

Since you are working with ASCII, another issue might be that you are not sending the correct line-terminating character. Some devices expect \r and others \n. Make sure you are setting that correctly.

Erich Mirabal
I see your point, but with EOT you don't need it.
0A0D
I'll take your word on that. I do know that some of the devices I've used required the character for their internal command parsing to work correctly.
Erich Mirabal
@Erich: I've seen it on swipe readers where they send a CR-LF for every swipe (which you parse to see end of line), but I have never had to send it to any device (receipt printers, ticket machines, hid card readers, etc). YMMV
0A0D
A: 

the device is a monitor. The code was only an example. there isn't a heartbeat just a response for a correct message sent or a error message. the settings are standard 9600-8-N-1. "paperclip between pins 2 and 3 (TX and RX)." yeah i know the cable works because its the same one used with the product software that works. and im getting back what i send. I have used a virtual com program and everything seems alright. "6 bytes but initializing only the first 5. " sorry typo.

the SerialPort.DtrEnable and RtsEnable properties to true. are on by default in c#. i have tried the hyper terminal and am not getting a response with that either. I have sent \r and \n with no luck.

UF