2

I have a similar problem to the following link. How to change baud rate without closing the connection?. But i use C# in stead of java. I am beginning with baud rate 300. Then after a hand shake i have to change my baud rate to 4800. I send a command to the device to do this. And I change my serialport's baud rate. After that i can never continue the communication with the device. (If i stay in 300 baud and send command to the device for 300 baud i can continue the communication)

I have a third party software which also reads from this device. I am listening the port when that software is running. It sends the same commands that i am sending to the device. It sends command to set 4800 baud to the device and can get the answer from the device. It does not close or reopen the port during baud change. I have to do the same thing. But when i send command to change to 4800 baud i get no answer from the device ever.

I am waiting for your answers. Thank you, FERDA

This is the output from serial port monitoring program Port opened by process "ReadY.vshost.exe" (PID: 2608)

01 42 30 03 71 .B0.q

Answer: 09.08.2011 10:55:40.81864 (+0.2656 seconds)

06 .

Request: 09.08.2011 10:55:41.06864 (+1.2500 seconds)

2F 3F 31 31 39 39 39 30 30 34 21 0D 0A /?11999004!..

Answer: 09.08.2011 10:55:41.58464 (+0.5156 seconds)

2F 53 54 52 34 5F 42 53 4D 31 33 31 0D 0A /STR4_BSM131..

Request: 09.08.2011 10:55:42.31864 (+0.2969 seconds)

06 30 34 31 0D 0A .041..

Community
  • 1
  • 1
Ferda-Ozdemir-Sonmez
  • 674
  • 2
  • 13
  • 38
  • 1
    It would be much easier to help if you show us what commands you are attempting to send. – Kaos Aug 09 '11 at 07:09
  • use a packet tracer and see the difference? – Polity Aug 09 '11 at 07:15
  • Have you tried using `SerialPort.DiscardInBuffer` ? please post some code you are using – V4Vendetta Aug 09 '11 at 07:29
  • I am using a serial port monitoring program to find a difference. I will add the commands ...Thank you – Ferda-Ozdemir-Sonmez Aug 09 '11 at 07:34
  • writer.Write(amessage);//this is the ACK message to the device that tells to set baud to 4800 the other software also sends this writer.Flush(); serialPort.Close(); if (serialPort != null) serialPort.BaudRate = 4800; System.Threading.Thread.Sleep(3000); serialPort.Open(); if (serialPort != null) serialPort.BaudRate = 4800; reader = new StreamReader((serialPort).BaseStream); writer = new StreamWriter((serialPort).BaseStream); – Ferda-Ozdemir-Sonmez Aug 09 '11 at 07:38
  • The other software does not close the port. – Ferda-Ozdemir-Sonmez Aug 09 '11 at 07:39
  • I am setting baud rate 2 times before and after reopening cause i am not sure which one will work? When should i use discardinbuffer? – Ferda-Ozdemir-Sonmez Aug 09 '11 at 07:40
  • The other software does not wait 3 seconds after sending the command. I tried both waiting and not waiting. But in both way i couldn't get any answer from device. – Ferda-Ozdemir-Sonmez Aug 09 '11 at 07:41
  • Hi V4Vendetta, I am using DiscardInBuffer when i first open the port in the beggining of my program. When should i use it next when changing baud rate. After i send ACK to the device I have to wait for some answers so using DiscardInbuffer there seemed wrong to me. – Ferda-Ozdemir-Sonmez Aug 09 '11 at 08:19
  • Is your serial port a USB to serial adapter? If so try starting your app at 4800 baud. Then tell the device (what is the device?) to switch to 4800 when needed. – dbasnett Aug 09 '11 at 12:20
  • Hi DBasNett, I tried to start my application with 4800 baud already. But I can never communicate with the device ( an electric meter) by that way. I ever tried starting with 600 baud, 1200 baud cause i thought maybe it is difficult to change 300 to 4800 so i should make the difference smaller. But i could only communicate if i start with 300. – Ferda-Ozdemir-Sonmez Aug 10 '11 at 06:37

2 Answers2

2

In my experience baud rate switches require closing and reopening the serial port hardware: I would suggest trying that. Send the baud rate switch command then close the port, set the baud rate and then re-open the port. Opening/closing the port should be transparent to the device on the other side (unless you are using the CTS/RTS/DTR/DSR lines).

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Hi Femi, I tried closing setting baud rate and reopening already. Initial baud rate is 300 baud. If i send switch command for 300 baud ( when it is already 300) I can get answer from the device. But when i send command to switch to 4800 I can not. – Ferda-Ozdemir-Sonmez Aug 09 '11 at 08:06
  • Hi again Femi, do you have another idea to solve my problem. Thank you – Ferda-Ozdemir-Sonmez Aug 12 '11 at 15:05
1

To change baud rate of a device, follow these steps. eg: To Start communication with a baud rate of 1200.

  1. Send data command for changing the baud rate to 1200.
  2. Receive the acknowledgement from the device.
  3. If the ACK is proper, set the Port.BaudRate=1200.

Next you want to change the baud rate to 4800, then follow these steps

  1. Send data command for changing the baud rate to 4800.
  2. Receive the acknowledgement from the device.
  3. If the ACK is proper, set the Port.BaudRate=4800.

NOTE: Change the Port.BaudRate after receving the ACK from the controller device. The communciation will be started with the previously set BaudRate.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
SeemaSR
  • 11
  • 3