473,890 Members | 1,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

serial_port_RTS _pin_problem in C#

14 New Member
Hello,

I try to do a simple application in Winforms C# to read or write some data on serial port under win XP.I try to communicate with an industrial controller on RS485.So I have an converter RS232- RS485 The signal who does the converter control is RTS (switch RX or TX for RS485 half duplex)
My communication protocol is a version of old BitBus by Intel. When I want to sent something(eg. Address, command) I have to set the RTS line like:
//here the serial port is defined with baud rate, parity, etc.
byte[] command = new byte[1];
command[0] = 0x03;
port.Open();
port.RtsEnable = true;
port.Write(comm and, 0, 1);
port.RtsEnable = false;
// here shold begin to receive dataÖ
Now commes my problem: I need immediatley after I set RTS line to send data.
I looked with an digital scope to signals and I have an aprox 7mS time delay between the switching of RTS line and the actual data sending (write fuction).
Actually Iím trying to replace with the C# program an old driver writen in x86 ASM (witch cannot be runned under XP).Under the DOS driver I donít have any delay and because of this my C# communication is not working. How to syncronise RTS line with the write to port data in C#??
Any suggestions will be greatly appreciated.

Thanks in advance and all the best
Svcc.
Sep 10 '07
27 11562
rodrigofsilva
5 New Member
Dear,

the application usually works, when I open first in the hyperterminal and closed.

Else makes this procedure the application doesn't work.

Did anybody already go by this problem?

Thanks
Oct 9 '07 #21
Plater
7,872 Recognized Expert Expert
Only ONE program can use a com port at a time.
Dear,

the application usually works, when I open first in the hyperterminal and closed.

Else makes this procedure the application doesn't work.

Did anybody already go by this problem?

Thanks
Oct 9 '07 #22
rodrigofsilva
5 New Member
Hi Platter,

I think I didn't go clear

However if me don't open the hyperterminal and to close him, my application doesn't work.

I am accessing a Switch Cisco for administration.

To separate Switch and to call again and to try to access direct of the application it doesn't work.

The application opens the door usually but doesn't send the commands. In case opens Hyperteminal and close. And to open the application again he usually works.


it follows the code below:

if (m_comPort != null)
{
if (m_comPort.IsOp en)
{
m_comPort.Close ();
}
}
else
{
m_comPort = new SerialPort();
}

m_comPort.DataR eceived += new SerialDataRecei vedEventHandler (port_DataRecei ved);

/// Port Name
m_comPort.PortN ame = comPortSwitch.S electedItem.ToS tring(); ;

m_comPort.BaudR ate = 9600;

m_comPort.StopB its = StopBits.One;

m_comPort.Parit y = Parity.None;

m_comPort.DataB its = 8;

m_comPort.Hands hake = System.IO.Ports .Handshake.Requ estToSend;

m_comPort.Open( );

m_comPort.DtrEn able = true;

m_comPort.Write (txtCommands.Te xt);

m_comPort.Write (new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3);

m_comPort.Close ();

will it be that it lacks some thing?


Thanks
Oct 9 '07 #23
Plater
7,872 Recognized Expert Expert
If hyperterminal is "connected" on a COM port, it has it open and your program isn't allowed to use it until you "disconnect " (close) the port in hyperterminal
Oct 9 '07 #24
rodrigofsilva
5 New Member
everything well,

the hyperterminal is already disconnected.

But for my application to get to send commands for the equipment.

I need to enter in Hyperteminal and soon soon afterwards to close.

Then to enter again in the application. It seems that when I enter in Hyperterminal he enables some thing
Oct 9 '07 #25
svcc
14 New Member
I am pretty sure you would have to send a SERIOUSLY huge amount of data for it to be backedup and still have bytes to write.
Or it could be something about auto-flushing on the stream, I'm not sure.

I did recently learn that System.Windows. Forms.Timer's -> Tick() event doesn't have priority over things and was not getting called at correct times, so if you were using it to gather data as I hinted at, try one of the other time objects.
Hi Plater ,

Thanks for replying,
Regarding the BytesToWrite I felt the need for some way to know precise when write operation finishes and I explain you why.
For example I have a button function and inside I have to write to my controller a sequence of commands. I noticed that I have to press the button on GUI many times until the controller executes. I made further experiments and I discovered that all this stuff is related to my 9 bit communication system using parity bit in order to tell the controller that I send an address.
So if I have the following snippet:
private void button1_Click(o bject sender, EventArgs e)
{
port.Parity = Parity.Mark;
Thread.Sleep(10 );
port.write(addr ess byte,0,count);
Thread.Sleep(10 );
port.Parity = Parity.Space;
port.write(comm and bytes,0, count);
port.write(data bytes,0,count);

}

Without the Treed.Sleep() timings I have the situation described above (no stable communication) It seems for me that when switching parity you need some extra time, otherwise you will get errors. First I was thinking that I can use BytesToWrite to be sure when to change parity. I think itís also possible that this could generate the parity issue described in my earlier posts. Because Treed.Sleep() is not so precise do you know a better way to implement a precise timer in C#?
Regarding the point with System.Windows. Forms.Timer's what kind of timer function in C# you recommend for gathering data?
Oct 10 '07 #26
Plater
7,872 Recognized Expert Expert
Possibly a whole other thread entirely, I'm not sure as other things came up and the project has been abandoned for now.

It's possible that the reason you have to wait is something inherent to the timings involved at the hardware level. Like to send a single character on rs232 there's a preamble then the character and sometimes a deliminator sequence of 0s and 1s. It's possible that sending the parity bit change at the wrong time causes signals of 0s and 1s to get mis-interpreted.
I'm just guessing, and you're outside my realm of knowledge on this stuff, so I'm not sure what else to offer.
Oct 10 '07 #27
svcc
14 New Member
Possibly a whole other thread entirely, I'm not sure as other things came up and the project has been abandoned for now.

It's possible that the reason you have to wait is something inherent to the timings involved at the hardware level. Like to send a single character on rs232 there's a preamble then the character and sometimes a deliminator sequence of 0s and 1s. It's possible that sending the parity bit change at the wrong time causes signals of 0s and 1s to get mis-interpreted.
I'm just guessing, and you're outside my realm of knowledge on this stuff, so I'm not sure what else to offer.
Hi Plater

I succeded to solve my problem. I found a very good article abou serial port in .NET and I want to share it http://www.innovatic.d k/knowledg/SerialCOM/SerialCOM.htm
My last problem was related to the 11 bit communication and with parity
changing. I found the explanation reading this article:
"Microsoft has put an 8 bit wide buffer on top of the 11-bit receiver FIFO and therefore destroyed the possibility for a precise Break, 9th bit and error detection. "

All the best,

SVCC
Dec 7 '07 #28

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.