Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Releji i visual basic

[es] :: Visual Basic 6 :: Releji i visual basic

Strane: 1 2

[ Pregleda: 5333 | Odgovora: 26 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

galahad
Slobodan Todorov
Radio-televizija Vojvodine,
Jack-Of-All-IT-Trades, Web redakcija
Novi Sad

Član broj: 20613
Poruke: 146
*.adsl-2.sezampro.yu.

Jabber: galahad@elitesecurity.org
ICQ: 52020296
Sajt: www.todorowww.net


+4 Profil

icon Re: Releji i visual basic10.04.2008. u 14:10 - pre 195 meseci
Ok, vidim nateraliste da to radi... Sorry shto nisam naleteo ranije...

Imam malo iskustva sa radom sa hardverom na COm portu, i koliko sam skontao, to je neki standard u komunikaciji...

Kada se neshto shalje, potrebno je da uredjaj zna odakle pochinje da chita, i dokle je jedna komanda... Postoje razlichite varijante, ali recimo, ono sa chim sam ja radio je bilo u obliku Chr$(3) [komanda] Chr$(2)

ASCII 3 oznachava pochetak transmisije, a ASCII 2 oznachava kraj... Sve izmedju je komandna kobasica (tako smo zbali to :))

E, sad, ovaj uredjaj prihvata komande duzhine 4 bajta, i bez ASCII 2... Ok... Uzmimo za primer sledetju liniju:
Code:
If led = "5" Then send$ = Chr$(3) + Chr$(1) + Chr$(16) + Chr$(18): GoTo prikaz


Chr$(3) oznachava pochetak komande
Chr$(1) verovatno oznachava komandu UPALI
Chr$(16) mu dodje LED 5
E sad, Chr$(18) je cheksum, koji se rachuna na klasichan nachin XOR-ovanjem prva 3 bajta komande... Tako kontroler diodica, zna da je komanda ispravna Dakle 3 XOR 1 XOR 16 = 18

E sad, kad hotjesh da palish neke diode, idesh po ovom obrascu:
LED1 = 1
LED2 = 2
LED3 = 4
LED4 = 8
LED5 = 16
LED6 = 32
LED7 = 64
LED8 = 128

E sad, recimo hotjesh diode 2, 3 i 7 upaljene... Nema neke teshke filozofije, to uradish tako shto postavljash 2, 3 i 7 bit na 1, ovako:
Code:

Dim upali as byte
upali = 2 Or 4 Or 64

Posle taj bajt dodash onoj kobasici, i voila, radi... Mozhesh da chuvash trenutno stanje, pa da palish i gasish diode po potrebi... Menjanje stanja bita radish sa XOR... Tako da bi:
Code:

upali = upali XOR 4

ugasilo LED3

Mislim da tje ovo da ti pomogne sa dilemom... Nema IF-ova, nema njackanja, samo prosta manipulacija bitovima... A uz to, i kratji i pregledniji kod :)

Vichi ako negde zapne...
- SKRati link - JaZaKraljevo.rs -

"I have never let my schooling interfere with my education." - Mark Twain
 
Odgovor na temu

stefanpn
Total anonymous
Programer

Član broj: 172809
Poruke: 509
77.46.182.*



+1 Profil

icon Re: Releji i visual basic10.04.2008. u 14:25 - pre 195 meseci
@Galahad
Mislim da smo to vec utvrdili

BTW: Mozda si trebao da procitas thread do kraja...

[Ovu poruku je menjao stefanpn dana 10.04.2008. u 15:42 GMT+1]
 
Odgovor na temu

galahad
Slobodan Todorov
Radio-televizija Vojvodine,
Jack-Of-All-IT-Trades, Web redakcija
Novi Sad

Član broj: 20613
Poruke: 146
*.adsl-2.sezampro.yu.

Jabber: galahad@elitesecurity.org
ICQ: 52020296
Sajt: www.todorowww.net


+4 Profil

icon Re: Releji i visual basic10.04.2008. u 15:48 - pre 195 meseci
@stefanpn
Prochitao sam do kraja celu temu, ali mi se chinilo da ste reshili to vishe na silu nego po logici... Izvinjavam se ako sam pogreshno protumachio... A mozhda tje josh nekom biti interesantno da vidi kako se manipulishe sa bitovima...

A i chovek je lepo pitao kako da reshi kad ima text boxove i vrednosti u njima, a ja sam napisao kako da to uradi bez iIF-ova, nego samo da te vrednosti prebaci u broj, i odradi u jednom redu kod...
- SKRati link - JaZaKraljevo.rs -

"I have never let my schooling interfere with my education." - Mark Twain
 
Odgovor na temu

Kiter

Član broj: 128170
Poruke: 25
89.143.240.*



Profil

icon Re: Releji i visual basic10.04.2008. u 17:33 - pre 195 meseci
Ok, hvala svima:) Budem sada nesto isprobao, pa cu se javiti ako nest ne bude jasno. Pozdrav!
 
Odgovor na temu

Kiter

Član broj: 128170
Poruke: 25
80.80.49.*



Profil

icon Re: Releji i visual basic11.04.2008. u 09:12 - pre 195 meseci
Galahad, sad isprobavam ovo kako si ti opisao, ali ne znam gdje dodati sad taj byte 'upali'. Kojoj 'kobasici' to moram dodati?
 
Odgovor na temu

stefanpn
Total anonymous
Programer

Član broj: 172809
Poruke: 509
212.200.207.*



+1 Profil

icon Re: Releji i visual basic11.04.2008. u 14:01 - pre 195 meseci
1. Ne verujem da jednostavni uredjaj kao ovaj ima u sebi ASCII tabelu, tako da se CHR$(3) koristi zbog toga sto MSCOMM kontrola podrzava samo ANSI slanje podataka. Uredjaj to cita kao vrednost 3.

2. Nije 2 za prekid komunikacije, jer se uvek salje po 4 bajta. Kod fiksne duzine nema razloga da postoji vrednost za prekid prenosa.

Za checksum se slazem.

Citat:
E, sad, ovaj uredjaj prihvata komande duzhine 4 bajta, i bez ASCII 2... Ok... Uzmimo za primer sledetju liniju


Za ovo se ne slazem jer uredjaj prihvata i bajtove sa vrednoscu 2.

Sem toga koliko sam ja shvatio taj uredjaj ima samo komandu za gasenje i komandu za promenu stanja koja kao atribut ima stanje na koje se menja. Jebiga ipak je to 8 segmentni led display, vise od toga mu i ne treba.

Znaci vrednost koju saljes kao treci bajt je zbir vrednosti bitova, gde ti bitovi vrede:

Code:

128  64  32  16  8   4   2   1
 x   x   x   x   x   x   x   x


znaci saljes: 3, 1, treci_bajt, 3 xor 1 xor treci_bajt

A koliko sam ja shvatio, ovo je ta kobasica: "1, treci_bajt, 3 xor 1 xor treci_bajt"
 
Odgovor na temu

rgdrajko
Beograd

Član broj: 117734
Poruke: 710
80.93.249.*



+3 Profil

icon Re: Releji i visual basic12.04.2008. u 19:02 - pre 195 meseci
Preporucujem da pogledas http://www.bitwisemag.com/copy/vb/vb1.html. Tamo imas sve detaljno u vezi com porta.

Code:
Communicating - the basics 

Serial communication is a heart quite a simple idea. There are three pins that are important. Data is transmitted over one pin (Transmit Data or TXD for short) and received over another pin (Receive Data or RXD). The data is sent sequentially as a series of on-off pulses down the wire, so a byte will take eight bits or pulses to transmit. In fact, there are one or two other bits needed as well for synchronisation and error checking, but that’s the basics. The third wire that’s needed is the Ground wire (GND) – these are electrical signals and so need a return path. While you can get away with just three wires, it’s often useful to have other control signals to make things easier for the software. There are six other signals: Ring Indicator (RI) is set by a modem when it detects an incoming call. Carrier Detect (CD) is again set by the modem when it detects another modem as the originator of the incoming call. For a null-modem, RI and CD can are ignored.

This leaves four other signals which come in pairs. Request To Send (RTS) and Clear To Send (CTS) are used by the PC to signal that it is ready to write or read data respectively. Data Terminal Ready (DTR) and Data Set Ready (DSR) do a similar sort of job (there are actually finer details of what these signals should be used for – but that needn’t bother us here). So for our null-modem purposes there are three pairs of wires – and these need to be ‘crossed over’ for the two ports to communicate. That is, the RCD pin of one port must be connected to the TXD pin of the second and so on. This means that when COM1 transmits data (on TXD) COM2 will receive data on RCD. The complete wiring between the two connectors looks like:COM1     COM2
RCD     2     TXD     3 
TXD     3     RCD     2 
DTR     4     DSR     6 
GND     5     GND     5 
DSR     6     DTR     4 
RTS     7     CTS     8 
CTS     8     RTS     7 


If you’re confident with a soldering iron you can get the parts separately and wire them up or if you’d rather leave it to an expert, get a ready made cable – though check out the type of ports of the back of your PC first. Older PCs tend to have one 25 pin port and one 9 pin port – all of the above wiring refers to the more modern 9-pin varieties.

OK, so assuming you’re equipped with a suitable null-modem cable and you’ve plugged it into the two ports what next? One way to check that everything is working OK is to use two HyperTerminal sessions, one connected to COM1 the other to COM2 with exactly the same speed settings. You should be able to type from one screen to the other with no trouble. Another way (more interesting, of course!) is to use a Visual Basic program.

I’ve written a simple program using mostly the default settings of the two communication ports to illustrate how events are communicated back to Visual Basic. In the program COMMS.VBP, there are two text boxes that display the input received on the respective port and two sets of buttons that toggle DTR and RTS. There’s also an Initialize button that opens the ports and sets off a polling loop. The code initially sets COM1 and COM2 up in a similar manner with both RTS and DTR off:
With MSComm1
    .DTREnable = False
    .RTSEnable = False
    .CommPort = 1
    .PortOpen = True
End With

Once set up the program just polls for input:
Do While MSComm1.PortOpen And MSComm2.PortOpen
    s1 = MSComm1.Input
    s2 = MSComm2.Input
    If s1 <> "" Then
        Port2.Text = Port2.Text & s1
    ElseIf s2 <> "" Then
        Port1.Text = Port1.Text & s2
    Else
        DoEvents
    End If
Loop

There are two things to notice about this simple loop. First, the loop condition tests to see if the ports are still open. If you don’t do this, you will get an annoying error when you close the program. For some reason, Visual Basic seems to close the ports before unloading the form. Secondly, the DoEvents keyword ‘releases’ the program to look for other things – such as a button click or a communication event. 

In the DTR (and RTS) toggle code, you’ll see this:
With MSComm1
    .PortOpen = False
    If .DTREnable = False Then
        .DTREnable = True
    Else
        .DTREnable = False
    End If
    .PortOpen = True
End With 

These communication events (DSR corresponding to DTR and CTS corresponding to RTS) events are detected in the OnComm event code:
Private Sub MSComm1_OnComm()
   EventLog.Text = EventLog.Text & "Port1: " _
       & CommEventText(MSComm1.CommEvent) & vbCrLf
End Sub 

and just written to the EventLog window.

All this looks pretty neat and simple but there’s a ‘gotcha’: to set a control signal (say, RTS) you have to close the port, set RTS and then reopen it. This is disastrous if you are communicating with some hardware that isn’t really a modem – you can lose data in the small interval while the port is closed.

The problem is that the operating system uses these control wires to signal to the modem that your program is ready (that’s the main use of DTR) and it uses the RTS/CTS wires to implement hardware flow control. Normally, you’d be quite happy with this – but not if you want to control your patent dog-walker (or train set) directly. I’ll look at this next month.

rgdrajko
 
Odgovor na temu

[es] :: Visual Basic 6 :: Releji i visual basic

Strane: 1 2

[ Pregleda: 5333 | Odgovora: 26 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.