├── .gitignore ├── LICENSE ├── README.md ├── USB2SERIAL_RS485_Read ├── MSP430_Transmitter │ ├── MSP430_uart_rs485_Transmit.c │ └── MSP430_uart_rs485_TransmitString.c └── PC_Receiver │ ├── Python 2.7.x │ └── RS485_Read_2.7.py │ └── Python 3.x.x │ └── RS485_Read.py └── USB2SERIAL_RS485_Write ├── MSP430_Receiver └── MSP430_uart_rs485_reieve .c └── PC_Transmitter ├── Python 2.7.x └── RS485_Write_2.7.py └── Python 3.x.x └── RS485_Write.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | 61 | #Ipython Notebook 62 | .ipynb_checkpoints 63 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cross Platform RS485 Programming using Python and PySerial 2 | Cross platform Python code for communicating between PC and microcontroller using RS485 Protocol 3 | -------------------------------------------------------------------------------- /USB2SERIAL_RS485_Read/MSP430_Transmitter/MSP430_uart_rs485_Transmit.c: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------------------------------// 2 | // RS485 (Transmitter side) // 3 | //----------------------------------------------------------------------------------------------------// 4 | // Program to send data from MSP430 to PC through RS485 link.MSP430G2553 is interfaced with a RS485 // 5 | // decoder chip (MAX485).MSP430 sends a character to PC which is read by the PC side software. // 6 | // MSP430 sends a character 'A' . // 7 | //----------------------------------------------------------------------------------------------------// 8 | // // 9 | // |------------| // 10 | // | RS485 | +---------+ +-----------+ // 11 | // | Read.exe | USB2SERIAL V1.0 | RO|---->|P1.1(RXD) | // 12 | // |------------| |--------------| | ~RE|-----|P2.1 | // 13 | // \ [][][][][] \===========| USB <- RS485 |~~~~~~~~~~~~~~~~~~~|A,B | | | // 14 | // \------------\ USB |--------------| Twisted | DE|-----|P2.0 | // 15 | // Laptop Pair | DI|<----|P1.2(TXD) | // 16 | // +---------+ +-----------+ // 17 | // MAX485 MSP430G2553 // 18 | // // 19 | // [Reciever] <-------------------------------------------- [-------Transmitter---------] // 20 | // // 21 | //====================================================================================================// 22 | // Hardware // 23 | // // 24 | // MSP430G2553 on LaunchPad Development board Connected to MAX485. // 25 | // Clocks :- DC0 @ 1MHz ,SMCLK @1MHz // 26 | // Baudrate :- 9600bps // 27 | // [MSP430] P1.1(RXD) <- R0 [RS485](Recieve Out) // 28 | // [MSP430] P1.2(TXD) -> DI [RS485](Driver Input) // 29 | // [MSP430] P2.0 -> DE [RS485](Driver Out Enable) // 30 | // [MSP430] P2.1 -> RE [RS485](Recieve Enable) // 31 | //----------------------------------------------------------------------------------------------------// 32 | // Compiled on IAR Embedded Workbench for MSP430 version 5.30.1 // 33 | // 02-October-2014 // 34 | // Rahul.S // 35 | //----------------------------------------------------------------------------------------------------// 36 | 37 | /*====================================================================================================*/ 38 | /* www.xanthium.in */ 39 | /* Copyright (C) 2014 Rahul.S */ 40 | /*====================================================================================================*/ 41 | 42 | #include "msp430g2553.h" 43 | void main(void) 44 | { 45 | WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog 46 | 47 | //------------------- Configure the Clocks -------------------// 48 | 49 | if (CALBC1_1MHZ==0xFF) // If calibration constant erased 50 | { 51 | while(1); // do not load, trap CPU!! 52 | } 53 | 54 | DCOCTL = 0; // Select lowest DCOx and MODx settings 55 | BCSCTL1 = CALBC1_1MHZ; // Set range 56 | DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation 57 | 58 | 59 | 60 | //------------------- Configure the Ports -------------------// 61 | 62 | // Setting the UART function for P1.1 & P1.2 63 | P1SEL |= BIT1 + BIT2; // P1.1 UCA0RXD input 64 | P1SEL2 |= BIT1 + BIT2; // P1.2 UCA0TXD output 65 | 66 | // Setting the direction for P1.0, P1.6, P2.0 and P2.1 67 | P1DIR |= BIT0 + BIT6 ; // P1.0,P1.6 output(leds) 68 | P2DIR |= BIT0 + BIT1; // P2.0 -> DE,P2.1 -> ~RE Output 69 | P1OUT &= ~BIT0 + BIT6; // Clear P1.0 70 | 71 | //Enabling MAX485 in Transmit mode 72 | P2OUT |= BIT0; //P2.0 -> DE (high) 73 | P2OUT |= BIT1; //P2.1 -> ~RE (high) RE is active Low 74 | 75 | 76 | 77 | //-------------- Configure USCI_A0 in UART mode --------------// 78 | 79 | UCA0CTL1 |= UCSSEL_2 + UCSWRST; // USCI Clock = SMCLK,USCI_A0 disabled 80 | UCA0BR0 = 104; // 104 From datasheet table- 81 | UCA0BR1 = 0; // -selects baudrate =9600,clk = SMCLK 82 | UCA0MCTL = UCBRS_1; // Modulation value = 1 from datasheet 83 | 84 | UCA0CTL1 &= ~UCSWRST; // Clear UCSWRST to enable USCI_A0 85 | 86 | UCA0TXBUF = 'A'; 87 | 88 | _BIS_SR(LPM0_bits + GIE); // Going to LPM0 89 | 90 | }//end of main() 91 | 92 | -------------------------------------------------------------------------------- /USB2SERIAL_RS485_Read/MSP430_Transmitter/MSP430_uart_rs485_TransmitString.c: -------------------------------------------------------------------------------- 1 | 2 | //----------------------------------------------------------------------------------------------------// 3 | // RS485 (Transmitter side) // 4 | //----------------------------------------------------------------------------------------------------// 5 | // Program to send data from MSP430 to PC through RS485 link.MSP430G2553 is interfaced with a RS485 // 6 | // decoder chip (MAX485).MSP430 sends a string to PC which is read by the PC side software. // 7 | // MSP430 sends string "Hello from MSP430" . // 8 | //----------------------------------------------------------------------------------------------------// 9 | // // 10 | // |------------| // 11 | // | RS485 | +---------+ +-----------+ // 12 | // | Read.exe | USB2SERIAL V1.0 | RO|---->|P1.1(RXD) | // 13 | // |------------| |--------------| | ~RE|-----|P2.1 | // 14 | // \ [][][][][] \===========| USB <- RS485 |~~~~~~~~~~~~~~~~~~~|A,B | | | // 15 | // \------------\ USB |--------------| Twisted | DE|-----|P2.0 | // 16 | // Laptop Pair | DI|<----|P1.2(TXD) | // 17 | // +---------+ +-----------+ // 18 | // MAX485 MSP430G2553 // 19 | // // 20 | // [Reciever] <-------------------------------------------- [-------Transmitter---------] // 21 | // // 22 | //====================================================================================================// 23 | // Hardware // // 24 | // // 25 | // MSP430G2553 on LaunchPad Development board Connected to MAX485. // 26 | // Clocks :- DC0 @ 1MHz ,SMCLK @1MHz // 27 | // Baudrate :- 9600bps // 28 | // [MSP430] P1.1(RXD) <- R0 [RS485](Recieve Out) // 29 | // [MSP430] P1.2(TXD) -> DI [RS485](Driver Input) // 30 | // [MSP430] P2.0 -> DE [RS485](Driver Out Enable) // 31 | // [MSP430] P2.1 -> RE [RS485](Recieve Enable) // 32 | //----------------------------------------------------------------------------------------------------// 33 | // Compiled on IAR Embedded Workbench for MSP430 version 5.30.1 // 34 | // 02-October-2014 // 35 | // Rahul.S // 36 | //----------------------------------------------------------------------------------------------------// 37 | 38 | /*====================================================================================================*/ 39 | /* www.xanthium.in */ 40 | /* Copyright (C) 2014 Rahul.S */ 41 | /*====================================================================================================*/ 42 | 43 | #include "msp430g2553.h" 44 | void main(void) 45 | { 46 | WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog 47 | 48 | //------------------- Configure the Clocks -------------------// 49 | 50 | if (CALBC1_1MHZ==0xFF) // If calibration constant erased 51 | { 52 | while(1); // do not load, trap CPU!! 53 | } 54 | 55 | DCOCTL = 0; // Select lowest DCOx and MODx settings 56 | BCSCTL1 = CALBC1_1MHZ; // Set range 57 | DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation 58 | 59 | //char Text[] = {'H','e','l','l','o',' ','f','r','o','m',' ','M','S','P','4','3','0'}; 60 | char Text[50] = "Hello From MSP430\n"; 61 | char Count = 0; 62 | //------------------- Configure the Ports -------------------// 63 | 64 | // Setting the UART function for P1.1 & P1.2 65 | P1SEL |= BIT1 + BIT2; // P1.1 UCA0RXD input 66 | P1SEL2 |= BIT1 + BIT2; // P1.2 UCA0TXD output 67 | 68 | // Setting the direction for P1.0, P1.6, P2.0 and P2.1 69 | P1DIR |= BIT0 + BIT6 ; // P1.0,P1.6 output(leds) 70 | P2DIR |= BIT0 + BIT1; // P2.0 -> DE,P2.1 -> ~RE Output 71 | P1OUT &= ~BIT0 + BIT6; // Clear P1.0 72 | 73 | //Enabling MAX485 in Transmit mode 74 | P2OUT |= BIT0; //P2.0 -> DE (high) 75 | P2OUT |= BIT1; //P2.1 -> ~RE (high) RE is active Low 76 | 77 | 78 | 79 | //-------------- Configure USCI_A0 in UART mode --------------// 80 | 81 | UCA0CTL1 |= UCSSEL_2 + UCSWRST; // USCI Clock = SMCLK,USCI_A0 disabled 82 | UCA0BR0 = 104; // 104 From datasheet table- 83 | UCA0BR1 = 0; // -selects baudrate =9600,clk = SMCLK 84 | UCA0MCTL = UCBRS_1; // Modulation value = 1 from datasheet 85 | 86 | UCA0CTL1 &= ~UCSWRST; // Clear UCSWRST to enable USCI_A0 87 | 88 | while(Text[Count] != '\0') 89 | { 90 | while(!(UCA0TXIFG & IFG2)); 91 | UCA0TXBUF = Text[Count]; 92 | Count++; 93 | } 94 | 95 | _BIS_SR(LPM0_bits + GIE); // Going to LPM0 96 | 97 | }//end of main() 98 | 99 | -------------------------------------------------------------------------------- /USB2SERIAL_RS485_Read/PC_Receiver/Python 2.7.x/RS485_Read_2.7.py: -------------------------------------------------------------------------------- 1 | #----------------------------------------------------------------------------------------------------# 2 | # RS485 Communication using Python (Read) (hardware = USB2SERIAL) (Python 2.7.x) # 3 | #----------------------------------------------------------------------------------------------------# 4 | # Program runs on the PC side and waits for a string to be send by microcontroller.Upon receiving- # 5 | # -the string program displays on terminal.Program uses PySerial module to communicate with Serial # 6 | # Port of USB2SERIAL # 7 | #----------------------------------------------------------------------------------------------------# 8 | # BaudRate -> 9600 # 9 | # Data formt -> 8 databits,No parity,1 Stop bit (8N1) # 10 | # Flow Control -> None # 11 | #----------------------------------------------------------------------------------------------------# 12 | 13 | #====================================================================================================# 14 | # www.xanthium.in # 15 | # Copyright (C) 2015 Rahul.S # 16 | #====================================================================================================# 17 | 18 | #====================================================================================================# 19 | # Interpreter/IDE : Python 2.7.x/IDLE # 20 | # Module : PySerial # # # 21 | # OS : Linux # 22 | # Programmer : Rahul.S # 23 | # Date : 31-March-2015 # 24 | #====================================================================================================# 25 | 26 | #====================================================================================================# 27 | # Finding out the serial port number corresponding to your Computer # 28 | #====================================================================================================# 29 | # On Linux # 30 | #----------------------------------------------------------------------------------------------------# 31 | # USB2SERIAL will be detected as ttyUSB0 or ttyUSB1.You can check the port number of USB2SERIAL by # 32 | # connecting you board to USB port and doing # 33 | # dmesg | tail # 34 | # and checking the output. # 35 | #====================================================================================================# 36 | 37 | #====================================================================================================# 38 | # Running the Program # 39 | #====================================================================================================# 40 | # On Linux # 41 | #----------------------------------------------------------------------------------------------------# 42 | # Find out your serial port name and number corresponding to USB2SERIAL on your system.It will be- # 43 | # -usually in the form of ttyUSB0 and ttyUSB1. # 44 | # Open terminal and type the following command to invoke Python3.x interpretor # 45 | # [user@linux:~$] sudo python RS485_Write.py # 46 | # Give the password and then enter your portnumber when program asks ->/dev/ttyUSB0 # 47 | #----------------------------------------------------------------------------------------------------# 48 | # On Windows # 49 | #----------------------------------------------------------------------------------------------------# 50 | # Open the command prompt and type the following # 51 | # C:\>python RS485_Write.py # 52 | # Enter the COM number when program asks -> com31 # 53 | #====================================================================================================# 54 | 55 | import serial # import the module 56 | 57 | def banner_top(): 58 | print ' +-------------------------------------------+' 59 | print ' | USB2SERIAL RS485 Read in Python 2.7.x |' 60 | print ' | (c) www.xanthium.in |' 61 | print ' +-------------------------------------------+' 62 | 63 | def Usage(): 64 | print ' | Windows -> COMxx eg COM32 |' 65 | print ' | Linux ->/dev/ttyS* eg /dev/ttyUSB0 |' 66 | print ' +-------------------------------------------+' 67 | 68 | def banner_bottom(): 69 | print ' +-------------------------------------------+' 70 | print ' | Press Any Key to Exit |' 71 | print ' +-------------------------------------------+' 72 | 73 | banner_top() # Display the top banner 74 | Usage() 75 | COM_PortName = raw_input('\n Enter the COM Port Name ->') 76 | 77 | #Opening the serial port 78 | 79 | COM_Port = serial.Serial(COM_PortName) # open the COM port 80 | print '\n ',COM_PortName,'Opened' 81 | 82 | COM_Port.baudrate = 9600 # set Baud rate 83 | COM_Port.bytesize = 8 # Number of data bits = 8 84 | COM_Port.parity = 'N' # No parity 85 | COM_Port.stopbits = 1 # Number of Stop bits = 1 86 | 87 | print '\n Baud rate = ',COM_Port.baudrate 88 | print ' Data bits = ',COM_Port.bytesize 89 | print ' Parity = ',COM_Port.parity 90 | print ' Stop bits = ',COM_Port.stopbits 91 | 92 | #Controlling DTR and RTS pins to put USB2SERIAL in transmit mode 93 | 94 | COM_Port.setRTS(1) #RTS=1,~RTS=0 so ~RE=0,Receive mode enabled for MAX485 95 | COM_Port.setDTR(1) #DTR=1,~DTR=0 so DE=0,(In FT232 RTS and DTR pins are inverted) 96 | #~RE and DE LED's on USB2SERIAL board will be off 97 | 98 | print '\n DTR = 1,~DTR = 0 so DE = 0' 99 | print ' RTS = 1,~RTS = 0 so ~RE = 0,Receive mode enabled for MAX485' 100 | 101 | print '\n Waiting for data.....\n' 102 | 103 | RxedData = COM_Port.readline() 104 | print ' ',RxedData, '\n' 105 | 106 | COM_Port.close() # Close the Serial port 107 | 108 | banner_bottom() # Display the bottom banner 109 | dummy = raw_input() # press any key to close 110 | -------------------------------------------------------------------------------- /USB2SERIAL_RS485_Read/PC_Receiver/Python 3.x.x/RS485_Read.py: -------------------------------------------------------------------------------- 1 | #----------------------------------------------------------------------------------------------------# 2 | # RS485 Communication using Python (Read) (hardware = USB2SERIAL) (Python 3.x) # 3 | #----------------------------------------------------------------------------------------------------# 4 | # Program runs on the PC side and waits for a string to be send by microcontroller.Upon receiving- # 5 | # -the string program displays on terminal.Program uses PySerial module to communicate with Serial # 6 | # Port of USB2SERIAL # 7 | #----------------------------------------------------------------------------------------------------# 8 | # BaudRate -> 9600 # 9 | # Data formt -> 8 databits,No parity,1 Stop bit (8N1) # 10 | # Flow Control -> None # 11 | #----------------------------------------------------------------------------------------------------# 12 | 13 | #====================================================================================================# 14 | # www.xanthium.in # 15 | # Copyright (C) 2015 Rahul.S # 16 | #====================================================================================================# 17 | 18 | #====================================================================================================# 19 | # Interpreter/IDE : Python 3.x/IDLE # 20 | # Module : PySerial # # 21 | # OS : Windows(Windows 7)/Linux # 22 | # Programmer : Rahul.S # 23 | # Date : 31-March-2015 # 24 | #====================================================================================================# 25 | 26 | #====================================================================================================# 27 | # Finding out the serial port number corresponding to your Computer # 28 | #====================================================================================================# 29 | # On Linux # 30 | #----------------------------------------------------------------------------------------------------# 31 | # You can use "dmesg | grep ttyS" to list the serial ports available in your PC. # 32 | # Hardware Serial ports are listed as ttyS* (eg ttyS1,ttyS2 etc) # 33 | # USB2SERIAL will be listed as ttyUSB*(eg ttyUSB0,or ttyUSB1) # 34 | # # 35 | # You can also connect your USB2SERIAL to PC USB port and use "dmesg |tail" to find out the name of- # 36 | # -the USB to Serial Converter. # 37 | #----------------------------------------------------------------------------------------------------# 38 | # On Windows # 39 | #----------------------------------------------------------------------------------------------------# 40 | # Use "Device Manager" in Windows to find out the COM Port number allotted to USB2SERIAL converter- # 41 | # -in your Computer,under ports # 42 | #====================================================================================================# 43 | 44 | #====================================================================================================# 45 | # Running the Program # 46 | #====================================================================================================# 47 | # On Linux # 48 | #----------------------------------------------------------------------------------------------------# 49 | # Find out your serial port name and number corresponding to USB2SERIAL on your system.It will be- # 50 | # -usually in the form of ttyUSB0 and ttyUSB1. # 51 | # Open terminal and type the following command to invoke Python3.x interpretor # 52 | # [user@linux:~$] sudo python RS485_Write.py # 53 | # Give the password and then enter your portnumber when program asks ->/dev/ttyUSB0 # 54 | #----------------------------------------------------------------------------------------------------# 55 | # On Windows # 56 | #----------------------------------------------------------------------------------------------------# 57 | # Open the command prompt and type the following # 58 | # C:\>python RS485_Write.py # 59 | # Enter the COM number when program asks -> com31 # 60 | #====================================================================================================# 61 | 62 | import serial # import the module 63 | 64 | def banner_top(): 65 | print(' +-------------------------------------------+') 66 | print(' | USB2SERIAL RS485 Read in Python 3.x |') 67 | print(' | (c) www.xanthium.in |') 68 | print(' +-------------------------------------------+') 69 | 70 | def Usage(): 71 | print(' | Windows -> COMxx eg COM32 |') 72 | print(' | Linux ->/dev/ttyS* eg /dev/ttyUSB0 |') 73 | print(' +-------------------------------------------+') 74 | 75 | def banner_bottom(): 76 | print(' +-------------------------------------------+') 77 | print(' | Press Any Key to Exit |') 78 | print(' +-------------------------------------------+') 79 | 80 | banner_top() # Display the top banner 81 | Usage() 82 | COM_PortName = input('\n Enter the COM Port Name ->') 83 | 84 | #Opening the serial port 85 | 86 | COM_Port = serial.Serial(COM_PortName) # open the COM port 87 | print('\n ',COM_PortName,'Opened') 88 | 89 | COM_Port.baudrate = 9600 # set Baud rate 90 | COM_Port.bytesize = 8 # Number of data bits = 8 91 | COM_Port.parity = 'N' # No parity 92 | COM_Port.stopbits = 1 # Number of Stop bits = 1 93 | 94 | print('\n Baud rate = ',COM_Port.baudrate) 95 | print(' Data bits = ',COM_Port.bytesize) 96 | print(' Parity = ',COM_Port.parity) 97 | print(' Stop bits = ',COM_Port.stopbits) 98 | 99 | #Controlling DTR and RTS pins to put USB2SERIAL in Receive mode 100 | 101 | COM_Port.setRTS(1) #RTS=1,~RTS=0 so ~RE=0,Receive mode enabled for MAX485 102 | COM_Port.setDTR(1) #DTR=1,~DTR=0 so DE=0,(In FT232 RTS and DTR pins are inverted) 103 | #~RE and DE LED's on USB2SERIAL board will be off 104 | 105 | print('\n DTR = 1,~DTR = 0 so DE = 0') 106 | print(' RTS = 1,~RTS = 0 so ~RE = 0,Receive mode enabled for MAX485') 107 | 108 | print('\n Waiting for data.....\n') 109 | 110 | RxedData = COM_Port.readline() 111 | print(' ',RxedData, '\n') 112 | 113 | COM_Port.close() # Close the Serial port 114 | 115 | banner_bottom() # Display the bottom banner 116 | dummy = input() # press any key to close 117 | -------------------------------------------------------------------------------- /USB2SERIAL_RS485_Write/MSP430_Receiver/MSP430_uart_rs485_reieve .c: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------------------------------// 2 | // RS485 Reception (Reciever side) // 3 | //----------------------------------------------------------------------------------------------------// 4 | // Program to recieve the data send from the PC through RS485 link.MSP430G2553 is interfced with a- // 5 | // -RS485 decoder chip (MAX485).MSP430 waits for Character send from the PC and toggles the LED. // // 6 | // PC sends Character 'A' or 'B'. // 7 | // MSP430 on Rxing A toggles LED connected to P1.0. // 8 | // MSP430 on Rxing B toggles LED connected to P1.1. // 9 | //----------------------------------------------------------------------------------------------------// 10 | // // 11 | // |------------| // 12 | // | RS485 | +---------+ +-----------+ // 13 | // | Write.exe | USB2SERIAL V1.0 | RO|---->|P1.1(RXD) | // 14 | // |------------| |--------------| | ~RE|-----|P2.1 | // 15 | // \ [][][][][] \===========| USB -> RS485 |~~~~~~~~~~~~~~~~~~~|A,B | | | // 16 | // \------------\ USB |--------------| Twisted | DE|-----|P2.0 | // 17 | // Laptop Pair | DI|<----|P1.2(TXD) | // 18 | // +---------+ +-----------+ // 19 | // MAX485 MSP430G2553 // 20 | // // 21 | // [Transmitter] ---------------------------------------> [--------Reciever-----------] // 22 | // // 23 | //====================================================================================================// 24 | // Hardware // 25 | // // 26 | // MSP430G2553 on LaunchPad Development board Connected to MAX485. // 27 | // Clocks :- DC0 @ 1MHz ,SMCLK @1MHz // 28 | // Baudrate :- 9600bps // 29 | // [MSP430] P1.1(RXD) <- R0 [RS485](Recieve Out) // 30 | // [MSP430] P1.2(TXD) -> DI [RS485](Driver Input) // 31 | // [MSP430] P2.0 -> DE [RS485](Driver Out Enable) // 32 | // [MSP430] P2.1 -> RE [RS485](Recieve Enable) // 33 | //----------------------------------------------------------------------------------------------------// 34 | // Compiled on IAR Embedded Workbench for MSP430 version 5.30.1 // 35 | // 02-October-2014 // 36 | // Rahul.S // 37 | //----------------------------------------------------------------------------------------------------// 38 | 39 | /*====================================================================================================*/ 40 | /* www.xanthium.in */ 41 | /* Copyright (C) 2014 Rahul.S */ 42 | /*====================================================================================================*/ 43 | 44 | #include "msp430g2553.h" 45 | void main(void) 46 | { 47 | WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog 48 | 49 | //------------------- Configure the Clocks -------------------// 50 | 51 | if (CALBC1_1MHZ==0xFF) // If calibration constant erased 52 | { 53 | while(1); // do not load, trap CPU!! 54 | } 55 | 56 | DCOCTL = 0; // Select lowest DCOx and MODx settings 57 | BCSCTL1 = CALBC1_1MHZ; // Set range 58 | DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation 59 | 60 | //------------------- Configure the Ports -------------------// 61 | 62 | // Setting the UART function for P1.1 & P1.2 63 | P1SEL |= BIT1 + BIT2; // P1.1 UCA0RXD input 64 | P1SEL2 |= BIT1 + BIT2; // P1.2 UCA0TXD output 65 | 66 | // Setting the direction for P1.0, P1.6, P2.0 and P2.1 67 | P1DIR |= BIT0 + BIT6 ; // P1.0,P1.6 output(leds) 68 | P2DIR |= BIT0 + BIT1; // P2.0 -> DE,P2.1 -> RE Output 69 | P1OUT &= ~BIT0 + BIT6; // Clear P1.0 70 | 71 | //Enabling MAX485 in recieve mode 72 | P2OUT &= ~BIT0; //P2.0 -> DE (low) 73 | P2OUT &= ~BIT1; //P2.1 -> RE (low) 74 | 75 | //-------------- Configure USCI_A0 in UART mode --------------// 76 | 77 | UCA0CTL1 |= UCSSEL_2 + UCSWRST; // USCI Clock = SMCLK,USCI_A0 disabled 78 | UCA0BR0 = 104; // 104 From datasheet table- 79 | UCA0BR1 = 0; // -selects baudrate =9600,clk = SMCLK 80 | UCA0MCTL = UCBRS_1; // Modulation value = 1 from datasheet 81 | 82 | UCA0CTL1 &= ~UCSWRST; // Clear UCSWRST to enable USCI_A0 83 | 84 | //-------------- Configure USCI_A0 interrupts --------------// 85 | 86 | IE2 |= UCA0RXIE; //Enable Recieve interrupt 87 | 88 | _BIS_SR(LPM0_bits + GIE); // Going to LPM0 89 | 90 | }//end of main() 91 | 92 | //----------------- USCI_A0 Recieve ISR --------------------------// 93 | 94 | #pragma vector = USCIAB0RX_VECTOR 95 | __interrupt void USCI_A0_RecieveInterrupt(void) 96 | { 97 | IFG2 &= ~UCA0RXIFG; //Clear the UCA0RXIFG 98 | // Switch to ontrol P1.0 and P1.6 99 | switch(UCA0RXBUF) 100 | { 101 | case 'A': P1OUT ^= BIT0; 102 | break; 103 | case 'B': P1OUT ^= BIT6; 104 | break; 105 | } 106 | 107 | } -------------------------------------------------------------------------------- /USB2SERIAL_RS485_Write/PC_Transmitter/Python 2.7.x/RS485_Write_2.7.py: -------------------------------------------------------------------------------- 1 | #----------------------------------------------------------------------------------------------------# 2 | # RS485 Communication using Python (Write) (hardware = USB2SERIAL) (Python 2.7.x) # 3 | #----------------------------------------------------------------------------------------------------# 4 | #Program runs on the PC side and transmits a character to the Serial Port @9600bps .Program uses # 5 | #PySerial module to communicate with Serial Port of USB2SERIAL # 6 | #----------------------------------------------------------------------------------------------------# 7 | # BaudRate -> 9600 # 8 | # Data formt -> 8 databits,No parity,1 Stop bit (8N1) # 9 | # Flow Control -> None # 10 | #----------------------------------------------------------------------------------------------------# 11 | 12 | #====================================================================================================# 13 | # www.xanthium.in # 14 | # Copyright (C) 2015 Rahul.S # 15 | #====================================================================================================# 16 | 17 | #====================================================================================================# 18 | # Interpreter/IDE : Python 2.7.x/IDLE # 19 | # Module : PySerial # # 20 | # OS : Windows(Windows 7)/Linux # 21 | # Programmer : Rahul.S # 22 | # Date : 31-March-2015 # 23 | #====================================================================================================# 24 | 25 | #====================================================================================================# 26 | # Finding out the serial port number corresponding to your Computer # 27 | #====================================================================================================# 28 | # On Linux # 29 | #----------------------------------------------------------------------------------------------------# 30 | # USB2SERIAL will be detected as ttyUSB0 or ttyUSB1.You can check the port number of USB2SERIAL by # 31 | # connecting you board to USB port and doing # 32 | # dmesg | tail # 33 | # and checking the output. # 34 | #====================================================================================================# 35 | 36 | #====================================================================================================# 37 | # Running the Program # 38 | #====================================================================================================# 39 | # On Linux # 40 | #----------------------------------------------------------------------------------------------------# 41 | # Find out your serial port name and number corresponding to USB2SERIAL on your system.It will be- # 42 | # -usually in the form of ttyUSB0 and ttyUSB1. # 43 | # Open terminal and type the following command to invoke Python3.x interpretor # 44 | # [user@linux:~$] sudo python RS485_Write.py # 45 | # Give the password and then enter your portnumber when program asks ->/dev/ttyUSB0 # 46 | #----------------------------------------------------------------------------------------------------# 47 | # On Windows # 48 | #----------------------------------------------------------------------------------------------------# 49 | # Open the command prompt and type the following # 50 | # C:\>python RS485_Write.py # 51 | # Enter the COM number when program asks -> com31 # 52 | #====================================================================================================# 53 | 54 | import serial # import the module 55 | 56 | def banner_top(): 57 | print ' +-------------------------------------------+' 58 | print ' | USB2SERIAL RS485 Write in Python 2.7.x |' 59 | print ' | (c) www.xanthium.in |' 60 | print ' +-------------------------------------------+' 61 | 62 | def Usage(): 63 | print ' | Windows -> COMxx eg COM32 |' 64 | print ' | Linux ->/dev/ttyS* eg /dev/ttyUSB0 |' 65 | print ' +-------------------------------------------+' 66 | 67 | def banner_bottom(): 68 | print ' +-------------------------------------------+' 69 | print ' | Press Any Key to Exit |' 70 | print ' +-------------------------------------------+' 71 | 72 | banner_top() # Display the top banner 73 | Usage() 74 | COM_PortName = raw_input('\n Enter the COM Port Name ->') 75 | 76 | #Opening the serial port 77 | 78 | COM_Port = serial.Serial(COM_PortName) # open the COM port 79 | print '\n ',COM_PortName,'Opened' 80 | 81 | COM_Port.baudrate = 9600 # set Baud rate 82 | COM_Port.bytesize = 8 # Number of data bits = 8 83 | COM_Port.parity = 'N' # No parity 84 | COM_Port.stopbits = 1 # Number of Stop bits = 1 85 | 86 | print '\n Baud rate = ',COM_Port.baudrate 87 | print ' Data bits = ',COM_Port.bytesize 88 | print ' Parity = ',COM_Port.parity 89 | print ' Stop bits = ',COM_Port.stopbits 90 | 91 | #Controlling DTR and RTS pins to put USB2SERIAL in transmit mode 92 | 93 | COM_Port.setDTR(0) #DTR=0,~DTR=1 so DE = 1,Transmit mode enabled 94 | COM_Port.setRTS(0) #RTS=0,~RTS=1 (In FT232 RTS and DTR pins are inverted) 95 | 96 | print '\n DTR = 0,~DTR = 1 so DE = 1,Transmit mode enabled' 97 | print ' RTS = 0,~RTS = 1' 98 | 99 | #Write character 'A' to serial port 100 | data = bytearray(b'A') # Convert Character to byte array 101 | NoOfBytes = COM_Port.write(data) # Write data to serial port 102 | 103 | print '\n ',NoOfBytes,' bytes written' 104 | print '\n A written to',COM_PortName 105 | 106 | COM_Port.close() # Close the Serial port 107 | 108 | banner_bottom() # Display the bottom banner 109 | dummy = raw_input() # press any key to close 110 | -------------------------------------------------------------------------------- /USB2SERIAL_RS485_Write/PC_Transmitter/Python 3.x.x/RS485_Write.py: -------------------------------------------------------------------------------- 1 | #----------------------------------------------------------------------------------------------------# 2 | # RS485 Communication using Python (Write) (hardware = USB2SERIAL) (Python 3.x) # 3 | #----------------------------------------------------------------------------------------------------# 4 | #Program runs on the PC side and transmits a character to the Serial Port @9600bps .Program uses # 5 | #PySerial module to communicate with Serial Port # 6 | #----------------------------------------------------------------------------------------------------# 7 | # BaudRate -> 9600 # 8 | # Data formt -> 8 databits,No parity,1 Stop bit (8N1) # 9 | # Flow Control -> None # 10 | #----------------------------------------------------------------------------------------------------# 11 | 12 | #====================================================================================================# 13 | # www.xanthium.in # 14 | # Copyright (C) 2015 Rahul.S # 15 | #====================================================================================================# 16 | 17 | #====================================================================================================# 18 | # Interpreter/IDE : Python 3.x/IDLE # 19 | # Module : PySerial # # 20 | # OS : Windows(Windows 7)/Linux # 21 | # Programmer : Rahul.S # 22 | # Date : 31-March-2015 # 23 | #====================================================================================================# 24 | 25 | #====================================================================================================# 26 | # Finding out the serial port number corresponding to your Computer # 27 | #====================================================================================================# 28 | # On Linux # 29 | #----------------------------------------------------------------------------------------------------# 30 | # You can use "dmesg | grep ttyS" to list the serial ports available in your PC. # 31 | # Hardware Serial ports are listed as ttyS* (eg ttyS1,ttyS2 etc) # 32 | # USB2SERIAL will be listed as ttyUSB*(eg ttyUSB0,or ttyUSB1) # 33 | # # 34 | # You can also connect your USB2SERIAL to PC USB port and use "dmesg |tail" to find out the name of- # 35 | # -the USB to Serial Converter. # 36 | #----------------------------------------------------------------------------------------------------# 37 | # On Windows # 38 | #----------------------------------------------------------------------------------------------------# 39 | # Use "Device Manager" in Windows to find out the COM Port number allotted to USB2SERIAL converter- # 40 | # -in your Computer,under ports # 41 | #====================================================================================================# 42 | 43 | #====================================================================================================# 44 | # Running the Program # 45 | #====================================================================================================# 46 | # On Linux # 47 | #----------------------------------------------------------------------------------------------------# 48 | # Find out your serial port name and number corresponding to USB2SERIAL on your system.It will be- # 49 | # -usually in the form of ttyUSB0 and ttyUSB1. # 50 | # Open terminal and type the following command to invoke Python3.x interpretor # 51 | # [user@linux:~$] sudo python RS485_Write.py # 52 | # Give the password and then enter your portnumber when program asks ->/dev/ttyUSB0 # 53 | #----------------------------------------------------------------------------------------------------# 54 | # On Windows # 55 | #----------------------------------------------------------------------------------------------------# 56 | # Open the command prompt and type the following # 57 | # C:\>python RS485_Write.py # 58 | # Enter the COM number when program asks -> com31 # 59 | #====================================================================================================# 60 | 61 | import serial # import the module 62 | 63 | def banner_top(): 64 | print(' +-------------------------------------------+') 65 | print(' | USB2SERIAL RS485 Write in Python 3.x |') 66 | print(' | (c) www.xanthium.in |') 67 | print(' +-------------------------------------------+') 68 | 69 | def Usage(): 70 | print(' | Windows -> COMxx eg COM32 |') 71 | print(' | Linux ->/dev/ttyS* eg /dev/ttyUSB0 |') 72 | print(' +-------------------------------------------+') 73 | 74 | def banner_bottom(): 75 | print(' +-------------------------------------------+') 76 | print(' | Press Any Key to Exit |') 77 | print(' +-------------------------------------------+') 78 | 79 | banner_top() # Display the top banner 80 | Usage() 81 | COM_PortName = input('\n Enter the COM Port Name ->') 82 | 83 | #Opening the serial port 84 | 85 | COM_Port = serial.Serial(COM_PortName) # open the COM port 86 | print('\n ',COM_PortName,'Opened') 87 | 88 | COM_Port.baudrate = 9600 # set Baud rate 89 | COM_Port.bytesize = 8 # Number of data bits = 8 90 | COM_Port.parity = 'N' # No parity 91 | COM_Port.stopbits = 1 # Number of Stop bits = 1 92 | 93 | print('\n Baud rate = ',COM_Port.baudrate) 94 | print(' Data bits = ',COM_Port.bytesize) 95 | print(' Parity = ',COM_Port.parity) 96 | print(' Stop bits = ',COM_Port.stopbits) 97 | 98 | #Controlling DTR and RTS pins to put USB2SERIAL in transmit mode 99 | 100 | COM_Port.setDTR(0) #DTR=0,~DTR=1 so DE = 1,Transmit mode enabled 101 | COM_Port.setRTS(0) #RTS=0,~RTS=1 (In FT232 RTS and DTR pins are inverted) 102 | 103 | print('\n DTR = 0,~DTR = 1 so DE = 1,Transmit mode enabled') 104 | print(' RTS = 0,~RTS = 1') 105 | 106 | #Write character 'A' to serial port 107 | data = bytearray(b'A') # Convert Character to byte array 108 | NoOfBytes = COM_Port.write(data) # Write data to serial port 109 | 110 | print('\n ',NoOfBytes,' bytes written') 111 | print('\n A written to',COM_PortName) 112 | 113 | COM_Port.close() # Close the Serial port 114 | 115 | banner_bottom() # Display the bottom banner 116 | dummy = input() # press any key to close 117 | --------------------------------------------------------------------------------