0

I want to do serial port communication with a machine which uses RS232-USB ports. I am using serial port class. I am very new to the concept. In my first Machine interfacing I only had to do the serialport.readLine( to get the readings from the machine and there was no need to send ACK /NAK). but for the new machine interface the document says following things:

   The following is an example of the RA500 communication:

   Computer :<05h 31h 0dh>

   RA500 :1st line of information

   Computer :<06h 0dh>

   RA500 :2nd line of information

   Computer :<06h 0dh>

   RA500 :”EOF”<0dh>

What i understood from this is i have to write to comport before reading from it. this is what i am doing in my code:

ACK = "06 0d"; NAK = "15 0d"; str = "05 31 0d";
while (count <= 5)
            {
                rx = ComPortDataReceived(str);
                if (!string.IsNullOrEmpty(rx))
                {
                    str = ACK;
                    returnReading += rx;
                }
                else if (string.IsNullOrEmpty(rx)) str = NAK;
                count++;
            }

 private string ComPortDataReceived(string str)
    {
        string Rx = string.Empty;
        string exceptionMessage = string.Empty;
        try
        {

                byte[] bytes = str.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray();
                comPort.Write(bytes, 0, bytes.Length);
                Rx = comPort.ReadExisting();
                PEHRsLibrary.writeTextFile(DateTime.Now + " RxString :" + Rx);
                return Rx;
        }
 catch(Exception e){}

when i use this code i am receiving empty strings as responce. but if i use comPort.ReadExisting() only without using comPort.Write i am receving a string with all the readings but the probblem is it only gives one line of information and dosnt give 2nd or 3rd line readings.

when i try using comPort.ReadLine() after Write() I am getting Timeout exception.

i dont know what i am doing wrong in this case. I am writing ACk after receving 1st line but not receving 2nd line. Next thing i am gonna try is read() as byte and then convert it to string instead of using ReadExisting(). Any other way i can do this?

uday vichare
  • 61
  • 12
  • you can't expect a response straight after writing. It takes time before you get a response. – Keith Nicholas Apr 06 '17 at 05:32
  • I wrote a little bit http://stackoverflow.com/questions/15124132/serial-port-polling-and-data-handling/15124287#15124287 on how to handle serial comms. Though what you need is another layer on top which is a state machine to handle the protocol cleanly – Keith Nicholas Apr 06 '17 at 05:34
  • also, I do suggest you use https://github.com/genielabs/serialport-lib-dotnet/ instead of the build in comm port, it's much more robust. The built in one has a number of issues ( including crashing your entire program randomly with hot plugging ), there are many questions on SO about dealing with the problems – Keith Nicholas Apr 06 '17 at 05:37
  • @KeithNicholas Thanks for your reply. I will go through your links. and I didn't say i want response right away to my question. – uday vichare Apr 06 '17 at 06:14
  • This code is not close to what is required to implement this protocol, you'll have to throw it away. You'll have to stop using ReadExisting() first. Get ahead by setting the SerialPort.NewLine property to "\r", now you can use ReadLine() to read the response. And avoid using the DataReceived event, it is much easier to get it done correctly with a little worker thread. – Hans Passant Apr 06 '17 at 07:23
  • @HansPassant I have done testing with SerialPort.NewLine property assigned to the last character i was getting in the string but still it was giving the timeiut exception. I will try using "/r" though. thanks for the reply. I just wanted to confirm is this how you send ACK and NAK or any other message to Machine via serial port before getting reading? or is there any other way than using write() method? – uday vichare Apr 06 '17 at 09:12
  • I tried using SerialPort.NewLine = "Bas" where is ETB character and tried using comport.readLine() but still got the timeout exception. i will try using "/r". Is there any specific reason not to use readExsisting()? becuase it is giving me the reading atleast one line than not getting anything. – uday vichare Apr 06 '17 at 09:23

0 Answers0