Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

Serial Port - problem with Write()

(0) ShareShare
ReportReport
Posted on by

Hello All, 

I trying send text via serial port and I have a problem with Write function. When this function is running, application stop and I must close app from task menager.

AX2012 is running on server, I connect via RDP with ports inherit.

What is wrong?

My code:

static void Job2(Args _args)
{
System.IO.Ports.SerialPort SerialPort;

SerialPort = new System.IO.Ports.SerialPort();

SerialPort.set_PortName("COM4");
SerialPort.set_BaudRate(9600);
SerialPort.set_Parity(System.IO.Ports.Parity::None);
SerialPort.set_DataBits(8);
SerialPort.set_StopBits(System.IO.Ports.StopBits::One);
SerialPort.Open();

SerialPort.Write("something");

}

  • Martin Dráb Profile Picture
    Martin Dráb 230,476 Most Valuable Professional on at
    RE: Serial Port - problem with Write()

    Then the code seems to be fine and it doesn't need replacing.

    The next step is running it from an AX client installed on the local machine. I assume it'll work and the problem is your attempt of doing it via RDP. But please test this hypothesis.

  • Andriej Profile Picture
    Andriej on at
    RE: Serial Port - problem with Write()

    I am sorry, yes, I tested the communication outside the AX2012 on a local computer and it worked without a problem.

    I was sending a message from the serial port console, which my device received.

  • Martin Dráb Profile Picture
    Martin Dráb 230,476 Most Valuable Professional on at
    RE: Serial Port - problem with Write()

    You're already using a DLL file (System.IO.Ports.dll). You surely can use a different library, but it's not clear in which respect it should be different.

    Have you considered my suggest to test the logic in a simple console application before trying to integrate it to AX 2012 and nefore trying to call the integrated solution via RDP? If you test many things at once, than you don't know which one failed.

  • Andriej Profile Picture
    Andriej on at
    RE: Serial Port - problem with Write()

    Maybe I had the wrong approach to this case from the beginning.

    I want send some information form AX2012 via serial port to my device. How it should works?

    Maybe need I some dll library?

  • Martin Dráb Profile Picture
    Martin Dráb 230,476 Most Valuable Professional on at
    RE: Serial Port - problem with Write()

    Okay, then you seem to be using an USB to COM bridge.

    Please try the suggestion to test commnication from a simple console application, before trying to run it from AX and through a remote desktop connection. Also, check once more that you're connection to the right port.

  • Andriej Profile Picture
    Andriej on at
    RE: Serial Port - problem with Write()

    I have adruino device with other addins. I connected it only via USB, but now I think that maybe i should use RS232 to USB conventer?

    I will try to use AX client on my computer, as you say.

  • Martin Dráb Profile Picture
    Martin Dráb 230,476 Most Valuable Professional on at
    RE: Serial Port - problem with Write()

    What device do you have in the USB port? Some kind of USB-to-SerialPort converter?

    What if you run your from the local machine? Maybe you can simply install AX client there, or create a simple console application and try it from both machines.

  • Andriej Profile Picture
    Andriej on at
    RE: Serial Port - problem with Write()

    Yes, client application stops.

    Debugging show nothing... App stops on 14 line (in your code).

    pastedimage1611590573147v2.png

    Port inherit - in RDP connection:

    pastedimage1611590370287v1.png

    I have device connect to USB on my computer and connect to desktop with AX via RDP connection.

  • Martin Dráb Profile Picture
    Martin Dráb 230,476 Most Valuable Professional on at
    RE: Serial Port - problem with Write()

    Which applications stops? The client? Jobs are normally executed on client, therefore I guess that's the case.

    What did you find when you debugged your code? Where does it gets stuck?

    What do you mean by the remark about "ports inherit"? That the serial port is on your local machine, not the machine where you're running your code?

    By the way, please always use Insert > Insert Code (in the rich-formatting view) to paste source code. It preserves indentation, making code easier to read.

    This is your code, but formatted and with handling of CLR exceptions (otherwise CLR exceptions are not shown in UI):

    System.IO.Ports.SerialPort serialPort;
    
    try
    {
    	serialPort = new System.IO.Ports.SerialPort();
    
    	serialPort.set_PortName("COM4");
    	serialPort.set_BaudRate(9600);
    	serialPort.set_Parity(System.IO.Ports.Parity::None);
    	serialPort.set_DataBits(8);
    	serialPort.set_StopBits(System.IO.Ports.StopBits::One);
    	serialPort.Open();
    
    	serialPort.Write("something");
    }
    catch (Exception::CLRError)
    {
    	throw error(AifUtil::getClrErrorMessage());
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,784 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,476 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans