NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

RS232
簡單來說是 用別人寫好的rs232.c , 所以你要include "rs232.h" //程式碼裡會看到
其實那個就是別人寫好了一堆function可以控制rs232,我們去call function就好了。
他網站有說明 http://www.teuniz.net/RS-232/
說明的英文都很簡單啦,最好是要看(邊做邊看就好了,用到的function都有說明,滿短的)

主程式是main.c 你可以複製程式碼然後用自己習慣名稱就好。
rs232.h和rs232.c要和你的程式碼放在同一個資料夾才能include到。

43行scanf("%d", & portNum ) ; 就是讀入port號碼。機測的時候你要問助教是接在哪個port。
下面一行又把他-1是因為。我們用的rs232.c所定義的port是從0開始算,但是電腦上是從1開始寫。
46行OpenComport 就是打開rs232的port 第一個參數就是剛剛的port number 第二個是baud rate,就是一秒可以傳幾bit,當作速度來看就是了。 用了這行就可以控制指定的rs232 port了(這邊的是用來發資料)。

然後29行的註解是錯的,把它弄掉XD

由於你要用同一個程式 收 發 資料,所以你必須至少要開一個thread來用,加上process本身,才能做到一收一發。
54行那邊就是呼叫thread開始執行了,那個thread會一直收資料(16行的PollComport就是,第一個是參數port號碼,跟上面發資料的差不多,第二個參數是buffer,收到的資料就在裡面,下面程式碼會去把它印出來,第三個是最大長度,可自訂~ 然後回傳的東西(n)是收到的長度,所以下面if n > 0 就是如果有收到東西,就一直印出來。if(buf[i] < 32) 是因為,rs232收資料如果有不可見字元,或是空的東西,好像都會變成一堆點('.'),所以<32表示ascii code < 32 的東西,下面就把它換成0(NULL),就不會印出一堆點了。
喔對你可以看到這整個是個無窮迴圈,因為就一直收,main那邊迴圈結束以後70行有close會把這邊關掉。

main那邊一樣就是個迴圈,sendover表示不送了,所以 not sendover就一直等你輸入資料來送,64行有寫說,除非你輸入/q,不然就會一直跑,所以你要結束程式就打/q就可以了。
送資料的function在63 行call的cprintf( cport_nr, buffer ) ; 他會把buffer內的字串送出去,前面那個是port號碼應該看很多次了。

大概就這樣啦。
我裡面用到的function建議你都到http://www.teuniz.net/RS-232/ 那邊,他的function都寫很清楚。

my code: https://dl.dropboxusercontent.com/u/8753544/tmp/rs232.7z
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.