1. 程式人生 > >Halcon串列埠配置

Halcon串列埠配置

open_serial( : : PortName : SerialHandle)

開啟串列埠

close_serial( : : SerialHandle : )

關閉串列埠

set_serial_param( : : SerialHandle, BaudRate, DataBits, FlowControl, Parity, StopBits, TotalTimeOut, InterCharTimeOut : )

配置串列埠
TotalTimeOut:總超時間隔,包含接收第一個位元組的等待超時和接收兩個位元組之間的等待超時
InterCharTimeOut :接收兩個位元組之間的等待超時,這個數值要小於總超時間隔
如果TotalTimeOut和InterCharTimeOut 其中的一個設定為-1,則read_serial會一直等待到讀取到NumCharacters個位元組後才返回

read_serial( : : SerialHandle, NumCharacters : Data)

NumCharacters:讀NumCharacters個位元組後返回,如果到達等待超時時間,直接返回

tuple_chr — Convert a tuple of integer numbers into strings of length 1.
將整數元組轉換成單個元素長度為1的字串元組
tuple_chrt — Convert a tuple of integer numbers into strings.
將整數元組轉換成字串元組
tuple_chrt配合read_serial

使用

tuple_chr ( [72, 101, 108, 108, 111, 10, 13], Chr)
tuple_chrt ( [72, 101, 108, 108, 111, 10, 13], Chrt)
Chr := ['H', 'e', 'l', 'l', 'o', '\n', '\r']
Chrs := ['Hello\n\r']

write_serial( : : SerialHandle, Data : )

一次性寫完所有資料後返回,無超時等待機制

tuple_ord — Convert a tuple of strings of length 1 into a tuple of integer numbers.
將單個元素長度為1的字串元組轉換成整數元組
tuple_ords — Convert a tuple of strings into a tuple of integer numbers.
將字串元組轉換成整數元組
tuple_ords 配合write_serial

使用

tuple_ords (['Hello', '\n\r'], Ords)
tuple_ord (['H', 'e', 'l', 'l', 'o', '\n', '\r'], Ord)
Ords := [72, 101, 108, 108, 111, 10, 13]
Ord := [72, 101, 108, 108, 111, 10, 13]

總的Demo

open_serial ('COM1', SerialHandle)
* Set the parameters of the serial interface (e.g., a character-based terminal).
set_serial_param (SerialHandle, 9600, 8, 'none', 'none', 1, 2000, 100)
* Display a nice message on the terminal.
write_serial (SerialHandle, ords(['Hello world!', '\n\r']))
* Now read 10 characters from the terminal...
read_serial (SerialHandle, 100, Data)
* ...and if the user typed some characters within the one second...
if (|Data| > 0)
    * ...convert the read data to a string.
    Read := chrt(Data)
endif
* Finally, close the serial interface so other programs can use it.
close_serial (SerialHandle)