<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python &#8211; 醬是創客</title>
	<atom:link href="https://www.chosemaker.com/language/python/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.chosemaker.com</link>
	<description>醬是創客</description>
	<lastBuildDate>Tue, 15 Dec 2020 05:19:16 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.6.8</generator>

<image>
	<url>https://www.chosemaker.com/wp-content/uploads/2020/07/cropped-favicon-1-32x32.jpg</url>
	<title>Python &#8211; 醬是創客</title>
	<link>https://www.chosemaker.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>[Python教學#2] Windows外接USB轉RS485 Serial並使用python的pySerial接收/發送byte實作</title>
		<link>https://www.chosemaker.com/language/python/python-lesson-2/</link>
		
		<dc:creator><![CDATA[pyclifecom]]></dc:creator>
		<pubDate>Tue, 04 Aug 2020 08:16:58 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[程式語言分類]]></category>
		<guid isPermaLink="false">https://www.chosemaker.com/?p=419</guid>

					<description><![CDATA[<p>醬是創客的Python教學主題第2篇，以Python語言來實作教學，本篇教學將以Windows 10 &#8230;</p>
<p>這篇文章 <a rel="nofollow" href="https://www.chosemaker.com/language/python/python-lesson-2/">[Python教學#2] Windows外接USB轉RS485 Serial並使用python的pySerial接收/發送byte實作</a> 最早出現於 <a rel="nofollow" href="https://www.chosemaker.com">醬是創客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>醬是創客的Python教學主題第2篇，以Python語言來實作教學，本篇教學將以Windows 10外接USB轉RS485 Serial並使用python的pySerial套件接收/發送byte實作，常見的應用就是來當Modbus Master透過RTU抓Slave的資料</p>



<p>設備:<br>Windows PC一台<br>PYC U485B USB轉RS485 #<a href="https://www.ruten.com.tw/item/show?22013373684403" target="_blank" rel="noreferrer noopener">露天拍賣</a> #<a href="https://shopee.tw/PYC-U485B-USB%E8%BD%89RS485-2-wire-%E6%A8%B9%E8%8E%93%E6%B4%BE-Linux-Raspberry-Pi-%E5%BA%8F%E5%88%97%E5%9F%A0-Serial-i.293276804.4545802005" target="_blank" rel="noreferrer noopener">蝦皮購物</a></p>



<p>接線方式:<br>PC–USB–PYC U485B(USB轉RS485)<br>PYC U485B(USB轉RS485)–A線–A線–自行撰寫的RS485設備<br>PYC U485B(USB轉RS485)–B線–B線–自行撰寫的RS485設備<br>(115200,N,8,1)</p>



<p>首先要先準備一台Windows PC，並安裝好Python最新的版本，並到Windows的cmd底下安裝python的pyserial套件</p>


<pre class="brush: plain; title: ; notranslate">
C:\Users\chosemaker\Desktop&gt;pip install pyserial
</pre>


<p>接著我們安裝PYC U485B的驅動程式，到他的官網下載驅動程式，裡面並附有如何在Windows安裝驅動程式的說明。接著到Windows裝置管理員，找出連接埠&gt;&gt;通訊連接埠，裡面會顯示出該USB的COM port 是多少，本範例使用COM1</p>



<p>開始來寫Python的程式</p>


<pre class="brush: python; title: ; notranslate">
import serial, time
  
ser = serial.Serial()
ser.port = &quot;COM1&quot;
  
#115200,N,8,1
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
  
ser.timeout = 0.5          #non-block read 0.5s
ser.writeTimeout = 0.5     #timeout for write 0.5s
ser.xonxoff = False    #disable software flow control
ser.rtscts = False     #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False     #disable hardware (DSR/DTR) flow control
  
try: 
    ser.open()
except Exception as ex:
    print (&quot;open serial port error &quot; + str(ex))
    exit()
  
if ser.isOpen():
  
    try:
        ser.flushInput() #flush input buffer
        ser.flushOutput() #flush output buffer
  
        #write 8 byte data
        ser.write([78, 78, 78, 78, 78, 78, 78, 78])
        print(&quot;write 8 byte data: 78, 78, 78, 78, 78, 78, 78, 78&quot;)
  
        time.sleep(0.5)  #wait 0.5s
  
        #read 8 byte data
        response = ser.read(8)
        print(&quot;read 8 byte data:&quot;)
        print(response)
  
        ser.close()
    except Exception as e1:
        print (&quot;communicating error &quot; + str(e1))
  
else:
    print (&quot;open serial port error&quot;)
</pre>


<p>自行撰寫的RS232設備收到PC發的8 byte 0x78後，會回發一個8 byte裡面是可以直接顯示8個string “N”。建立一個new.py並放在/home/pi，執行python new.py就可以跑出以下</p>


<pre class="brush: plain; title: ; notranslate">
C:\Users\chosemaker\Desktop&gt;python new.py
write 8 byte data: 78, 78, 78, 78, 78, 78, 78, 78
read 8 byte data:
b'NNNNNNNN'
</pre><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Flanguage%2Fpython%2Fpython-lesson-2%2F&amp;linkname=%5BPython%E6%95%99%E5%AD%B8%232%5D%20Windows%E5%A4%96%E6%8E%A5USB%E8%BD%89RS485%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook_messenger" href="https://www.addtoany.com/add_to/facebook_messenger?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Flanguage%2Fpython%2Fpython-lesson-2%2F&amp;linkname=%5BPython%E6%95%99%E5%AD%B8%232%5D%20Windows%E5%A4%96%E6%8E%A5USB%E8%BD%89RS485%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Facebook Messenger" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Flanguage%2Fpython%2Fpython-lesson-2%2F&amp;linkname=%5BPython%E6%95%99%E5%AD%B8%232%5D%20Windows%E5%A4%96%E6%8E%A5USB%E8%BD%89RS485%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Flanguage%2Fpython%2Fpython-lesson-2%2F&amp;linkname=%5BPython%E6%95%99%E5%AD%B8%232%5D%20Windows%E5%A4%96%E6%8E%A5USB%E8%BD%89RS485%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.chosemaker.com%2Flanguage%2Fpython%2Fpython-lesson-2%2F&#038;title=%5BPython%E6%95%99%E5%AD%B8%232%5D%20Windows%E5%A4%96%E6%8E%A5USB%E8%BD%89RS485%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" data-a2a-url="https://www.chosemaker.com/language/python/python-lesson-2/" data-a2a-title="[Python教學#2] Windows外接USB轉RS485 Serial並使用python的pySerial接收/發送byte實作"></a></p><p>這篇文章 <a rel="nofollow" href="https://www.chosemaker.com/language/python/python-lesson-2/">[Python教學#2] Windows外接USB轉RS485 Serial並使用python的pySerial接收/發送byte實作</a> 最早出現於 <a rel="nofollow" href="https://www.chosemaker.com">醬是創客</a>。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>[Python教學#1] Windows外接USB轉RS232 Serial並使用python的pySerial接收/發送byte實作</title>
		<link>https://www.chosemaker.com/language/python/python-lesson-1/</link>
		
		<dc:creator><![CDATA[pyclifecom]]></dc:creator>
		<pubDate>Tue, 04 Aug 2020 08:12:25 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[程式語言分類]]></category>
		<guid isPermaLink="false">https://www.chosemaker.com/?p=416</guid>

					<description><![CDATA[<p>醬是創客的Python教學主題第1篇，以Python語言來實作教學，本篇教學將以Windows 10 &#8230;</p>
<p>這篇文章 <a rel="nofollow" href="https://www.chosemaker.com/language/python/python-lesson-1/">[Python教學#1] Windows外接USB轉RS232 Serial並使用python的pySerial接收/發送byte實作</a> 最早出現於 <a rel="nofollow" href="https://www.chosemaker.com">醬是創客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>醬是創客的Python教學主題第1篇，以Python語言來實作教學，本篇教學將以Windows 10外接USB轉RS232 Serial並使用python的pySerial套件接收/發送byte實作，常見的應用就是使用AT command來發送簡訊</p>



<p>設備:<br>Windows PC一台<br>PYC U232 USB轉RS232 #<a href="https://www.ruten.com.tw/item/show?22002193991895" target="_blank" rel="noreferrer noopener">露天拍賣</a> #<a href="https://shopee.tw/PYC-U232-USB%E8%BD%89RS232-DB9-%E5%85%AC%E9%A0%AD-%E6%A8%B9%E8%8E%93%E6%B4%BE-Linux-Raspberry-Pi-%E5%BA%8F%E5%88%97%E5%9F%A0-Serial-i.293276804.3845893193" target="_blank" rel="noreferrer noopener">蝦皮購物</a></p>



<p>接線方式:<br>Windows PC–USB–PYC U232(USB轉RS232)<br>PYC U232(USB轉RS232)–RS232線–自行撰寫的RS232設備(115200,N,8,1)</p>



<p>首先要先準備一台Windows PC，並安裝好Python最新的版本，並到Windows的cmd底下安裝python的pyserial套件</p>


<pre class="brush: plain; title: ; notranslate">
C:\Users\chosemaker\Desktop&gt;pip install pyserial
</pre>


<p>接著我們安裝PYC U232的驅動程式，到他的官網下載驅動程式，裡面並附有如何在Windows安裝驅動程式的說明。接著到Windows裝置管理員，找出連接埠&gt;&gt;通訊連接埠，裡面會顯示出該USB的COM port 是多少，本範例使用COM1</p>



<p>開始來寫Python的程式</p>


<pre class="brush: python; title: ; notranslate">
import serial, time
 
ser = serial.Serial()
ser.port = &quot;COM1&quot;
 
#115200,N,8,1
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
 
ser.timeout = 0.5          #non-block read 0.5s
ser.writeTimeout = 0.5     #timeout for write 0.5s
ser.xonxoff = False    #disable software flow control
ser.rtscts = False     #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False     #disable hardware (DSR/DTR) flow control
 
try: 
    ser.open()
except Exception as ex:
    print (&quot;open serial port error &quot; + str(ex))
    exit()
 
if ser.isOpen():
 
    try:
        ser.flushInput() #flush input buffer
        ser.flushOutput() #flush output buffer
 
        #write 8 byte data
        ser.write([78, 78, 78, 78, 78, 78, 78, 78])
        print(&quot;write 8 byte data: 78, 78, 78, 78, 78, 78, 78, 78&quot;)
 
        time.sleep(0.5)  #wait 0.5s
 
        #read 8 byte data
        response = ser.read(8)
        print(&quot;read 8 byte data:&quot;)
        print(response)
 
        ser.close()
    except Exception as e1:
        print (&quot;communicating error &quot; + str(e1))
 
else:
    print (&quot;open serial port error&quot;)
</pre>


<p>自行撰寫的RS232設備收到PC發的8 byte 0x78後，會回發一個8 byte裡面是可以直接顯示8個string “N”。建立一個new.py並放在/home/pi，執行python new.py就可以跑出以下</p>


<pre class="brush: plain; title: ; notranslate">
C:\Users\chosemaker\Desktop&gt;python new.py
write 8 byte data: 78, 78, 78, 78, 78, 78, 78, 78
read 8 byte data:
b'NNNNNNNN'
</pre><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Flanguage%2Fpython%2Fpython-lesson-1%2F&amp;linkname=%5BPython%E6%95%99%E5%AD%B8%231%5D%20Windows%E5%A4%96%E6%8E%A5USB%E8%BD%89RS232%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook_messenger" href="https://www.addtoany.com/add_to/facebook_messenger?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Flanguage%2Fpython%2Fpython-lesson-1%2F&amp;linkname=%5BPython%E6%95%99%E5%AD%B8%231%5D%20Windows%E5%A4%96%E6%8E%A5USB%E8%BD%89RS232%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Facebook Messenger" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Flanguage%2Fpython%2Fpython-lesson-1%2F&amp;linkname=%5BPython%E6%95%99%E5%AD%B8%231%5D%20Windows%E5%A4%96%E6%8E%A5USB%E8%BD%89RS232%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Flanguage%2Fpython%2Fpython-lesson-1%2F&amp;linkname=%5BPython%E6%95%99%E5%AD%B8%231%5D%20Windows%E5%A4%96%E6%8E%A5USB%E8%BD%89RS232%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.chosemaker.com%2Flanguage%2Fpython%2Fpython-lesson-1%2F&#038;title=%5BPython%E6%95%99%E5%AD%B8%231%5D%20Windows%E5%A4%96%E6%8E%A5USB%E8%BD%89RS232%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" data-a2a-url="https://www.chosemaker.com/language/python/python-lesson-1/" data-a2a-title="[Python教學#1] Windows外接USB轉RS232 Serial並使用python的pySerial接收/發送byte實作"></a></p><p>這篇文章 <a rel="nofollow" href="https://www.chosemaker.com/language/python/python-lesson-1/">[Python教學#1] Windows外接USB轉RS232 Serial並使用python的pySerial接收/發送byte實作</a> 最早出現於 <a rel="nofollow" href="https://www.chosemaker.com">醬是創客</a>。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>[樹莓派教學#4] Raspberry Pi外接USB轉RS485 Serial並使用python的pySerial接收/發送byte實作</title>
		<link>https://www.chosemaker.com/board/raspberry-pi/pi-lesson-4/</link>
		
		<dc:creator><![CDATA[pyclifecom]]></dc:creator>
		<pubDate>Tue, 04 Aug 2020 07:58:10 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[樹莓派]]></category>
		<category><![CDATA[程式語言分類]]></category>
		<category><![CDATA[開發板分類]]></category>
		<guid isPermaLink="false">https://www.chosemaker.com/?p=412</guid>

					<description><![CDATA[<p>醬是創客的樹莓派教學主題第4篇，以Raspberry Pi開發板來實作教學，本篇教學將著重Raspb &#8230;</p>
<p>這篇文章 <a rel="nofollow" href="https://www.chosemaker.com/board/raspberry-pi/pi-lesson-4/">[樹莓派教學#4] Raspberry Pi外接USB轉RS485 Serial並使用python的pySerial接收/發送byte實作</a> 最早出現於 <a rel="nofollow" href="https://www.chosemaker.com">醬是創客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>醬是創客的樹莓派教學主題第4篇，以Raspberry Pi開發板來實作教學，本篇教學將著重Raspberry Pi外接USB轉RS485 Serial並使用python 的pySerial接收/發送byte實作，常見的應用就是樹莓派來當Modbus Master透過RTU抓Slave的資料</p>



<p>設備:<br>Raspberry Pi 各系列<br>樹莓派4B #<a href="https://www.ruten.com.tw/item/show?22051318422459" target="_blank" rel="noreferrer noopener">露天拍賣</a> #<a href="https://shopee.tw/%E6%A8%B9%E8%8E%93%E6%B4%BE4B-%E8%8B%B1%E5%9C%8B%E8%A3%BD%E5%8E%9F%E5%BB%A0%E9%96%8B%E7%99%BC%E6%9D%BF-4G-8G-Raspberry-Pi-4B-AI%E5%BD%B1%E5%83%8F%E8%BE%A8%E8%AD%98-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E5%B0%88%E7%94%A8-Linux-i.293276804.5269190409" target="_blank" rel="noreferrer noopener">蝦皮購物</a><br>PYC U485B USB轉RS485 #<a href="https://www.ruten.com.tw/item/show?22013373684403" target="_blank" rel="noreferrer noopener">露天拍賣</a> #<a href="https://shopee.tw/PYC-U485B-USB%E8%BD%89RS485-2-wire-%E6%A8%B9%E8%8E%93%E6%B4%BE-Linux-Raspberry-Pi-%E5%BA%8F%E5%88%97%E5%9F%A0-Serial-i.293276804.4545802005" target="_blank" rel="noreferrer noopener">蝦皮購物</a></p>



<p>接線方式:<br>樹莓派–USB–PYC U485B(USB轉RS485)<br>PYC U485B(USB轉RS485)–A線–A線–自行撰寫的RS485設備<br>PYC U485B(USB轉RS485)–B線–B線–自行撰寫的RS485設備<br>(115200,N,8,1)</p>



<p>首先要先準備一台裝好的樹莓派，並安裝好Raspbian，預設的Raspbian已經安裝好python了，再來我們先更新系統到最新版本，並安裝python的pyserial套件</p>


<pre class="brush: plain; title: ; notranslate">
sudo apt-get update
sudo apt-get upgrade
pip install pyserial
</pre>


<p>接著我們安裝PYC U485B的驅動程式，到他的官網下載驅動程式，裡面並附有如何在Linux安裝驅動程式的說明。我們也有發現最新的樹莓派版本已經有支援該款晶片，如果插入後沒有出現，有CH340的相關資料，再去官網下載安裝，我們輸入lsusb以後就可以找到USB Serial相關資訊，代表有成功抓到</p>


<pre class="brush: as3; title: ; notranslate">
root@raspberrypi_chosemaker:/home/pi# lsusb
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 1a86:7523 340 USB-Serial adapter
</pre>


<p>我們要去debug message下面抓該張USB Serial使用哪一個tty，所以打dmesg | grep tty，這時候可以看到它使用ttyUSB0，它的Port名是/dev/ttyUSB0</p>


<pre class="brush: as3; title: ; notranslate">
root@raspberrypi_chosemaker:/home/pi# dmesg | grep tty
[12708.761510] usb 1-1.3: ch341-uart converter now attached to ttyUSB0
</pre>


<p>開始來寫Python的程式</p>


<pre class="brush: python; title: ; notranslate">
import serial, time
 
ser = serial.Serial()
ser.port = &quot;/dev/ttyUSB0&quot;
 
#115200,N,8,1
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
 
ser.timeout = 0.5          #non-block read 0.5s
ser.writeTimeout = 0.5     #timeout for write 0.5s
ser.xonxoff = False    #disable software flow control
ser.rtscts = False     #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False     #disable hardware (DSR/DTR) flow control
 
try: 
    ser.open()
except Exception as ex:
    print (&quot;open serial port error &quot; + str(ex))
    exit()
 
if ser.isOpen():
 
    try:
        ser.flushInput() #flush input buffer
        ser.flushOutput() #flush output buffer
 
        #write 8 byte data
        ser.write([78, 78, 78, 78, 78, 78, 78, 78])
        print(&quot;write 8 byte data: 78, 78, 78, 78, 78, 78, 78, 78&quot;)
 
        time.sleep(0.5)  #wait 0.5s
 
        #read 8 byte data
        response = ser.read(8)
        print(&quot;read 8 byte data:&quot;)
        print(response)
 
        ser.close()
    except Exception as e1:
        print (&quot;communicating error &quot; + str(e1))
 
else:
    print (&quot;open serial port error&quot;)
</pre>


<p>自行撰寫的RS485設備收到樹莓派發的8 byte 0x78後，會回發一個8 byte裡面是可以直接顯示8個string &#8220;N”。建立一個new.py並放在/home/pi，執行python3 new.py就可以跑出以下</p>


<pre class="brush: as3; title: ; notranslate">
root@raspberrypi_chosemaker:/home/pi# python3 new.py
write 8 byte data: 78, 78, 78, 78, 78, 78, 78, 78
read 8 byte data:
b'NNNNNNNN'
</pre>


<p></p>
<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Fboard%2Fraspberry-pi%2Fpi-lesson-4%2F&amp;linkname=%5B%E6%A8%B9%E8%8E%93%E6%B4%BE%E6%95%99%E5%AD%B8%234%5D%20Raspberry%20Pi%E5%A4%96%E6%8E%A5USB%E8%BD%89RS485%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook_messenger" href="https://www.addtoany.com/add_to/facebook_messenger?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Fboard%2Fraspberry-pi%2Fpi-lesson-4%2F&amp;linkname=%5B%E6%A8%B9%E8%8E%93%E6%B4%BE%E6%95%99%E5%AD%B8%234%5D%20Raspberry%20Pi%E5%A4%96%E6%8E%A5USB%E8%BD%89RS485%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Facebook Messenger" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Fboard%2Fraspberry-pi%2Fpi-lesson-4%2F&amp;linkname=%5B%E6%A8%B9%E8%8E%93%E6%B4%BE%E6%95%99%E5%AD%B8%234%5D%20Raspberry%20Pi%E5%A4%96%E6%8E%A5USB%E8%BD%89RS485%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Fboard%2Fraspberry-pi%2Fpi-lesson-4%2F&amp;linkname=%5B%E6%A8%B9%E8%8E%93%E6%B4%BE%E6%95%99%E5%AD%B8%234%5D%20Raspberry%20Pi%E5%A4%96%E6%8E%A5USB%E8%BD%89RS485%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.chosemaker.com%2Fboard%2Fraspberry-pi%2Fpi-lesson-4%2F&#038;title=%5B%E6%A8%B9%E8%8E%93%E6%B4%BE%E6%95%99%E5%AD%B8%234%5D%20Raspberry%20Pi%E5%A4%96%E6%8E%A5USB%E8%BD%89RS485%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" data-a2a-url="https://www.chosemaker.com/board/raspberry-pi/pi-lesson-4/" data-a2a-title="[樹莓派教學#4] Raspberry Pi外接USB轉RS485 Serial並使用python的pySerial接收/發送byte實作"></a></p><p>這篇文章 <a rel="nofollow" href="https://www.chosemaker.com/board/raspberry-pi/pi-lesson-4/">[樹莓派教學#4] Raspberry Pi外接USB轉RS485 Serial並使用python的pySerial接收/發送byte實作</a> 最早出現於 <a rel="nofollow" href="https://www.chosemaker.com">醬是創客</a>。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>[樹莓派教學#3] Raspberry Pi外接USB轉RS232 Serial並使用python的pySerial接收/發送byte實作</title>
		<link>https://www.chosemaker.com/board/raspberry-pi/pi-lesson-3/</link>
		
		<dc:creator><![CDATA[pyclifecom]]></dc:creator>
		<pubDate>Tue, 04 Aug 2020 07:51:13 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[樹莓派]]></category>
		<category><![CDATA[程式語言分類]]></category>
		<category><![CDATA[開發板分類]]></category>
		<guid isPermaLink="false">https://www.chosemaker.com/?p=410</guid>

					<description><![CDATA[<p>醬是創客的樹莓派教學主題第3篇，以Raspberry Pi開發板來實作教學，本篇教學將著重Raspb &#8230;</p>
<p>這篇文章 <a rel="nofollow" href="https://www.chosemaker.com/board/raspberry-pi/pi-lesson-3/">[樹莓派教學#3] Raspberry Pi外接USB轉RS232 Serial並使用python的pySerial接收/發送byte實作</a> 最早出現於 <a rel="nofollow" href="https://www.chosemaker.com">醬是創客</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>醬是創客的樹莓派教學主題第3篇，以Raspberry Pi開發板來實作教學，本篇教學將著重Raspberry Pi外接USB轉RS232 Serial並使用python 的pySerial接收/發送byte實作，常見的應用就是樹莓派透過MQTT拋送從COM port抓到的資料</p>



<p>設備:<br>Raspberry Pi 各系列<br>樹莓派4B #<a href="https://www.ruten.com.tw/item/show?22051318422459" target="_blank" rel="noreferrer noopener">露天拍賣</a> #<a href="https://shopee.tw/%E6%A8%B9%E8%8E%93%E6%B4%BE4B-%E8%8B%B1%E5%9C%8B%E8%A3%BD%E5%8E%9F%E5%BB%A0%E9%96%8B%E7%99%BC%E6%9D%BF-4G-8G-Raspberry-Pi-4B-AI%E5%BD%B1%E5%83%8F%E8%BE%A8%E8%AD%98-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E5%B0%88%E7%94%A8-Linux-i.293276804.5269190409" target="_blank" rel="noreferrer noopener">蝦皮購物</a><br>PYC U232 USB轉RS232 #<a href="https://www.ruten.com.tw/item/show?22002193991895" target="_blank" rel="noreferrer noopener">露天拍賣</a> #<a href="https://shopee.tw/PYC-U232-USB%E8%BD%89RS232-DB9-%E5%85%AC%E9%A0%AD-%E6%A8%B9%E8%8E%93%E6%B4%BE-Linux-Raspberry-Pi-%E5%BA%8F%E5%88%97%E5%9F%A0-Serial-i.293276804.3845893193" target="_blank" rel="noreferrer noopener">蝦皮購物</a></p>



<p>接線方式:<br>樹莓派&#8211;USB&#8211;PYC U232(USB轉RS232)<br>PYC U232(USB轉RS232)&#8211;RS232線&#8211;自行撰寫的RS232設備(115200,N,8,1)</p>



<p>首先要先準備一台裝好的樹莓派，並安裝好Raspbian，預設的Raspbian已經安裝好python了，再來我們先更新系統到最新版本，並安裝python的pyserial套件</p>


<pre class="brush: python; title: ; notranslate">
sudo apt-get update
sudo apt-get upgrade
pip install pyserial
</pre>


<p>接著我們安裝PYC U232的驅動程式，到他的官網下載驅動程式，裡面並附有如何在Linux安裝驅動程式的說明。我們也有發現最新的樹莓派版本已經有支援該款晶片，如果插入後沒有出現，有CH340的相關資料，再去官網下載安裝，我們輸入lsusb以後就可以找到USB Serial相關資訊，代表有成功抓到</p>


<pre class="brush: as3; title: ; notranslate">
root@raspberrypi_chosemaker:/home/pi# lsusb
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 1a86:7523 340 USB-Serial adapter
</pre>


<p>我們要去debug message下面抓該張USB Serial使用哪一個tty，所以打dmesg | grep tty，這時候可以看到它使用ttyUSB0，它的Port名是/dev/ttyUSB0</p>


<pre class="brush: as3; title: ; notranslate">
root@raspberrypi_chosemaker:/home/pi# dmesg | grep tty
[12708.761510] usb 1-1.3: ch341-uart converter now attached to ttyUSB0
</pre>


<p>開始來寫Python的程式</p>


<pre class="brush: python; title: ; notranslate">
import serial, time

ser = serial.Serial()
ser.port = &quot;/dev/ttyUSB0&quot;

#115200,N,8,1
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check
ser.stopbits = serial.STOPBITS_ONE #number of stop bits

ser.timeout = 0.5          #non-block read 0.5s
ser.writeTimeout = 0.5     #timeout for write 0.5s
ser.xonxoff = False    #disable software flow control
ser.rtscts = False     #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False     #disable hardware (DSR/DTR) flow control

try: 
    ser.open()
except Exception as ex:
    print (&quot;open serial port error &quot; + str(ex))
    exit()

if ser.isOpen():

    try:
        ser.flushInput() #flush input buffer
        ser.flushOutput() #flush output buffer

        #write 8 byte data
        ser.write([78, 78, 78, 78, 78, 78, 78, 78])
        print(&quot;write 8 byte data: 78, 78, 78, 78, 78, 78, 78, 78&quot;)

        time.sleep(0.5)  #wait 0.5s

        #read 8 byte data
        response = ser.read(8)
        print(&quot;read 8 byte data:&quot;)
        print(response)

        ser.close()
    except Exception as e1:
        print (&quot;communicating error &quot; + str(e1))

else:
    print (&quot;open serial port error&quot;)
</pre>


<p>自行撰寫的RS232設備收到樹莓派發的8 byte 0x78後，會回發一個8 byte裡面是可以直接顯示8個string &#8220;N”。建立一個new.py並放在/home/pi，執行python3 new.py就可以跑出以下</p>


<pre class="brush: as3; title: ; notranslate">
root@raspberrypi_chosemaker:/home/pi# python3 new.py
write 8 byte data: 78, 78, 78, 78, 78, 78, 78, 78
read 8 byte data:
b'NNNNNNNN'
</pre><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Fboard%2Fraspberry-pi%2Fpi-lesson-3%2F&amp;linkname=%5B%E6%A8%B9%E8%8E%93%E6%B4%BE%E6%95%99%E5%AD%B8%233%5D%20Raspberry%20Pi%E5%A4%96%E6%8E%A5USB%E8%BD%89RS232%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook_messenger" href="https://www.addtoany.com/add_to/facebook_messenger?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Fboard%2Fraspberry-pi%2Fpi-lesson-3%2F&amp;linkname=%5B%E6%A8%B9%E8%8E%93%E6%B4%BE%E6%95%99%E5%AD%B8%233%5D%20Raspberry%20Pi%E5%A4%96%E6%8E%A5USB%E8%BD%89RS232%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Facebook Messenger" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_line" href="https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Fboard%2Fraspberry-pi%2Fpi-lesson-3%2F&amp;linkname=%5B%E6%A8%B9%E8%8E%93%E6%B4%BE%E6%95%99%E5%AD%B8%233%5D%20Raspberry%20Pi%E5%A4%96%E6%8E%A5USB%E8%BD%89RS232%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Line" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.chosemaker.com%2Fboard%2Fraspberry-pi%2Fpi-lesson-3%2F&amp;linkname=%5B%E6%A8%B9%E8%8E%93%E6%B4%BE%E6%95%99%E5%AD%B8%233%5D%20Raspberry%20Pi%E5%A4%96%E6%8E%A5USB%E8%BD%89RS232%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.chosemaker.com%2Fboard%2Fraspberry-pi%2Fpi-lesson-3%2F&#038;title=%5B%E6%A8%B9%E8%8E%93%E6%B4%BE%E6%95%99%E5%AD%B8%233%5D%20Raspberry%20Pi%E5%A4%96%E6%8E%A5USB%E8%BD%89RS232%20Serial%E4%B8%A6%E4%BD%BF%E7%94%A8python%E7%9A%84pySerial%E6%8E%A5%E6%94%B6%2F%E7%99%BC%E9%80%81byte%E5%AF%A6%E4%BD%9C" data-a2a-url="https://www.chosemaker.com/board/raspberry-pi/pi-lesson-3/" data-a2a-title="[樹莓派教學#3] Raspberry Pi外接USB轉RS232 Serial並使用python的pySerial接收/發送byte實作"></a></p><p>這篇文章 <a rel="nofollow" href="https://www.chosemaker.com/board/raspberry-pi/pi-lesson-3/">[樹莓派教學#3] Raspberry Pi外接USB轉RS232 Serial並使用python的pySerial接收/發送byte實作</a> 最早出現於 <a rel="nofollow" href="https://www.chosemaker.com">醬是創客</a>。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
