Why is this PortName deemed nonexistent?

68 views Asked by At

I'm trying to refactor/update some legacy serial comm code. I've got this:

private SerialPort cereal;
private String receivedData;

private FileXferLegacy()
{
    cereal = new SerialPort("COM1", 9600);
    cereal.PortName = "7727";
    cereal.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    // Is this just as well, as the elided part is grayed out above?: cereal.DataReceived += port_DataReceived;
}

...but am getting the err msg "The port '7727:' does not exist" when I try to connect and send a ping:

public void SendDataContentsAsXML(string destinationPath, string XMLData)
{
    byte[] stuff;
    ExceptionLoggingService.Instance.WriteLog("Reached FileXferLegacy.SendDataContentsAsXML"); // <= This is written to the log file
    cereal.Open();
    stuff = System.Text.UTF8Encoding.UTF8.GetBytes("PING" + "\n");
    cereal.Write(stuff, 0, stuff.Length);
    stuff = System.Text.UTF8Encoding.UTF8.GetBytes(XMLData + "\n");
    cereal.Write(stuff, 0, stuff.Length);
}

7727 is the same port that is successfully used in the legacy app.

I do see that there is a colon appended, and wonder if that is the problem - why is it seeing "7727:" instead of plain old "7727", and how can I disabuse it of the notion of having an appended colon if that is indeed a problem?

1

There are 1 answers

2
Reticulated Spline On BEST ANSWER

Because PortName refers to the serial port name, not a port number. In your code you're creating your SerialPort object

cereal = new SerialPort("COM1", 9600);

So COM1 is already being assigned to PortName. Your next statement is just overriding it unnecessarily and incorrectly.