blah blah blah is here! blah blah » Close

up0down
link

I'm having trouble performing what should be a simple check to make sure a device is connected via serial cable. It hangs up on the "test1.Read..." and I can't work out what is wrong. If anyone is able to tell me the no-doubt obvious error I'm making I'd appreciate it!

Thanks in advance.



private void Start_Click(object sender, EventArgs e)
{
byte[] buff1 = new byte[256];
SerialPort test1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
test1.Open();

test1.WriteLine("\x01\n"); //Hex code to enable remote control
test1.WriteLine(":MODType?\n"); //code to request Modulation type
test1.Read(buff1, 0, (int)buff1.Length);

//Check to see if buff1 has a value
if (buff1.Length < 1)
{
//Do one thing
}
else
{
//Do something else
}

last answered one year ago

1 answers

up0down
link

i'm not involved in serial port coding that much, but what i can tell from your code is that the message sent is not understood. the read holds the current thread you are in and wait for a message to be received but since there is no messsage received it holds ur application. what i noticed in ur code is that u r writing

test1.WriteLine("\x01\n"); //Hex code to enable remote control
test1.WriteLine(":MODType?\n"); //code to request Modulation type

where there is a \n in the end of the string and it is a writeline command so it would be \x01\n\n. try .Write instead of writeline maybe it will work with the hardware connected.

MickW
151

Thanks for the feedback Muster. I know the commands being sent are fine as the \x01 causes the remote light to come on, on my device which is happening. I can control the device fine by writing to it I just can't work out what I'm doing wrong on the receive side. All I want is to affirm connection so any sent request that returns a value is sufficient to prove that.

muster
1556

if it is activating some things in the device as u said and no message is being received then no message can be received because the device is not sending any from the :MODType? command, so check any other syntax or message that can be reliable for confirmation of the message other than the MODType command.

MickW
151

Yeah I've tried that with a number of different requests such as *IDN? and others but all give the same issue. I'm stuck!

MickW
151

Anyone able to offer further advice? Or even a complete alternative to checking the device is connected via serial cable?

vulpes
17279

As you haven't set the ReadTimeout property (it's infinite by default), the Read() method will block until 256 bytes are read which may never happen. So, I'd try setting the ReadTimeout property to a suitable number of milliseconds. I also agree with muster that, if you're going to specify '\n' in the command string, then you should use the Write() rather than the WriteLine() method as the latter is (in effect) sending an additional 'empty' command.

MickW
151

Thanks for that Vulpes. No change though - It just gives me a timeout exception with the ReadTimout property set. Should this work provided the request code (i.e. *IDN?) is correct???

vulpes
17279

If you're getting a timeout exception with the Read (as opposed to the ReadLine) method, then that's an indication that no bytes are being read. So, if you're sure the commands are correct, there must be something else wrong. Is it possible that you need to set the Handshake property - it's 'none' by default?

MickW
151

I have set the handshake property to "None". I'll try some different values for that then - thanks.

MickW
151

Sheesh - that's all it was "Handshake.RequestToSend" gave me a return. Thanks Vulpes - put it as an answer and I'll tick it!

vulpes
17279

Glad to hear it's working now. Don't worry about the points - I've got plenty already :)

Feedback