Professional Documents
Culture Documents
Cc thng s c bn
Tc : 300, 1200, 2400, 9600, 19200 .. . Ch khi thit lp tc ca cng Comm v thit b ghp ni phi bng nhau, nu khng dn n d liu tr v s b sai lch. b. Bt d liu ( Data bits) 7 ( ASCII), 8 ( 1 byte) c. Phng thc kim tra li ( Parity) Cc phng thc bao gm none (N), odd (O), even (E), mark (M), hoc space (S). d. Stop bits =1 e. Lung iu khin ( Flow Control ) f. Bt tay ( Handshaking )
a.
Tool v v d
Visual Basic 6.0
Gii thiu
Visual Basic 6.0 l ngn ng lp trnh c bn v d dng nht tip cn vic lp trnh giao tip truyn thng qua cng ni tip. Cho nn bi ny s hng dn chi tit cch to 1 phn mm c bn c th giao tip c vi cc thit b khc qua cng Comm
Visual C++ 6
Gii thiu Lp trnh
1. S dng class CSerialPort 2. Cu hnh cng 3. Gi v nhn d liu 4. X l s kin
DotNET 2003 ( C# )
Gii thiu s qua v ngn ng
DotNET l bc ci tin ng n v ngn ng lp trnh do Microsoft cung cp. Trong phin bn ny (chy di nn Framework 1.1) khng c sn lp (managed class) thc hin giao tip vi cng Comm. Chnh v th c nhiu hng th 3 cung cp di dng ActiveX cc module giao tip vi cng Comm, tuy nhin vi gi thnh cha hp l cho sinh vin Vit Nam. Chnh v th ti xin gii thiu mt phng php t tn km hn l s dng li ActiveX MS Communications Control ca b Visual Studio 6.0 . Khi Add component vo project, .NET s s dng Platform Invoke ( P/Invoke) chuyn .ocx thnh unmanaged class.
ngha cc thng s
8. com.CommPort a. Gn hoc ly tn cng ni tip ( VD COM1,COM2)
9. com.PortOpen a. ng hoc m cng ni tip 10. com.Rthreshold a. Thit lp s k t ti thiu t cng ni tip s kin OnComm thc hin (quan trng) 11. com.InputMode a. Xc nh kiu d liu nhn v l text hoc binary, s dng hng MSCommLib.InputModeConstants 12. com.Settings a. Thit lp baud,d,p,s trong baud = baud rate, p = parity, d = # data bits, and s = # stop bits .V d : 9600,8,n,1 13. com.Handshaking a. Xc nh kiu bt tay s dng hng MSCommLib.HandshakeConstants 14. com.InBufferCount a. Tr v s k t trong b m nhn ( receive buffer) 15. com.Input a. Ly v xa b m nhn, tr v l chui nu InputMode l text hoc tr v l mng byte nu InputMode l binary 16. com.Output a. Gi d liu ra cng Comm 17. com.CommEvent
Tr v cc thng bo ca i tng MSComm thng qua cc hng MSCommLib.CommEventConstants, MSCommLib.ErrorConstants, MSCommLib.OnCommConstants 18. com.NullDiscard a. Nu thit lp = true th cng ni tip s b qua k t null ( 0x00) v ngc li . 19. com.InputLen a. Xc nh s k t khi lnh Input c t b m nhn, mc nh 0 l ly tt c. 20. OnComm Event a. S kin xy ra khi c d liu trong b m nhn hoc gi, ch s kin ch xy ra khi thit lp Rthreshod >0 hoc Sthreshod >0. b. V d
a.
public MyForm() { InitializeComponents(); // Initialize Form Components com.RThreshold = 1; // S kin OnComm xy ra khi d liu trong b m nhn
l 1
com.OnComm += new System.EventHandler(this.On Comm); // Gn handler cho s kin } private void OnComm(object sender, EventArgs e){ if (com.InBufferCount > 0) ProcessData((string) com.Input); if (com.CommEvent == MSCommLib.OnCommConstants.c omEvCTS) Console.WriteLine(" CTS Line Changed");
Gi v nhn d liu
21. Gi d liu ra cng Comm a. Theo nh dng text i. V d
com.Output=@S01# b. Theo nh dng binary i. V d com.Output = new byte[] {0, 0x41, (byte) 'A', 255}; 22. Nhn d liu cng Comm a. Ch i. D liu nhn v t cng Comm khng d dng qun l. V d : nhn c chui Viet Nam th cng Comm c th nhn thnh nhiu gi nh Vie t N v am chng hn. Do ta phi s dng cc phng php ph hp nhn d liu.Sau y l 3 cch ph bin b. Nhn d liu theo frame ( c bit start v bit stop ) i. Vi d liu c ng khung trc khi gi th ch khi no c bit start v bit stop th mi xc nh u l 1 frame. ii. V d: Ta quy nh bit start @ bit stop #.on code sau minh ha chi tit
using System.Text.RegularExpressions; private string ComBuffer = ""; private void OnComm(object sender,
EventArgs e { // Nhn d liu t cng Comm ComBuffer += (string) com.Input; // To mt regular expression Regex r = new Regex("@.*?#"); // Cycle through the matches for (Match m = r.Match(ComBuffer); m.Success; m = m.NextMatch()) { Console.WriteLine(m.Value); // Xa chui dc khi buffer ComBuffer = ComBuffer.Replace(m.Value, ""); } }
c. Nhn d liu theo khong thi gian nht nh i. Nu xc nh chnh xc khong thi gian cho tng gi d liu th c th p dng phng php ny
ii. V d:
using System.Timers; private string ComBuffer = ""; private Timer tmrWaitData = new Timer(); private void ExtraInitCode() { // Khong thi gian cho tng gi d liu tmrWaitData.Interval = 1000; // S kin khi thi gian thay i tmrWaitData.Elapsed += new ElapsedEventHandler(tmrWaitData_Elapsed); } private void OnComm(object sender, EventArgs e { // Kim tra d liu trong b m if (com.InBufferCount > 0) { // Nhn d liu vo bin ComBuffer += (string) com.Input; tmrWaitData.Stop(); // Reset timer tmrWaitData.Start(); }
} private void tmrWaitData_Elapsed(object sender, ElapsedEventArgs e) { Console.WriteLine("Data Packet: " + ComBuffer tmrWaitData.Stop(); }
d. Nhn d liu theo kch thc nht nh i. Thit lp Rthreshod bng kch thc ca gi d liu.
i tng SerialPort
Java
Gii thiu
Khc vi cc ngn ng trn u do hng Microsoft cung cp, Java c pht trin bi hng Sun , Java vi th mch trong mi trng mng.. giao tip vi cc thit b nhng cng nh thc hin truyn thng hng Sun cung cp gi m rng Java Communications 3.0 API (javax.comm)
Interface
CommDriver CommPortOwnersh ipListener ParallePortEventLis tener SerialPortEventList ener Driver cho truyn thng S kin cho truyn thng c bn S kin cho truyn thng qua cng // S kin cho truyn thng qua cng ni tip
Class
CommPort CommPortIdentifier ParallePort ParallePortEvent SerialPort SerialPortEvent Cng truyn thng Qun l cng truyn thng Cng song song S kin cho cng song song Cng ni tip S kin cho cng ni tip
Exception
NoSuchPortExcepti Xy ra khi khng tm thy on cng xc nh PortInUseException Xy ra khi cng c s dng UnSupportedComm Driver khng h tr phng OpereationExceptio thc ny ( function) n
Lp trnh
23. Cu hnh cng
private SerialPort
sPort;
sPort.portName = 1; sPort.baudRate = 9600; sPort.flowControlIn = SerialPort.FLOWCONTROL_NONE; sPort.flowControlOut = SerialPort.FLOWCONTROL_NONE; sPort.databits = SerialPort.DATABITS_8; sPort.stopbits = SerialPort.STOPBITS_1; sPort.parity = SerialPort.PARITY_NONE;
24.
ng m cng
import javax.comm.*; private CommPortIdentifier portId; private SerialPort sPort; //To i tng cng ni tip try { portId = CommPortIdentifier.getPortIdentifier(1);
} catch (NoSuchPortException e) {throw new SerialConnectionException(e.getMessage()); } //M cng ni tip try { sPort = (SerialPort) portId.open("BKSerial", 30000); } catch (PortInUseException e) { throw new SerialConnectionException(e.getMessage()); } // Thit lp notifyOnDataAvailable cho php s kin khi nhn sPort.notifyOnDataAvailable(true); // Thit lp notifyOnBreakInterrup cho php s kin khi dng sPort.notifyOnBreakInterrupt(true); // Thit lp timeout nhn try { sPort.enableReceiveTimeout(30); } catch (UnsupportedCommOperationException e) {} //ng cng
sPort.close();
25.
Gi v nhn
private OutputStream os; private InputStream is; os = sPort.getOutputStream(); is = sPort.getInputStream();
26.
public void serialEvent(SerialPortEvent e) { StringBuffer inputBuffer = new StringBuffer(); int newData = 0; switch (e.getEventType()) { case SerialPortEvent.DATA_AVAILABLE: while (newData != -1) {
newData = is.read(); if (newData == -1) { break; } if ('\r' == (char) newData) { inputBuffer.append('\n'); } else { inputBuffer.append((char) newData); } } catch (IOException ex) { System.err.println(ex); return; } case SerialPortEvent.BI: //BREAK RECEIVED } }
try {